Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Prevent panic when external contract query executed #432

Merged
merged 3 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions x/wasm/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,26 @@ func queryRawStore(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byt
return res, nil
}

func queryContractStore(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, error) {
func queryContractStore(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) (bz []byte, err error) {
// external query gas limit must be specified here
ctx = ctx.WithGasMeter(sdk.NewGasMeter(keeper.wasmConfig.ContractQueryGasLimit))

var params types.QueryContractParams
err := types.ModuleCdc.UnmarshalJSON(req.Data, &params)
err = types.ModuleCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

return keeper.queryToContract(ctx, params.ContractAddress, params.Msg)
// recover from out-of-gas panic
defer func() {
if r := recover(); r != nil {
err = sdkerrors.ErrOutOfGas
}
}()

bz, err = keeper.queryToContract(ctx, params.ContractAddress, params.Msg)

return
}

func queryParameters(ctx sdk.Context, keeper Keeper) ([]byte, error) {
Expand Down
9 changes: 4 additions & 5 deletions x/wasm/internal/keeper/recursive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerror "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/terra-project/core/x/wasm/internal/types"
Expand Down Expand Up @@ -251,11 +252,9 @@ func TestGasOnExternalQuery(t *testing.T) {
require.NoError(t, err)

if tc.expectPanic {
require.Panics(t, func() {
// this should run out of gas
_, err = querier(ctx, []string{types.QueryContractStore}, abci.RequestQuery{Data: []byte(bz)})
t.Logf("%v", err)
})
_, err = querier(ctx, []string{types.QueryContractStore}, abci.RequestQuery{Data: []byte(bz)})
require.Error(t, err)
require.Equal(t, err, sdkerror.ErrOutOfGas)
} else {
// otherwise, make sure we get a good success
_, err = querier(ctx, []string{types.QueryContractStore}, abci.RequestQuery{Data: []byte(bz)})
Expand Down