Properly throw OutOfGas when we hit storage limits inside the contract

This commit is contained in:
Ethan Frey 2020-05-07 12:03:31 +02:00
parent b3fe657920
commit 8306d89010
2 changed files with 10 additions and 5 deletions

View File

@ -138,6 +138,10 @@ func (k Keeper) Instantiate(ctx sdk.Context, codeID uint64, creator sdk.AccAddre
gas := gasForContract(ctx)
res, err := k.wasmer.Instantiate(codeInfo.CodeHash, params, initMsg, prefixStore, cosmwasmAPI, querier, gas)
if err != nil {
// TODO: wasmer doesn't return wasm gas used on error. we should consume it (for error on metering failure)
// Note: OutOfGas panics (from storage) are caught by go-cosmwasm, subtract one more gas to check if
// this contract died due to gas limit in Storage
consumeGas(ctx, GasMultiplier)
return contractAddress, sdkerrors.Wrap(types.ErrInstantiateFailed, err.Error())
}
consumeGas(ctx, res.GasUsed)
@ -185,7 +189,10 @@ func (k Keeper) Execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller
gas := gasForContract(ctx)
res, execErr := k.wasmer.Execute(codeInfo.CodeHash, params, msg, prefixStore, cosmwasmAPI, querier, gas)
if execErr != nil {
// TODO: wasmer doesn't return gas used on error. we should consume it (for error on metering failure)
// TODO: wasmer doesn't return wasm gas used on error. we should consume it (for error on metering failure)
// Note: OutOfGas panics (from storage) are caught by go-cosmwasm, subtract one more gas to check if
// this contract died due to gas limit in Storage
consumeGas(ctx, GasMultiplier)
return sdk.Result{}, sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
}
consumeGas(ctx, res.GasUsed)

View File

@ -382,7 +382,7 @@ func TestExecuteWithCpuLoop(t *testing.T) {
require.Equal(t, uint64(0), ctx.GasMeter().GasConsumed())
// this must fail
_, err = keeper.Execute(ctx, addr, fred, []byte(`{"cpuloop":{}}`), nil)
_, err = keeper.Execute(ctx, addr, fred, []byte(`{"cpu_loop":{}}`), nil)
assert.Error(t, err)
// make sure gas ran out
// TODO: wasmer doesn't return gas used on error. we should consume it (for error on metering failure)
@ -390,8 +390,6 @@ func TestExecuteWithCpuLoop(t *testing.T) {
}
func TestExecuteWithStorageLoop(t *testing.T) {
// TODO
t.Skip("out of gas error not thrown")
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
@ -434,7 +432,7 @@ func TestExecuteWithStorageLoop(t *testing.T) {
}()
// this should throw out of gas exception (panic)
_, _ = keeper.Execute(ctx, addr, fred, []byte(`{"storageloop":{}}`), nil)
_, err = keeper.Execute(ctx, addr, fred, []byte(`{"storage_loop":{}}`), nil)
require.True(t, false, "We must panic before this line")
}