Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update OnRecvPacket method to panic when an error is returned by the VM (backport #1298) #1303

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion x/wasm/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (k Keeper) OnRecvPacket(
res, gasUsed, execErr := k.wasmVM.IBCPacketReceive(codeInfo.CodeHash, env, msg, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)
if execErr != nil {
return nil, sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
panic(execErr)
}
if res.Err != "" { // handle error case as before https://github.com/CosmWasm/wasmvm/commit/c300106fe5c9426a495f8e10821e00a9330c56c6
return nil, sdkerrors.Wrap(types.ErrExecuteFailed, res.Err)
Expand Down
10 changes: 9 additions & 1 deletion x/wasm/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func TestOnConnectChannel(t *testing.T) {
Channel: myChannel,
},
}

err := keepers.WasmKeeper.OnConnectChannel(ctx, spec.contractAddr, msg)

// then
Expand Down Expand Up @@ -325,6 +326,7 @@ func TestOnRecvPacket(t *testing.T) {
expContractGas sdk.Gas
expAck []byte
expErr bool
expPanic bool
expEventTypes []string
}{
"consume contract gas": {
Expand All @@ -349,7 +351,7 @@ func TestOnRecvPacket(t *testing.T) {
Attributes: []wasmvmtypes.EventAttribute{{Key: "Foo", Value: "Bar"}},
},
contractErr: errors.New("test, ignore"),
expErr: true,
expPanic: true,
},
"dispatch contract messages on success": {
contractAddr: example.Contract,
Expand Down Expand Up @@ -444,6 +446,12 @@ func TestOnRecvPacket(t *testing.T) {

// when
msg := wasmvmtypes.IBCPacketReceiveMsg{Packet: myPacket}
if spec.expPanic {
assert.Panics(t, func() {
keepers.WasmKeeper.OnRecvPacket(ctx, spec.contractAddr, msg)
})
return
}
gotAck, err := keepers.WasmKeeper.OnRecvPacket(ctx, spec.contractAddr, msg)

// then
Expand Down
41 changes: 28 additions & 13 deletions x/wasm/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ func TestFromIBCTransferToContract(t *testing.T) {

transferAmount := sdk.NewInt(1)
specs := map[string]struct {
contract wasmtesting.IBCContractCallbacks
setupContract func(t *testing.T, contract wasmtesting.IBCContractCallbacks, chain *wasmibctesting.TestChain)
expChainABalanceDiff sdk.Int
expChainBBalanceDiff sdk.Int
contract wasmtesting.IBCContractCallbacks
setupContract func(t *testing.T, contract wasmtesting.IBCContractCallbacks, chain *wasmibctesting.TestChain)
expChainAPendingSendPackets int
expChainBPendingSendPackets int
expChainABalanceDiff sdk.Int
expChainBBalanceDiff sdk.Int
expErr bool
}{
"ack": {
contract: &ackReceiverContract{},
Expand All @@ -43,26 +46,33 @@ func TestFromIBCTransferToContract(t *testing.T) {
c.t = t
c.chain = chain
},
expChainABalanceDiff: transferAmount.Neg(),
expChainBBalanceDiff: transferAmount,
expChainAPendingSendPackets: 0,
expChainBPendingSendPackets: 0,
expChainABalanceDiff: transferAmount.Neg(),
expChainBBalanceDiff: transferAmount,
},
"nack": {
contract: &nackReceiverContract{},
setupContract: func(t *testing.T, contract wasmtesting.IBCContractCallbacks, chain *wasmibctesting.TestChain) {
c := contract.(*nackReceiverContract)
c.t = t
},
expChainABalanceDiff: sdk.ZeroInt(),
expChainBBalanceDiff: sdk.ZeroInt(),
expChainAPendingSendPackets: 0,
expChainBPendingSendPackets: 0,
expChainABalanceDiff: sdk.ZeroInt(),
expChainBBalanceDiff: sdk.ZeroInt(),
},
"error": {
contract: &errorReceiverContract{},
setupContract: func(t *testing.T, contract wasmtesting.IBCContractCallbacks, chain *wasmibctesting.TestChain) {
c := contract.(*errorReceiverContract)
c.t = t
},
expChainABalanceDiff: sdk.ZeroInt(),
expChainBBalanceDiff: sdk.ZeroInt(),
expChainAPendingSendPackets: 1,
expChainBPendingSendPackets: 0,
expChainABalanceDiff: transferAmount.Neg(),
expChainBBalanceDiff: sdk.ZeroInt(),
expErr: true,
},
}
for name, spec := range specs {
Expand Down Expand Up @@ -100,6 +110,7 @@ func TestFromIBCTransferToContract(t *testing.T) {
// when transfer via sdk transfer from A (module) -> B (contract)
coinToSendToB := sdk.NewCoin(sdk.DefaultBondDenom, transferAmount)
timeoutHeight := clienttypes.NewHeight(1, 110)

msg := ibctransfertypes.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, coinToSendToB, chainA.SenderAccount.GetAddress().String(), chainB.SenderAccount.GetAddress().String(), timeoutHeight, 0)
_, err := chainA.SendMsgs(msg)
require.NoError(t, err)
Expand All @@ -111,11 +122,15 @@ func TestFromIBCTransferToContract(t *testing.T) {

// and when relay to chain B and handle Ack on chain A
err = coordinator.RelayAndAckPendingPackets(path)
require.NoError(t, err)
if spec.expErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}

// then
require.Equal(t, 0, len(chainA.PendingSendPackets))
require.Equal(t, 0, len(chainB.PendingSendPackets))
require.Equal(t, spec.expChainAPendingSendPackets, len(chainA.PendingSendPackets))
require.Equal(t, spec.expChainBPendingSendPackets, len(chainB.PendingSendPackets))

// and source chain balance was decreased
newChainABalance := chainA.Balance(chainA.SenderAccount.GetAddress(), sdk.DefaultBondDenom)
Expand Down