diff --git a/x/wasm/internal/keeper/keeper.go b/x/wasm/internal/keeper/keeper.go index d4d0c0e..1996e90 100644 --- a/x/wasm/internal/keeper/keeper.go +++ b/x/wasm/internal/keeper/keeper.go @@ -91,10 +91,11 @@ func (k Keeper) Instantiate(ctx sdk.Context, creator sdk.AccAddress, codeID uint // get contact info store := ctx.KVStore(k.storeKey) bz := store.Get(types.GetCodeKey(codeID)) - var codeInfo types.CodeInfo - if bz != nil { - k.cdc.MustUnmarshalBinaryBare(bz, &codeInfo) + if bz == nil { + return nil, types.ErrNotFound("contract") } + var codeInfo types.CodeInfo + k.cdc.MustUnmarshalBinaryBare(bz, &codeInfo) // prepare params for contract instantiate call params := types.NewParams(ctx, creator, deposit, contractAccount) @@ -129,17 +130,19 @@ func (k Keeper) Instantiate(ctx sdk.Context, creator sdk.AccAddress, codeID uint func (k Keeper) Execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller sdk.AccAddress, coins sdk.Coins, msgs []byte) (sdk.Result, sdk.Error) { store := ctx.KVStore(k.storeKey) - var contract types.ContractInfo contractBz := store.Get(types.GetContractAddressKey(contractAddress)) - if contractBz != nil { - k.cdc.MustUnmarshalBinaryBare(contractBz, &contract) + if contractBz == nil { + return sdk.Result{}, types.ErrNotFound("contract") } + var contract types.ContractInfo + k.cdc.MustUnmarshalBinaryBare(contractBz, &contract) - var codeInfo types.CodeInfo contractInfoBz := store.Get(types.GetCodeKey(contract.CodeID)) - if contractInfoBz != nil { - k.cdc.MustUnmarshalBinaryBare(contractInfoBz, &codeInfo) + if contractInfoBz == nil { + return sdk.Result{}, types.ErrNotFound("contract info") } + var codeInfo types.CodeInfo + k.cdc.MustUnmarshalBinaryBare(contractInfoBz, &codeInfo) // add more funds sdkerr := k.bankKeeper.SendCoins(ctx, caller, contractAddress, coins) diff --git a/x/wasm/internal/keeper/keeper_test.go b/x/wasm/internal/keeper/keeper_test.go index b575eb2..93657b6 100644 --- a/x/wasm/internal/keeper/keeper_test.go +++ b/x/wasm/internal/keeper/keeper_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + "github.com/cosmwasm/wasmd/x/wasm/internal/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -75,6 +76,27 @@ func TestInstantiate(t *testing.T) { require.Equal(t, kvStoreGas+288, gasAfter-gasBefore) } +func TestInstantiateWithNonExistingCodeID(t *testing.T) { + tempDir, err := ioutil.TempDir("", "wasm") + require.NoError(t, err) + defer os.RemoveAll(tempDir) + ctx, accKeeper, keeper := CreateTestInput(t, false, tempDir) + + deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000)) + creator := createFakeFundedAccount(ctx, accKeeper, deposit) + + require.NoError(t, err) + + initMsg := InitMsg{} + initMsgBz, err := json.Marshal(initMsg) + require.NoError(t, err) + + const nonExistingCodeID = 9999 + addr, err := keeper.Instantiate(ctx, creator, nonExistingCodeID, initMsgBz, nil) + require.Error(t, err, types.ErrNotFound("contract")) + require.Nil(t, addr) +} + func TestExecute(t *testing.T) { tempDir, err := ioutil.TempDir("", "wasm") require.NoError(t, err) @@ -154,6 +176,21 @@ func TestExecute(t *testing.T) { t.Logf("Duration: %v (81488 gas)\n", diff) } +func TestExecuteWithNonExistingAddress(t *testing.T) { + tempDir, err := ioutil.TempDir("", "wasm") + require.NoError(t, err) + defer os.RemoveAll(tempDir) + ctx, accKeeper, keeper := CreateTestInput(t, false, tempDir) + + deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000)) + creator := createFakeFundedAccount(ctx, accKeeper, deposit.Add(deposit)) + + // unauthorized - trialCtx so we don't change state + nonExistingAddress := addrFromUint64(9999) + _, err = keeper.Execute(ctx, nonExistingAddress, creator, nil, []byte(`{}`)) + require.Error(t, err, types.ErrNotFound("contract info")) +} + type InitMsg struct { Verifier string `json:"verifier"` Beneficiary string `json:"beneficiary"` diff --git a/x/wasm/internal/types/errors.go b/x/wasm/internal/types/errors.go index 37cdcaa..2ae113c 100644 --- a/x/wasm/internal/types/errors.go +++ b/x/wasm/internal/types/errors.go @@ -16,6 +16,7 @@ const ( CodeExecuteFailed sdk.CodeType = 4 CodeGasLimit sdk.CodeType = 5 CodeInvalidGenesis sdk.CodeType = 6 + CodeNotFound sdk.CodeType = 7 ) // ErrCreateFailed error for wasm code that has already been uploaded or failed @@ -47,3 +48,8 @@ func ErrGasLimit(msg string) sdk.Error { func ErrInvalidGenesis(msg string) sdk.Error { return sdk.NewError(DefaultCodespace, CodeInvalidGenesis, fmt.Sprintf("invalid genesis: %s", msg)) } + +// ErrNotFound error for an entry not found in the stoe +func ErrNotFound(msg string) sdk.Error { + return sdk.NewError(DefaultCodespace, CodeNotFound, fmt.Sprintf("not found: %s", msg)) +}