github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/keeper/relay_test.go (about) 1 package keeper 2 3 //import ( 4 // "encoding/json" 5 // "errors" 6 // "math" 7 // "testing" 8 // 9 // wasmvm "github.com/CosmWasm/wasmvm" 10 // wasmvmtypes "github.com/CosmWasm/wasmvm/types" 11 // sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 12 // "github.com/stretchr/testify/assert" 13 // "github.com/stretchr/testify/require" 14 // 15 // "github.com/fibonacci-chain/fbc/x/wasm/keeper/wasmtesting" 16 // "github.com/fibonacci-chain/fbc/x/wasm/types" 17 //) 18 // 19 //func TestOnOpenChannel(t *testing.T) { 20 // var m wasmtesting.MockWasmer 21 // wasmtesting.MakeIBCInstantiable(&m) 22 // messenger := &wasmtesting.MockMessageHandler{} 23 // parentCtx, keepers := CreateTestInput(t, false, SupportedFeatures, WithMessageHandler(messenger)) 24 // example := SeedNewContractInstance(t, parentCtx, keepers, &m) 25 // const myContractGas = 40 26 // 27 // specs := map[string]struct { 28 // contractAddr sdk.AccAddress 29 // contractGas sdk.Gas 30 // contractErr error 31 // expGas uint64 32 // expErr bool 33 // }{ 34 // "consume contract gas": { 35 // contractAddr: example.Contract, 36 // contractGas: myContractGas, 37 // expGas: myContractGas, 38 // }, 39 // "consume max gas": { 40 // contractAddr: example.Contract, 41 // contractGas: math.MaxUint64 / DefaultGasMultiplier, 42 // expGas: math.MaxUint64 / DefaultGasMultiplier, 43 // }, 44 // "consume gas on error": { 45 // contractAddr: example.Contract, 46 // contractGas: myContractGas, 47 // contractErr: errors.New("test, ignore"), 48 // expErr: true, 49 // }, 50 // "unknown contract address": { 51 // contractAddr: RandomAccountAddress(t), 52 // expErr: true, 53 // }, 54 // } 55 // for name, spec := range specs { 56 // t.Run(name, func(t *testing.T) { 57 // myChannel := wasmvmtypes.IBCChannel{Version: "my test channel"} 58 // myMsg := wasmvmtypes.IBCChannelOpenMsg{OpenTry: &wasmvmtypes.IBCOpenTry{Channel: myChannel, CounterpartyVersion: "foo"}} 59 // m.IBCChannelOpenFn = func(codeID wasmvm.Checksum, env wasmvmtypes.Env, msg wasmvmtypes.IBCChannelOpenMsg, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBC3ChannelOpenResponse, uint64, error) { 60 // assert.Equal(t, myMsg, msg) 61 // return &wasmvmtypes.IBC3ChannelOpenResponse{}, spec.contractGas * DefaultGasMultiplier, spec.contractErr 62 // } 63 // 64 // ctx, _ := parentCtx.CacheContext() 65 // before := ctx.GasMeter().GasConsumed() 66 // 67 // // when 68 // msg := wasmvmtypes.IBCChannelOpenMsg{ 69 // OpenTry: &wasmvmtypes.IBCOpenTry{ 70 // Channel: myChannel, 71 // CounterpartyVersion: "foo", 72 // }, 73 // } 74 // _, err := keepers.WasmKeeper.OnOpenChannel(ctx, spec.contractAddr, msg) 75 // 76 // // then 77 // if spec.expErr { 78 // require.Error(t, err) 79 // return 80 // } 81 // require.NoError(t, err) 82 // // verify gas consumed 83 // const storageCosts = sdk.Gas(2903) 84 // assert.Equal(t, spec.expGas, ctx.GasMeter().GasConsumed()-before-storageCosts) 85 // }) 86 // } 87 //} 88 // 89 //func TestOnConnectChannel(t *testing.T) { 90 // var m wasmtesting.MockWasmer 91 // wasmtesting.MakeIBCInstantiable(&m) 92 // messenger := &wasmtesting.MockMessageHandler{} 93 // parentCtx, keepers := CreateTestInput(t, false, SupportedFeatures, WithMessageHandler(messenger)) 94 // example := SeedNewContractInstance(t, parentCtx, keepers, &m) 95 // const myContractGas = 40 96 // 97 // specs := map[string]struct { 98 // contractAddr sdk.AccAddress 99 // contractResp *wasmvmtypes.IBCBasicResponse 100 // contractErr error 101 // overwriteMessenger *wasmtesting.MockMessageHandler 102 // expContractGas sdk.Gas 103 // expErr bool 104 // expEventTypes []string 105 // }{ 106 // "consume contract gas": { 107 // contractAddr: example.Contract, 108 // expContractGas: myContractGas, 109 // contractResp: &wasmvmtypes.IBCBasicResponse{}, 110 // }, 111 // "consume gas on error, ignore events + messages": { 112 // contractAddr: example.Contract, 113 // expContractGas: myContractGas, 114 // contractResp: &wasmvmtypes.IBCBasicResponse{ 115 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}}, 116 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 117 // }, 118 // contractErr: errors.New("test, ignore"), 119 // expErr: true, 120 // }, 121 // "dispatch contract messages on success": { 122 // contractAddr: example.Contract, 123 // expContractGas: myContractGas, 124 // contractResp: &wasmvmtypes.IBCBasicResponse{ 125 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}, {ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Custom: json.RawMessage(`{"foo":"bar"}`)}}}, 126 // }, 127 // }, 128 // "emit contract events on success": { 129 // contractAddr: example.Contract, 130 // expContractGas: myContractGas + 10, 131 // contractResp: &wasmvmtypes.IBCBasicResponse{ 132 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 133 // }, 134 // expEventTypes: []string{types.WasmModuleEventType}, 135 // }, 136 // "messenger errors returned, events stored": { 137 // contractAddr: example.Contract, 138 // expContractGas: myContractGas + 10, 139 // contractResp: &wasmvmtypes.IBCBasicResponse{ 140 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}, {ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Custom: json.RawMessage(`{"foo":"bar"}`)}}}, 141 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 142 // }, 143 // overwriteMessenger: wasmtesting.NewErroringMessageHandler(), 144 // expErr: true, 145 // expEventTypes: []string{types.WasmModuleEventType}, 146 // }, 147 // "unknown contract address": { 148 // contractAddr: RandomAccountAddress(t), 149 // expErr: true, 150 // }, 151 // } 152 // for name, spec := range specs { 153 // t.Run(name, func(t *testing.T) { 154 // myChannel := wasmvmtypes.IBCChannel{Version: "my test channel"} 155 // myMsg := wasmvmtypes.IBCChannelConnectMsg{OpenConfirm: &wasmvmtypes.IBCOpenConfirm{Channel: myChannel}} 156 // m.IBCChannelConnectFn = func(codeID wasmvm.Checksum, env wasmvmtypes.Env, msg wasmvmtypes.IBCChannelConnectMsg, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBCBasicResponse, uint64, error) { 157 // assert.Equal(t, msg, myMsg) 158 // return spec.contractResp, myContractGas * DefaultGasMultiplier, spec.contractErr 159 // } 160 // 161 // ctx, _ := parentCtx.CacheContext() 162 // ctx = ctx.WithEventManager(sdk.NewEventManager()) 163 // 164 // before := ctx.GasMeter().GasConsumed() 165 // msger, capturedMsgs := wasmtesting.NewCapturingMessageHandler() 166 // *messenger = *msger 167 // if spec.overwriteMessenger != nil { 168 // *messenger = *spec.overwriteMessenger 169 // } 170 // 171 // // when 172 // msg := wasmvmtypes.IBCChannelConnectMsg{ 173 // OpenConfirm: &wasmvmtypes.IBCOpenConfirm{ 174 // Channel: myChannel, 175 // }, 176 // } 177 // err := keepers.WasmKeeper.OnConnectChannel(ctx, spec.contractAddr, msg) 178 // 179 // // then 180 // if spec.expErr { 181 // require.Error(t, err) 182 // assert.Empty(t, capturedMsgs) // no messages captured on error 183 // assert.Equal(t, spec.expEventTypes, stripTypes(ctx.EventManager().Events())) 184 // return 185 // } 186 // require.NoError(t, err) 187 // // verify gas consumed 188 // const storageCosts = sdk.Gas(2903) 189 // assert.Equal(t, spec.expContractGas, ctx.GasMeter().GasConsumed()-before-storageCosts) 190 // // verify msgs dispatched 191 // require.Len(t, *capturedMsgs, len(spec.contractResp.Messages)) 192 // for i, m := range spec.contractResp.Messages { 193 // assert.Equal(t, (*capturedMsgs)[i], m.Msg) 194 // } 195 // assert.Equal(t, spec.expEventTypes, stripTypes(ctx.EventManager().Events())) 196 // }) 197 // } 198 //} 199 // 200 //func TestOnCloseChannel(t *testing.T) { 201 // var m wasmtesting.MockWasmer 202 // wasmtesting.MakeIBCInstantiable(&m) 203 // messenger := &wasmtesting.MockMessageHandler{} 204 // parentCtx, keepers := CreateTestInput(t, false, SupportedFeatures, WithMessageHandler(messenger)) 205 // example := SeedNewContractInstance(t, parentCtx, keepers, &m) 206 // const myContractGas = 40 207 // 208 // specs := map[string]struct { 209 // contractAddr sdk.AccAddress 210 // contractResp *wasmvmtypes.IBCBasicResponse 211 // contractErr error 212 // overwriteMessenger *wasmtesting.MockMessageHandler 213 // expContractGas sdk.Gas 214 // expErr bool 215 // expEventTypes []string 216 // }{ 217 // "consume contract gas": { 218 // contractAddr: example.Contract, 219 // expContractGas: myContractGas, 220 // contractResp: &wasmvmtypes.IBCBasicResponse{}, 221 // }, 222 // "consume gas on error, ignore events + messages": { 223 // contractAddr: example.Contract, 224 // expContractGas: myContractGas, 225 // contractResp: &wasmvmtypes.IBCBasicResponse{ 226 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}}, 227 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 228 // }, 229 // contractErr: errors.New("test, ignore"), 230 // expErr: true, 231 // }, 232 // "dispatch contract messages on success": { 233 // contractAddr: example.Contract, 234 // expContractGas: myContractGas, 235 // contractResp: &wasmvmtypes.IBCBasicResponse{ 236 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}, {ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Custom: json.RawMessage(`{"foo":"bar"}`)}}}, 237 // }, 238 // }, 239 // "emit contract events on success": { 240 // contractAddr: example.Contract, 241 // expContractGas: myContractGas + 10, 242 // contractResp: &wasmvmtypes.IBCBasicResponse{ 243 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 244 // }, 245 // expEventTypes: []string{types.WasmModuleEventType}, 246 // }, 247 // "messenger errors returned, events stored": { 248 // contractAddr: example.Contract, 249 // expContractGas: myContractGas + 10, 250 // contractResp: &wasmvmtypes.IBCBasicResponse{ 251 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}, {ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Custom: json.RawMessage(`{"foo":"bar"}`)}}}, 252 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 253 // }, 254 // overwriteMessenger: wasmtesting.NewErroringMessageHandler(), 255 // expErr: true, 256 // expEventTypes: []string{types.WasmModuleEventType}, 257 // }, 258 // "unknown contract address": { 259 // contractAddr: RandomAccountAddress(t), 260 // expErr: true, 261 // }, 262 // } 263 // for name, spec := range specs { 264 // t.Run(name, func(t *testing.T) { 265 // myChannel := wasmvmtypes.IBCChannel{Version: "my test channel"} 266 // myMsg := wasmvmtypes.IBCChannelCloseMsg{CloseInit: &wasmvmtypes.IBCCloseInit{Channel: myChannel}} 267 // m.IBCChannelCloseFn = func(codeID wasmvm.Checksum, env wasmvmtypes.Env, msg wasmvmtypes.IBCChannelCloseMsg, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBCBasicResponse, uint64, error) { 268 // assert.Equal(t, msg, myMsg) 269 // return spec.contractResp, myContractGas * DefaultGasMultiplier, spec.contractErr 270 // } 271 // 272 // ctx, _ := parentCtx.CacheContext() 273 // before := ctx.GasMeter().GasConsumed() 274 // msger, capturedMsgs := wasmtesting.NewCapturingMessageHandler() 275 // *messenger = *msger 276 // 277 // if spec.overwriteMessenger != nil { 278 // *messenger = *spec.overwriteMessenger 279 // } 280 // 281 // // when 282 // msg := wasmvmtypes.IBCChannelCloseMsg{ 283 // CloseInit: &wasmvmtypes.IBCCloseInit{ 284 // Channel: myChannel, 285 // }, 286 // } 287 // err := keepers.WasmKeeper.OnCloseChannel(ctx, spec.contractAddr, msg) 288 // 289 // // then 290 // if spec.expErr { 291 // require.Error(t, err) 292 // assert.Empty(t, capturedMsgs) // no messages captured on error 293 // assert.Equal(t, spec.expEventTypes, stripTypes(ctx.EventManager().Events())) 294 // return 295 // } 296 // require.NoError(t, err) 297 // // verify gas consumed 298 // const storageCosts = sdk.Gas(2903) 299 // assert.Equal(t, spec.expContractGas, ctx.GasMeter().GasConsumed()-before-storageCosts) 300 // // verify msgs dispatched 301 // require.Len(t, *capturedMsgs, len(spec.contractResp.Messages)) 302 // for i, m := range spec.contractResp.Messages { 303 // assert.Equal(t, (*capturedMsgs)[i], m.Msg) 304 // } 305 // assert.Equal(t, spec.expEventTypes, stripTypes(ctx.EventManager().Events())) 306 // }) 307 // } 308 //} 309 // 310 //func TestOnRecvPacket(t *testing.T) { 311 // var m wasmtesting.MockWasmer 312 // wasmtesting.MakeIBCInstantiable(&m) 313 // messenger := &wasmtesting.MockMessageHandler{} 314 // parentCtx, keepers := CreateTestInput(t, false, SupportedFeatures, WithMessageHandler(messenger)) 315 // example := SeedNewContractInstance(t, parentCtx, keepers, &m) 316 // const myContractGas = 40 317 // const storageCosts = sdk.Gas(2903) 318 // 319 // specs := map[string]struct { 320 // contractAddr sdk.AccAddress 321 // contractResp *wasmvmtypes.IBCReceiveResponse 322 // contractErr error 323 // overwriteMessenger *wasmtesting.MockMessageHandler 324 // mockReplyFn func(codeID wasmvm.Checksum, env wasmvmtypes.Env, reply wasmvmtypes.Reply, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) 325 // expContractGas sdk.Gas 326 // expAck []byte 327 // expErr bool 328 // expEventTypes []string 329 // }{ 330 // "consume contract gas": { 331 // contractAddr: example.Contract, 332 // expContractGas: myContractGas, 333 // contractResp: &wasmvmtypes.IBCReceiveResponse{ 334 // Acknowledgement: []byte("myAck"), 335 // }, 336 // expAck: []byte("myAck"), 337 // }, 338 // "can return empty ack": { 339 // contractAddr: example.Contract, 340 // expContractGas: myContractGas, 341 // contractResp: &wasmvmtypes.IBCReceiveResponse{}, 342 // }, 343 // "consume gas on error, ignore events + messages": { 344 // contractAddr: example.Contract, 345 // expContractGas: myContractGas, 346 // contractResp: &wasmvmtypes.IBCReceiveResponse{ 347 // Acknowledgement: []byte("myAck"), 348 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}}, 349 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 350 // }, 351 // contractErr: errors.New("test, ignore"), 352 // expErr: true, 353 // }, 354 // "dispatch contract messages on success": { 355 // contractAddr: example.Contract, 356 // expContractGas: myContractGas, 357 // contractResp: &wasmvmtypes.IBCReceiveResponse{ 358 // Acknowledgement: []byte("myAck"), 359 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}, {ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Custom: json.RawMessage(`{"foo":"bar"}`)}}}, 360 // }, 361 // expAck: []byte("myAck"), 362 // }, 363 // "emit contract attributes on success": { 364 // contractAddr: example.Contract, 365 // expContractGas: myContractGas + 10, 366 // contractResp: &wasmvmtypes.IBCReceiveResponse{ 367 // Acknowledgement: []byte("myAck"), 368 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 369 // }, 370 // expEventTypes: []string{types.WasmModuleEventType}, 371 // expAck: []byte("myAck"), 372 // }, 373 // "emit contract events on success": { 374 // contractAddr: example.Contract, 375 // expContractGas: myContractGas + 46, // charge or custom event as well 376 // contractResp: &wasmvmtypes.IBCReceiveResponse{ 377 // Acknowledgement: []byte("myAck"), 378 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 379 // Events: []wasmvmtypes.Event{{ 380 // Type: "custom", 381 // Attributes: []wasmvmtypes.EventAttribute{{ 382 // Key: "message", 383 // Value: "to rudi", 384 // }}, 385 // }}, 386 // }, 387 // expEventTypes: []string{types.WasmModuleEventType, "wasm-custom"}, 388 // expAck: []byte("myAck"), 389 // }, 390 // "messenger errors returned, events stored": { 391 // contractAddr: example.Contract, 392 // expContractGas: myContractGas + 10, 393 // contractResp: &wasmvmtypes.IBCReceiveResponse{ 394 // Acknowledgement: []byte("myAck"), 395 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}, {ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Custom: json.RawMessage(`{"foo":"bar"}`)}}}, 396 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 397 // }, 398 // overwriteMessenger: wasmtesting.NewErroringMessageHandler(), 399 // expErr: true, 400 // expEventTypes: []string{types.WasmModuleEventType}, 401 // }, 402 // "submessage reply can overwrite ack data": { 403 // contractAddr: example.Contract, 404 // expContractGas: myContractGas + storageCosts, 405 // contractResp: &wasmvmtypes.IBCReceiveResponse{ 406 // Acknowledgement: []byte("myAck"), 407 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyAlways, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}}, 408 // }, 409 // mockReplyFn: func(codeID wasmvm.Checksum, env wasmvmtypes.Env, reply wasmvmtypes.Reply, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) { 410 // return &wasmvmtypes.Response{Data: []byte("myBetterAck")}, 0, nil 411 // }, 412 // expAck: []byte("myBetterAck"), 413 // expEventTypes: []string{types.EventTypeReply}, 414 // }, 415 // "unknown contract address": { 416 // contractAddr: RandomAccountAddress(t), 417 // expErr: true, 418 // }, 419 // } 420 // for name, spec := range specs { 421 // t.Run(name, func(t *testing.T) { 422 // myPacket := wasmvmtypes.IBCPacket{Data: []byte("my data")} 423 // 424 // m.IBCPacketReceiveFn = func(codeID wasmvm.Checksum, env wasmvmtypes.Env, msg wasmvmtypes.IBCPacketReceiveMsg, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBCReceiveResult, uint64, error) { 425 // assert.Equal(t, myPacket, msg.Packet) 426 // return &wasmvmtypes.IBCReceiveResult{Ok: spec.contractResp}, myContractGas * DefaultGasMultiplier, spec.contractErr 427 // } 428 // if spec.mockReplyFn != nil { 429 // m.ReplyFn = spec.mockReplyFn 430 // h, ok := keepers.WasmKeeper.wasmVMResponseHandler.(*DefaultWasmVMContractResponseHandler) 431 // require.True(t, ok) 432 // h.md = NewMessageDispatcher(messenger, keepers.WasmKeeper) 433 // } 434 // 435 // ctx, _ := parentCtx.CacheContext() 436 // before := ctx.GasMeter().GasConsumed() 437 // 438 // msger, capturedMsgs := wasmtesting.NewCapturingMessageHandler() 439 // *messenger = *msger 440 // 441 // if spec.overwriteMessenger != nil { 442 // *messenger = *spec.overwriteMessenger 443 // } 444 // 445 // // when 446 // msg := wasmvmtypes.IBCPacketReceiveMsg{Packet: myPacket} 447 // gotAck, err := keepers.WasmKeeper.OnRecvPacket(ctx, spec.contractAddr, msg) 448 // 449 // // then 450 // if spec.expErr { 451 // require.Error(t, err) 452 // assert.Empty(t, capturedMsgs) // no messages captured on error 453 // assert.Equal(t, spec.expEventTypes, stripTypes(ctx.EventManager().Events())) 454 // return 455 // } 456 // require.NoError(t, err) 457 // require.Equal(t, spec.expAck, gotAck) 458 // 459 // // verify gas consumed 460 // const storageCosts = sdk.Gas(2903) 461 // assert.Equal(t, spec.expContractGas, ctx.GasMeter().GasConsumed()-before-storageCosts) 462 // // verify msgs dispatched 463 // require.Len(t, *capturedMsgs, len(spec.contractResp.Messages)) 464 // for i, m := range spec.contractResp.Messages { 465 // assert.Equal(t, (*capturedMsgs)[i], m.Msg) 466 // } 467 // assert.Equal(t, spec.expEventTypes, stripTypes(ctx.EventManager().Events())) 468 // }) 469 // } 470 //} 471 // 472 //func TestOnAckPacket(t *testing.T) { 473 // var m wasmtesting.MockWasmer 474 // wasmtesting.MakeIBCInstantiable(&m) 475 // messenger := &wasmtesting.MockMessageHandler{} 476 // parentCtx, keepers := CreateTestInput(t, false, SupportedFeatures, WithMessageHandler(messenger)) 477 // example := SeedNewContractInstance(t, parentCtx, keepers, &m) 478 // const myContractGas = 40 479 // 480 // specs := map[string]struct { 481 // contractAddr sdk.AccAddress 482 // contractResp *wasmvmtypes.IBCBasicResponse 483 // contractErr error 484 // overwriteMessenger *wasmtesting.MockMessageHandler 485 // expContractGas sdk.Gas 486 // expErr bool 487 // expEventTypes []string 488 // }{ 489 // "consume contract gas": { 490 // contractAddr: example.Contract, 491 // expContractGas: myContractGas, 492 // contractResp: &wasmvmtypes.IBCBasicResponse{}, 493 // }, 494 // "consume gas on error, ignore events + messages": { 495 // contractAddr: example.Contract, 496 // expContractGas: myContractGas, 497 // contractResp: &wasmvmtypes.IBCBasicResponse{ 498 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}}, 499 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 500 // }, 501 // contractErr: errors.New("test, ignore"), 502 // expErr: true, 503 // }, 504 // "dispatch contract messages on success": { 505 // contractAddr: example.Contract, 506 // expContractGas: myContractGas, 507 // contractResp: &wasmvmtypes.IBCBasicResponse{ 508 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}, {ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Custom: json.RawMessage(`{"foo":"bar"}`)}}}, 509 // }, 510 // }, 511 // "emit contract events on success": { 512 // contractAddr: example.Contract, 513 // expContractGas: myContractGas + 10, 514 // contractResp: &wasmvmtypes.IBCBasicResponse{ 515 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 516 // }, 517 // expEventTypes: []string{types.WasmModuleEventType}, 518 // }, 519 // "messenger errors returned, events stored": { 520 // contractAddr: example.Contract, 521 // expContractGas: myContractGas + 10, 522 // contractResp: &wasmvmtypes.IBCBasicResponse{ 523 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}, {ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Custom: json.RawMessage(`{"foo":"bar"}`)}}}, 524 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 525 // }, 526 // overwriteMessenger: wasmtesting.NewErroringMessageHandler(), 527 // expErr: true, 528 // expEventTypes: []string{types.WasmModuleEventType}, 529 // }, 530 // "unknown contract address": { 531 // contractAddr: RandomAccountAddress(t), 532 // expErr: true, 533 // }, 534 // } 535 // for name, spec := range specs { 536 // t.Run(name, func(t *testing.T) { 537 // myAck := wasmvmtypes.IBCPacketAckMsg{Acknowledgement: wasmvmtypes.IBCAcknowledgement{Data: []byte("myAck")}} 538 // m.IBCPacketAckFn = func(codeID wasmvm.Checksum, env wasmvmtypes.Env, msg wasmvmtypes.IBCPacketAckMsg, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBCBasicResponse, uint64, error) { 539 // assert.Equal(t, myAck, msg) 540 // return spec.contractResp, myContractGas * DefaultGasMultiplier, spec.contractErr 541 // } 542 // 543 // ctx, _ := parentCtx.CacheContext() 544 // before := ctx.GasMeter().GasConsumed() 545 // msger, capturedMsgs := wasmtesting.NewCapturingMessageHandler() 546 // *messenger = *msger 547 // 548 // if spec.overwriteMessenger != nil { 549 // *messenger = *spec.overwriteMessenger 550 // } 551 // 552 // // when 553 // err := keepers.WasmKeeper.OnAckPacket(ctx, spec.contractAddr, myAck) 554 // 555 // // then 556 // 557 // if spec.expErr { 558 // require.Error(t, err) 559 // assert.Empty(t, capturedMsgs) // no messages captured on error 560 // assert.Equal(t, spec.expEventTypes, stripTypes(ctx.EventManager().Events())) 561 // return 562 // } 563 // require.NoError(t, err) 564 // // verify gas consumed 565 // const storageCosts = sdk.Gas(2903) 566 // assert.Equal(t, spec.expContractGas, ctx.GasMeter().GasConsumed()-before-storageCosts) 567 // // verify msgs dispatched 568 // require.Len(t, *capturedMsgs, len(spec.contractResp.Messages)) 569 // for i, m := range spec.contractResp.Messages { 570 // assert.Equal(t, (*capturedMsgs)[i], m.Msg) 571 // } 572 // assert.Equal(t, spec.expEventTypes, stripTypes(ctx.EventManager().Events())) 573 // }) 574 // } 575 //} 576 // 577 //func TestOnTimeoutPacket(t *testing.T) { 578 // var m wasmtesting.MockWasmer 579 // wasmtesting.MakeIBCInstantiable(&m) 580 // messenger := &wasmtesting.MockMessageHandler{} 581 // parentCtx, keepers := CreateTestInput(t, false, SupportedFeatures, WithMessageHandler(messenger)) 582 // example := SeedNewContractInstance(t, parentCtx, keepers, &m) 583 // const myContractGas = 40 584 // 585 // specs := map[string]struct { 586 // contractAddr sdk.AccAddress 587 // contractResp *wasmvmtypes.IBCBasicResponse 588 // contractErr error 589 // overwriteMessenger *wasmtesting.MockMessageHandler 590 // expContractGas sdk.Gas 591 // expErr bool 592 // expEventTypes []string 593 // }{ 594 // "consume contract gas": { 595 // contractAddr: example.Contract, 596 // expContractGas: myContractGas, 597 // contractResp: &wasmvmtypes.IBCBasicResponse{}, 598 // }, 599 // "consume gas on error, ignore events + messages": { 600 // contractAddr: example.Contract, 601 // expContractGas: myContractGas, 602 // contractResp: &wasmvmtypes.IBCBasicResponse{ 603 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}}, 604 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 605 // }, 606 // contractErr: errors.New("test, ignore"), 607 // expErr: true, 608 // }, 609 // "dispatch contract messages on success": { 610 // contractAddr: example.Contract, 611 // expContractGas: myContractGas, 612 // contractResp: &wasmvmtypes.IBCBasicResponse{ 613 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}, {ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Custom: json.RawMessage(`{"foo":"bar"}`)}}}, 614 // }, 615 // }, 616 // "emit contract attributes on success": { 617 // contractAddr: example.Contract, 618 // expContractGas: myContractGas + 10, 619 // contractResp: &wasmvmtypes.IBCBasicResponse{ 620 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 621 // }, 622 // expEventTypes: []string{types.WasmModuleEventType}, 623 // }, 624 // "emit contract events on success": { 625 // contractAddr: example.Contract, 626 // expContractGas: myContractGas + 46, // cost for custom events 627 // contractResp: &wasmvmtypes.IBCBasicResponse{ 628 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 629 // Events: []wasmvmtypes.Event{{ 630 // Type: "custom", 631 // Attributes: []wasmvmtypes.EventAttribute{{ 632 // Key: "message", 633 // Value: "to rudi", 634 // }}, 635 // }}, 636 // }, 637 // expEventTypes: []string{types.WasmModuleEventType, "wasm-custom"}, 638 // }, 639 // "messenger errors returned, events stored before": { 640 // contractAddr: example.Contract, 641 // expContractGas: myContractGas + 10, 642 // contractResp: &wasmvmtypes.IBCBasicResponse{ 643 // Messages: []wasmvmtypes.SubMsg{{ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Bank: &wasmvmtypes.BankMsg{}}}, {ReplyOn: wasmvmtypes.ReplyNever, Msg: wasmvmtypes.CosmosMsg{Custom: json.RawMessage(`{"foo":"bar"}`)}}}, 644 // Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}}, 645 // }, 646 // overwriteMessenger: wasmtesting.NewErroringMessageHandler(), 647 // expErr: true, 648 // expEventTypes: []string{types.WasmModuleEventType}, 649 // }, 650 // "unknown contract address": { 651 // contractAddr: RandomAccountAddress(t), 652 // expErr: true, 653 // }, 654 // } 655 // for name, spec := range specs { 656 // t.Run(name, func(t *testing.T) { 657 // myPacket := wasmvmtypes.IBCPacket{Data: []byte("my test packet")} 658 // m.IBCPacketTimeoutFn = func(codeID wasmvm.Checksum, env wasmvmtypes.Env, msg wasmvmtypes.IBCPacketTimeoutMsg, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.IBCBasicResponse, uint64, error) { 659 // assert.Equal(t, myPacket, msg.Packet) 660 // return spec.contractResp, myContractGas * DefaultGasMultiplier, spec.contractErr 661 // } 662 // 663 // ctx, _ := parentCtx.CacheContext() 664 // before := ctx.GasMeter().GasConsumed() 665 // msger, capturedMsgs := wasmtesting.NewCapturingMessageHandler() 666 // *messenger = *msger 667 // 668 // if spec.overwriteMessenger != nil { 669 // *messenger = *spec.overwriteMessenger 670 // } 671 // 672 // // when 673 // msg := wasmvmtypes.IBCPacketTimeoutMsg{Packet: myPacket} 674 // err := keepers.WasmKeeper.OnTimeoutPacket(ctx, spec.contractAddr, msg) 675 // 676 // // then 677 // if spec.expErr { 678 // require.Error(t, err) 679 // assert.Empty(t, capturedMsgs) // no messages captured on error 680 // assert.Equal(t, spec.expEventTypes, stripTypes(ctx.EventManager().Events())) 681 // return 682 // } 683 // require.NoError(t, err) 684 // // verify gas consumed 685 // const storageCosts = sdk.Gas(2903) 686 // assert.Equal(t, spec.expContractGas, ctx.GasMeter().GasConsumed()-before-storageCosts) 687 // // verify msgs dispatched 688 // require.Len(t, *capturedMsgs, len(spec.contractResp.Messages)) 689 // for i, m := range spec.contractResp.Messages { 690 // assert.Equal(t, (*capturedMsgs)[i], m.Msg) 691 // } 692 // assert.Equal(t, spec.expEventTypes, stripTypes(ctx.EventManager().Events())) 693 // }) 694 // } 695 //} 696 // 697 //func stripTypes(events sdk.Events) []string { 698 // var r []string 699 // for _, e := range events { 700 // r = append(r, e.Type) 701 // } 702 // return r 703 //}