Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Nov 9, 2020
1 parent 390d5e9 commit 77b2f21
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 55 deletions.
27 changes: 4 additions & 23 deletions x/wasm/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"testing"
"time"

"github.com/CosmWasm/go-cosmwasm"
wasmTypes "github.com/CosmWasm/go-cosmwasm/types"
"github.com/CosmWasm/wasmd/x/wasm/internal/types"
stypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -436,33 +434,16 @@ func TestInstantiateWithNonExistingCodeID(t *testing.T) {
func TestInstantiateWithCallbackToContract(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
var (
excuteCalled bool
err error
executeCalled bool
err error
)
wasmerMock := &MockWasmer{
CreateFn: func(code cosmwasm.WasmCode) (cosmwasm.CodeID, error) {
anyCodeID := bytes.Repeat([]byte{0x1}, 32)
return anyCodeID, nil
},
InstantiateFn: func(code cosmwasm.CodeID, env wasmTypes.Env, info wasmTypes.MessageInfo, initMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64) (*wasmTypes.InitResponse, uint64, error) {
return &wasmTypes.InitResponse{
Messages: []wasmTypes.CosmosMsg{
{Wasm: &wasmTypes.WasmMsg{Execute: &wasmTypes.ExecuteMsg{ContractAddr: env.Contract.Address, Msg: []byte(`{}`)}}},
},
}, 1, nil
},
ExecuteFn: func(code cosmwasm.CodeID, env wasmTypes.Env, info wasmTypes.MessageInfo, executeMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64) (*wasmTypes.HandleResponse, uint64, error) {
excuteCalled = true
return &wasmTypes.HandleResponse{}, 1, nil
},
}
wasmerMock := selfCallingInstMockWasmer(&executeCalled)

keepers.WasmKeeper.wasmer = wasmerMock
keepers.WasmKeeper.wasmer = wasmerMock
example := StoreHackatomExampleContract(t, ctx, keepers)
_, err = keepers.WasmKeeper.Instantiate(ctx, example.CodeID, example.CreatorAddr, nil, nil, "test", nil)
require.NoError(t, err)
assert.True(t, excuteCalled)
assert.True(t, executeCalled)
}

func TestExecute(t *testing.T) {
Expand Down
57 changes: 25 additions & 32 deletions x/wasm/internal/keeper/test_common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -415,38 +416,6 @@ func keyPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.AccAddress) {
return key, pub, addr
}

var _ types.WasmerEngine = &alwaysPanicMockWasmer{}

type alwaysPanicMockWasmer struct{}

func (a alwaysPanicMockWasmer) Create(code cosmwasm.WasmCode) (cosmwasm.CodeID, error) {
panic("not supposed to be called!")
}

func (a alwaysPanicMockWasmer) Instantiate(code cosmwasm.CodeID, env wasmTypes.Env, info wasmTypes.MessageInfo, initMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64) (*wasmTypes.InitResponse, uint64, error) {
panic("not supposed to be called!")
}

func (a alwaysPanicMockWasmer) Execute(code cosmwasm.CodeID, env wasmTypes.Env, info wasmTypes.MessageInfo, executeMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64) (*wasmTypes.HandleResponse, uint64, error) {
panic("not supposed to be called!")
}

func (a alwaysPanicMockWasmer) Query(code cosmwasm.CodeID, env wasmTypes.Env, queryMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64) ([]byte, uint64, error) {
panic("not supposed to be called!")
}

func (a alwaysPanicMockWasmer) Migrate(code cosmwasm.CodeID, env wasmTypes.Env, info wasmTypes.MessageInfo, migrateMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64) (*wasmTypes.MigrateResponse, uint64, error) {
panic("not supposed to be called!")
}

func (a alwaysPanicMockWasmer) GetCode(code cosmwasm.CodeID) (cosmwasm.WasmCode, error) {
panic("not supposed to be called!")
}

func (a alwaysPanicMockWasmer) Cleanup() {
panic("not supposed to be called!")
}

var _ types.WasmerEngine = &MockWasmer{}

// MockWasmer implements types.WasmerEngine for testing purpose. One or multiple messages can be stubbed.
Expand Down Expand Up @@ -510,3 +479,27 @@ func (m *MockWasmer) Cleanup() {
}
m.CleanupFn()
}

var alwaysPanicMockWasmer = &MockWasmer{}

// selfCallingInstMockWasmer prepares a Wasmer mock that calls itself on instantiation.
func selfCallingInstMockWasmer(executeCalled *bool) *MockWasmer {
return &MockWasmer{

CreateFn: func(code cosmwasm.WasmCode) (cosmwasm.CodeID, error) {
anyCodeID := bytes.Repeat([]byte{0x1}, 32)
return anyCodeID, nil
},
InstantiateFn: func(code cosmwasm.CodeID, env wasmTypes.Env, info wasmTypes.MessageInfo, initMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64) (*wasmTypes.InitResponse, uint64, error) {
return &wasmTypes.InitResponse{
Messages: []wasmTypes.CosmosMsg{
{Wasm: &wasmTypes.WasmMsg{Execute: &wasmTypes.ExecuteMsg{ContractAddr: env.Contract.Address, Msg: []byte(`{}`)}}},
},
}, 1, nil
},
ExecuteFn: func(code cosmwasm.CodeID, env wasmTypes.Env, info wasmTypes.MessageInfo, executeMsg []byte, store cosmwasm.KVStore, goapi cosmwasm.GoAPI, querier cosmwasm.Querier, gasMeter cosmwasm.GasMeter, gasLimit uint64) (*wasmTypes.HandleResponse, uint64, error) {
*executeCalled = true
return &wasmTypes.HandleResponse{}, 1, nil
},
}
}

0 comments on commit 77b2f21

Please sign in to comment.