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

fix: stop wrap twice the response of handling non-plus wasm message in plus handler #35

Merged
merged 5 commits into from
Apr 24, 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
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)
loloicci marked this conversation as resolved.
Show resolved Hide resolved
}
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")
}