Merge pull request #21 from cosmwasm/handle_not_found

Handel contact not found
This commit is contained in:
Ethan Frey 2020-01-13 15:58:40 +01:00 committed by GitHub
commit ce35d8de82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 9 deletions

View File

@ -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)

View File

@ -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"`

View File

@ -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))
}