Skip to content

Commit

Permalink
Merge pull request #427 from CosmWasm/instantiate_rsp_data_385
Browse files Browse the repository at this point in the history
Add data field to Instantiate response
  • Loading branch information
ethanfrey authored Mar 3, 2021
2 parents cca60e2 + 4a3e12a commit 573bada
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 115 deletions.
1 change: 1 addition & 0 deletions doc/proto.md
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ MsgInstantiateContractResponse return instantiation result data
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| address | [string](#string) | | Address is the bech32 address of the new contract instance. |
| data | [bytes](#bytes) | | Data contains base64-encoded bytes to returned from the contract |



Expand Down
2 changes: 1 addition & 1 deletion x/wasm/internal/keeper/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestBindingPortForIBCContractOnInstantiate(t *testing.T) {

// create a second contract should give yet another portID (and different address)
creator := RandomAccountAddress(t)
addr, err := keepers.WasmKeeper.Instantiate(ctx, example.CodeID, creator, nil, initMsgBz, "ibc-reflect-2", nil)
addr, _, err := keepers.WasmKeeper.Instantiate(ctx, example.CodeID, creator, nil, initMsgBz, "ibc-reflect-2", nil)
require.NoError(t, err)
require.NotEqual(t, example.Contract, addr)

Expand Down
24 changes: 12 additions & 12 deletions x/wasm/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,28 +215,28 @@ func (k Keeper) importCode(ctx sdk.Context, codeID uint64, codeInfo types.CodeIn
}

// Instantiate creates an instance of a WASM contract
func (k Keeper) Instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.AccAddress, initMsg []byte, label string, deposit sdk.Coins) (sdk.AccAddress, error) {
func (k Keeper) Instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.AccAddress, initMsg []byte, label string, deposit sdk.Coins) (sdk.AccAddress, []byte, error) {
return k.instantiate(ctx, codeID, creator, admin, initMsg, label, deposit, k.authZPolicy)
}

func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.AccAddress, initMsg []byte, label string, deposit sdk.Coins, authZ AuthorizationPolicy) (sdk.AccAddress, error) {
func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.AccAddress, initMsg []byte, label string, deposit sdk.Coins, authZ AuthorizationPolicy) (sdk.AccAddress, []byte, error) {
ctx.GasMeter().ConsumeGas(InstanceCost, "Loading CosmWasm module: init")

// create contract address
contractAddress := k.generateContractAddress(ctx, codeID)
existingAcct := k.accountKeeper.GetAccount(ctx, contractAddress)
if existingAcct != nil {
return nil, sdkerrors.Wrap(types.ErrAccountExists, existingAcct.GetAddress().String())
return nil, nil, sdkerrors.Wrap(types.ErrAccountExists, existingAcct.GetAddress().String())
}

// deposit initial contract funds
if !deposit.IsZero() {
if k.bankKeeper.BlockedAddr(creator) {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "blocked address can not be used")
return nil, nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "blocked address can not be used")
}
sdkerr := k.bankKeeper.SendCoins(ctx, creator, contractAddress, deposit)
if sdkerr != nil {
return nil, sdkerr
return nil, nil, sdkerr
}
} else {
// create an empty account (so we don't have issues later)
Expand All @@ -249,13 +249,13 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetCodeKey(codeID))
if bz == nil {
return nil, sdkerrors.Wrap(types.ErrNotFound, "code")
return nil, nil, sdkerrors.Wrap(types.ErrNotFound, "code")
}
var codeInfo types.CodeInfo
k.cdc.MustUnmarshalBinaryBare(bz, &codeInfo)

if !authZ.CanInstantiateContract(codeInfo.InstantiateConfig, creator) {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "can not instantiate")
return nil, nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "can not instantiate")
}

// prepare params for contract instantiate call
Expand All @@ -278,7 +278,7 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A
res, gasUsed, err := k.wasmer.Instantiate(codeInfo.CodeHash, env, info, initMsg, prefixStore, cosmwasmAPI, querier, gasMeter(ctx), gas)
consumeGas(ctx, gasUsed)
if err != nil {
return contractAddress, sdkerrors.Wrap(types.ErrInstantiateFailed, err.Error())
return contractAddress, nil, sdkerrors.Wrap(types.ErrInstantiateFailed, err.Error())
}

// emit all events from this contract itself
Expand All @@ -292,13 +292,13 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A
// check for IBC flag
report, err := k.wasmer.AnalyzeCode(codeInfo.CodeHash)
if err != nil {
return contractAddress, sdkerrors.Wrap(types.ErrInstantiateFailed, err.Error())
return contractAddress, nil, sdkerrors.Wrap(types.ErrInstantiateFailed, err.Error())
}
if report.HasIBCEntryPoints {
// register IBC port
ibcPort, err := k.ensureIbcPort(ctx, contractAddress)
if err != nil {
return nil, err
return nil, nil, err
}
contractInfo.IBCPortID = ibcPort
}
Expand All @@ -309,10 +309,10 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A
// then dispatch so that contract could be called back
err = k.dispatchMessages(ctx, contractAddress, contractInfo.IBCPortID, res.Messages)
if err != nil {
return nil, err
return nil, nil, err
}

return contractAddress, nil
return contractAddress, res.Data, nil
}

// Execute executes the contract instance
Expand Down
56 changes: 30 additions & 26 deletions x/wasm/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"github.com/CosmWasm/wasmd/x/wasm/internal/keeper/wasmtesting"
wasmvm "github.com/CosmWasm/wasmvm"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"io/ioutil"
"testing"
"time"
Expand Down Expand Up @@ -274,17 +276,17 @@ func TestInstantiate(t *testing.T) {
gasBefore := ctx.GasMeter().GasConsumed()

// create with no balance is also legal
contractAddr, err := keeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, "demo contract 1", nil)
gotContractAddr, _, err := keeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, "demo contract 1", nil)
require.NoError(t, err)
require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", contractAddr.String())
require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", gotContractAddr.String())

gasAfter := ctx.GasMeter().GasConsumed()
if types.EnableGasVerification {
require.Equal(t, uint64(0x118c2), gasAfter-gasBefore)
}

// ensure it is stored properly
info := keeper.GetContractInfo(ctx, contractAddr)
info := keeper.GetContractInfo(ctx, gotContractAddr)
require.NotNil(t, info)
assert.Equal(t, creator.String(), info.Creator)
assert.Equal(t, codeID, info.CodeID)
Expand All @@ -296,7 +298,7 @@ func TestInstantiate(t *testing.T) {
Updated: types.NewAbsoluteTxPosition(ctx),
Msg: json.RawMessage(initMsgBz),
}}
assert.Equal(t, exp, keeper.GetContractHistory(ctx, contractAddr))
assert.Equal(t, exp, keeper.GetContractHistory(ctx, gotContractAddr))
}

func TestInstantiateWithDeposit(t *testing.T) {
Expand Down Expand Up @@ -345,7 +347,7 @@ func TestInstantiateWithDeposit(t *testing.T) {
require.NoError(t, err)

// when
addr, err := keeper.Instantiate(ctx, contractID, spec.srcActor, nil, initMsgBz, "my label", deposit)
addr, _, err := keeper.Instantiate(ctx, contractID, spec.srcActor, nil, initMsgBz, "my label", deposit)
// then
if spec.expError {
require.Error(t, err)
Expand Down Expand Up @@ -412,7 +414,7 @@ func TestInstantiateWithPermissions(t *testing.T) {
contractID, err := keeper.Create(ctx, myAddr, wasmCode, "https://github.com/CosmWasm/wasmd/blob/master/x/wasm/testdata/escrow.wasm", "", &spec.srcPermission)
require.NoError(t, err)

_, err = keeper.Instantiate(ctx, contractID, spec.srcActor, nil, initMsgBz, "demo contract 1", nil)
_, _, err = keeper.Instantiate(ctx, contractID, spec.srcActor, nil, initMsgBz, "demo contract 1", nil)
assert.True(t, spec.expError.Is(err), "got %+v", err)
})
}
Expand All @@ -430,24 +432,26 @@ func TestInstantiateWithNonExistingCodeID(t *testing.T) {
require.NoError(t, err)

const nonExistingCodeID = 9999
addr, err := keeper.Instantiate(ctx, nonExistingCodeID, creator, nil, initMsgBz, "demo contract 2", nil)
addr, _, err := keeper.Instantiate(ctx, nonExistingCodeID, creator, nil, initMsgBz, "demo contract 2", nil)
require.True(t, types.ErrNotFound.Is(err), err)
require.Nil(t, addr)
}

func TestInstantiateWithCallbackToContract(t *testing.T) {
func TestInstantiateWithContractDataResponse(t *testing.T) {
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
var (
executeCalled bool
err error
)
wasmerMock := wasmtesting.SelfCallingInstMockWasmer(&executeCalled)

keepers.WasmKeeper.wasmer = wasmerMock
example := StoreHackatomExampleContract(t, ctx, keepers)
_, err = keepers.WasmKeeper.Instantiate(ctx, example.CodeID, example.CreatorAddr, nil, nil, "test", nil)
wasmerMock := &wasmtesting.MockWasmer{
InstantiateFn: func(codeID wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, initMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64) (*wasmvmtypes.InitResponse, uint64, error) {
return &wasmvmtypes.InitResponse{Data: []byte("my-response-data")}, 0, nil
},
AnalyzeCodeFn: wasmtesting.WithoutIBCAnalyzeFn,
CreateFn: wasmtesting.NoOpCreateFn,
}

example := StoreRandomContract(t, ctx, keepers, wasmerMock)
_, data, err := keepers.WasmKeeper.Instantiate(ctx, example.CodeID, example.CreatorAddr, nil, nil, "test", nil)
require.NoError(t, err)
assert.True(t, executeCalled)
assert.Equal(t, []byte("my-response-data"), data)
}

func TestExecute(t *testing.T) {
Expand All @@ -473,7 +477,7 @@ func TestExecute(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)

addr, err := keeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract 3", deposit)
addr, _, err := keeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract 3", deposit)
require.NoError(t, err)
require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", addr.String())

Expand Down Expand Up @@ -582,7 +586,7 @@ func TestExecuteWithDeposit(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)

contractAddr, err := keeper.Instantiate(ctx, codeID, spec.srcActor, nil, initMsgBz, "my label", nil)
contractAddr, _, err := keeper.Instantiate(ctx, codeID, spec.srcActor, nil, initMsgBz, "my label", nil)
require.NoError(t, err)

// when
Expand Down Expand Up @@ -636,7 +640,7 @@ func TestExecuteWithPanic(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)

addr, err := keeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract 4", deposit)
addr, _, err := keeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract 4", deposit)
require.NoError(t, err)

// let's make sure we get a reasonable error, no panic/crash
Expand Down Expand Up @@ -670,7 +674,7 @@ func TestExecuteWithCpuLoop(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)

addr, err := keeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract 5", deposit)
addr, _, err := keeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract 5", deposit)
require.NoError(t, err)

// make sure we set a limit before calling
Expand Down Expand Up @@ -715,7 +719,7 @@ func TestExecuteWithStorageLoop(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)

addr, err := keeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract 6", deposit)
addr, _, err := keeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract 6", deposit)
require.NoError(t, err)

// make sure we set a limit before calling
Expand Down Expand Up @@ -876,7 +880,7 @@ func TestMigrate(t *testing.T) {
t.Run(msg, func(t *testing.T) {
// given a contract instance
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
contractAddr, err := keeper.Instantiate(ctx, spec.fromCodeID, creator, spec.admin, spec.initMsg, "demo contract", nil)
contractAddr, _, err := keeper.Instantiate(ctx, spec.fromCodeID, creator, spec.admin, spec.initMsg, "demo contract", nil)
require.NoError(t, err)
if spec.overrideContractAddr != nil {
contractAddr = spec.overrideContractAddr
Expand Down Expand Up @@ -969,7 +973,7 @@ func TestMigrateWithDispatchedMessage(t *testing.T) {
initMsgBz := initMsg.GetBytes(t)

ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
contractAddr, err := keeper.Instantiate(ctx, originalContractID, creator, fred, initMsgBz, "demo contract", deposit)
contractAddr, _, err := keeper.Instantiate(ctx, originalContractID, creator, fred, initMsgBz, "demo contract", deposit)
require.NoError(t, err)

migMsgBz := BurnerExampleInitMsg{Payout: myPayoutAddr}.GetBytes(t)
Expand Down Expand Up @@ -1101,7 +1105,7 @@ func TestUpdateContractAdmin(t *testing.T) {
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
addr, err := keeper.Instantiate(ctx, originalContractID, creator, spec.instAdmin, initMsgBz, "demo contract", nil)
addr, _, err := keeper.Instantiate(ctx, originalContractID, creator, spec.instAdmin, initMsgBz, "demo contract", nil)
require.NoError(t, err)
if spec.overrideContractAddr != nil {
addr = spec.overrideContractAddr
Expand Down Expand Up @@ -1167,7 +1171,7 @@ func TestClearContractAdmin(t *testing.T) {
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
addr, err := keeper.Instantiate(ctx, originalContractID, creator, spec.instAdmin, initMsgBz, "demo contract", nil)
addr, _, err := keeper.Instantiate(ctx, originalContractID, creator, spec.instAdmin, initMsgBz, "demo contract", nil)
require.NoError(t, err)
if spec.overrideContractAddr != nil {
addr = spec.overrideContractAddr
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/internal/keeper/legacy_querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestLegacyQueryContractState(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)

addr, err := keeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract to query", deposit)
addr, _, err := keeper.Instantiate(ctx, contractID, creator, nil, initMsgBz, "demo contract to query", deposit)
require.NoError(t, err)

contractModel := []types.Model{
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestLegacyQueryContractListByCodeOrdering(t *testing.T) {
ctx = setBlock(ctx, h)
h++
}
_, err = keeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, fmt.Sprintf("contract %d", i), topUp)
_, _, err = keeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, fmt.Sprintf("contract %d", i), topUp)
require.NoError(t, err)
}

Expand Down
3 changes: 2 additions & 1 deletion x/wasm/internal/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (m msgServer) InstantiateContract(goCtx context.Context, msg *types.MsgInst
}
}

contractAddr, err := m.keeper.Instantiate(ctx, msg.CodeID, senderAddr, adminAddr, msg.InitMsg, msg.Label, msg.Funds)
contractAddr, data, err := m.keeper.Instantiate(ctx, msg.CodeID, senderAddr, adminAddr, msg.InitMsg, msg.Label, msg.Funds)
if err != nil {
return nil, err
}
Expand All @@ -70,6 +70,7 @@ func (m msgServer) InstantiateContract(goCtx context.Context, msg *types.MsgInst

return &types.MsgInstantiateContractResponse{
Address: contractAddr.String(),
Data: data,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion x/wasm/internal/keeper/proposal_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func handleInstantiateProposal(ctx sdk.Context, k Keeper, p types.InstantiateCon
return sdkerrors.Wrap(err, "admin")
}

contractAddr, err := k.instantiate(ctx, p.CodeID, runAsAddr, adminAddr, p.InitMsg, p.Label, p.Funds, GovAuthorizationPolicy{})
contractAddr, _, err := k.instantiate(ctx, p.CodeID, runAsAddr, adminAddr, p.InitMsg, p.Label, p.Funds, GovAuthorizationPolicy{})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/internal/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func TestQueryContractListByCodeOrdering(t *testing.T) {
ctx = setBlock(ctx, h)
h++
}
_, err = keeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, fmt.Sprintf("contract %d", i), topUp)
_, _, err = keeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, fmt.Sprintf("contract %d", i), topUp)
require.NoError(t, err)
}

Expand Down
12 changes: 6 additions & 6 deletions x/wasm/internal/keeper/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestMaskReflectContractSend(t *testing.T) {

// creator instantiates a contract and gives it tokens
maskStart := sdk.NewCoins(sdk.NewInt64Coin("denom", 40000))
maskAddr, err := keeper.Instantiate(ctx, maskID, creator, nil, []byte("{}"), "mask contract 2", maskStart)
maskAddr, _, err := keeper.Instantiate(ctx, maskID, creator, nil, []byte("{}"), "mask contract 2", maskStart)
require.NoError(t, err)
require.NotEmpty(t, maskAddr)

Expand All @@ -108,7 +108,7 @@ func TestMaskReflectContractSend(t *testing.T) {
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
escrowStart := sdk.NewCoins(sdk.NewInt64Coin("denom", 25000))
escrowAddr, err := keeper.Instantiate(ctx, escrowID, creator, nil, initMsgBz, "escrow contract 2", escrowStart)
escrowAddr, _, err := keeper.Instantiate(ctx, escrowID, creator, nil, initMsgBz, "escrow contract 2", escrowStart)
require.NoError(t, err)
require.NotEmpty(t, escrowAddr)

Expand Down Expand Up @@ -172,7 +172,7 @@ func TestMaskReflectCustomMsg(t *testing.T) {

// creator instantiates a contract and gives it tokens
contractStart := sdk.NewCoins(sdk.NewInt64Coin("denom", 40000))
contractAddr, err := keeper.Instantiate(ctx, codeID, creator, nil, []byte("{}"), "mask contract 1", contractStart)
contractAddr, _, err := keeper.Instantiate(ctx, codeID, creator, nil, []byte("{}"), "mask contract 1", contractStart)
require.NoError(t, err)
require.NotEmpty(t, contractAddr)

Expand Down Expand Up @@ -263,7 +263,7 @@ func TestMaskReflectCustomQuery(t *testing.T) {

// creator instantiates a contract and gives it tokens
contractStart := sdk.NewCoins(sdk.NewInt64Coin("denom", 40000))
contractAddr, err := keeper.Instantiate(ctx, codeID, creator, nil, []byte("{}"), "mask contract 1", contractStart)
contractAddr, _, err := keeper.Instantiate(ctx, codeID, creator, nil, []byte("{}"), "mask contract 1", contractStart)
require.NoError(t, err)
require.NotEmpty(t, contractAddr)

Expand Down Expand Up @@ -316,7 +316,7 @@ func TestMaskReflectWasmQueries(t *testing.T) {

// creator instantiates a contract and gives it tokens
maskStart := sdk.NewCoins(sdk.NewInt64Coin("denom", 40000))
maskAddr, err := keeper.Instantiate(ctx, maskID, creator, nil, []byte("{}"), "mask contract 2", maskStart)
maskAddr, _, err := keeper.Instantiate(ctx, maskID, creator, nil, []byte("{}"), "mask contract 2", maskStart)
require.NoError(t, err)
require.NotEmpty(t, maskAddr)

Expand Down Expand Up @@ -387,7 +387,7 @@ func TestWasmRawQueryWithNil(t *testing.T) {

// creator instantiates a contract and gives it tokens
maskStart := sdk.NewCoins(sdk.NewInt64Coin("denom", 40000))
maskAddr, err := keeper.Instantiate(ctx, maskID, creator, nil, []byte("{}"), "mask contract 2", maskStart)
maskAddr, _, err := keeper.Instantiate(ctx, maskID, creator, nil, []byte("{}"), "mask contract 2", maskStart)
require.NoError(t, err)
require.NotEmpty(t, maskAddr)

Expand Down
Loading

0 comments on commit 573bada

Please sign in to comment.