Skip to content

Commit

Permalink
fix: stop wrap twice the response of handling non-plus wasm message i…
Browse files Browse the repository at this point in the history
…n plus handler (CosmWasm#35)

* fix: stop wrap twice non-plus wasm hander's response

fix Finschia#33

* test: add tests handling non-plus wasm messages

* docs: add this PR to CHANGELOG.md

* fix: reflect golangci-lint

* fix: simplify how to handle the message in wasmplus
  • Loading branch information
loloicci committed Apr 24, 2023
1 parent d08bbbd commit c23a2db
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Improvements

### Bug Fixes
* [\#35](https://github.com/Finschia/wasmd/pull/35) stop wrap twice the response of handling non-plus wasm message in plus handler

### Breaking Changes

Expand Down
14 changes: 5 additions & 9 deletions x/wasmplus/handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package wasmplus

import (
"strings"

"github.com/gogo/protobuf/proto"

sdk "github.com/Finschia/finschia-sdk/types"
Expand All @@ -22,13 +20,11 @@ func NewHandler(k wasmtypes.ContractOpsKeeper) sdk.Handler {
res proto.Message
err error
)
res, err = wasmHandler(ctx, msg)
if err != nil && strings.Contains(err.Error(), "MsgStoreCodeAndInstantiateContract") {
// handle wasmplus service
msg2, ok := msg.(*types.MsgStoreCodeAndInstantiateContract)
if ok {
res, err = msgServer.StoreCodeAndInstantiateContract(sdk.WrapSDKContext(ctx), msg2)
}
switch msg := msg.(type) {
case *types.MsgStoreCodeAndInstantiateContract:
res, err = msgServer.StoreCodeAndInstantiateContract(sdk.WrapSDKContext(ctx), msg)
default:
return wasmHandler(ctx, msg)
}
return sdk.WrapServiceResult(ctx, res, err)
}
Expand Down
48 changes: 48 additions & 0 deletions x/wasmplus/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ func parseStoreAndInitResponse(t *testing.T, data []byte) (uint64, string) {
return codeID, addr
}

// ensure store code returns the expected response
func assertStoreCodeResponse(t *testing.T, data []byte, expected uint64) {
var pStoreResp wasmtypes.MsgStoreCodeResponse
require.NoError(t, pStoreResp.Unmarshal(data))
require.Equal(t, pStoreResp.CodeID, expected)
}

type prettyEvent struct {
Type string
Attr []sdk.Attribute
Expand Down Expand Up @@ -397,3 +404,44 @@ func TestErrorsCreateAndInstantiate(t *testing.T) {
})
}
}

func TestHandleNonPlusWasmCreate(t *testing.T) {
data := setupTest(t)
creator := data.faucet.NewFundedRandomAccount(data.ctx, sdk.NewInt64Coin("denom", 100000))

h := data.module.Route().Handler()
q := data.module.LegacyQuerierHandler(nil)

msg := &wasmtypes.MsgStoreCode{
Sender: creator.String(),
WASMByteCode: testContract,
}

res, err := h(data.ctx, msg)
require.NoError(t, err)
assertStoreCodeResponse(t, res.Data, 1)

require.Equal(t, 2, len(res.Events), prettyEvents(res.Events))
assert.Equal(t, "message", res.Events[0].Type)
assertAttribute(t, "module", "wasm", res.Events[0].Attributes[0])
assert.Equal(t, "store_code", res.Events[1].Type)
assertAttribute(t, "code_id", "1", res.Events[1].Attributes[1])

assertCodeList(t, q, data.ctx, 1)
assertCodeBytes(t, q, data.ctx, 1, testContract)
}

func TestErrorHandleNonPlusWasmCreate(t *testing.T) {
data := setupTest(t)
creator := data.faucet.NewFundedRandomAccount(data.ctx, sdk.NewInt64Coin("denom", 100000))

h := data.module.Route().Handler()

msg := &wasmtypes.MsgStoreCode{
Sender: creator.String(),
WASMByteCode: []byte("invalid WASM contract"),
}

_, err := h(data.ctx, msg)
require.ErrorContains(t, err, "Wasm validation")
}

0 comments on commit c23a2db

Please sign in to comment.