-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bugfix] limit wasm max gas with param (#502)
* limit wasm gas usage with param * update comment * use explicit subCtx
- Loading branch information
yys
authored
Jun 28, 2021
1 parent
505a4c8
commit c167a9d
Showing
8 changed files
with
180 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package keeper | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
core "github.com/terra-money/core/types" | ||
"github.com/terra-money/core/x/wasm/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
func TestInstantiateExceedMaxGas(t *testing.T) { | ||
input := CreateTestInput(t) | ||
ctx, accKeeper, bankKeeper, keeper := input.Ctx, input.AccKeeper, input.BankKeeper, input.WasmKeeper | ||
|
||
deposit := sdk.NewCoins(sdk.NewInt64Coin(core.MicroLunaDenom, 100000)) | ||
creator := createFakeFundedAccount(ctx, accKeeper, bankKeeper, deposit) | ||
|
||
wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm") | ||
require.NoError(t, err) | ||
|
||
codeID, err := keeper.StoreCode(ctx, creator, wasmCode) | ||
require.NoError(t, err) | ||
|
||
_, _, bob := keyPubAddr() | ||
_, _, fred := keyPubAddr() | ||
|
||
initMsg := HackatomExampleInitMsg{ | ||
Verifier: fred, | ||
Beneficiary: bob, | ||
} | ||
|
||
initMsgBz, err := json.Marshal(initMsg) | ||
require.NoError(t, err) | ||
|
||
// must panic | ||
require.Panics(t, func() { | ||
params := keeper.GetParams(ctx) | ||
params.MaxContractGas = types.InstanceCost + 1 | ||
keeper.SetParams(ctx, params) | ||
NewMsgServerImpl(keeper).InstantiateContract(ctx.Context(), types.NewMsgInstantiateContract(creator, sdk.AccAddress{}, codeID, initMsgBz, nil)) | ||
}) | ||
} | ||
|
||
func TestExecuteExceedMaxGas(t *testing.T) { | ||
input := CreateTestInput(t) | ||
ctx, accKeeper, bankKeeper, keeper := input.Ctx, input.AccKeeper, input.BankKeeper, input.WasmKeeper | ||
|
||
deposit := sdk.NewCoins(sdk.NewInt64Coin(core.MicroLunaDenom, 100000)) | ||
creator := createFakeFundedAccount(ctx, accKeeper, bankKeeper, deposit) | ||
|
||
wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm") | ||
require.NoError(t, err) | ||
|
||
codeID, err := keeper.StoreCode(ctx, creator, wasmCode) | ||
require.NoError(t, err) | ||
|
||
_, _, bob := keyPubAddr() | ||
_, _, fred := keyPubAddr() | ||
|
||
initMsg := HackatomExampleInitMsg{ | ||
Verifier: fred, | ||
Beneficiary: bob, | ||
} | ||
|
||
initMsgBz, err := json.Marshal(initMsg) | ||
require.NoError(t, err) | ||
|
||
addr, _, err := keeper.InstantiateContract(ctx, codeID, creator, sdk.AccAddress{}, initMsgBz, nil) | ||
|
||
// must panic | ||
require.Panics(t, func() { | ||
params := keeper.GetParams(ctx) | ||
params.MaxContractGas = types.InstanceCost + 1 | ||
keeper.SetParams(ctx, params) | ||
NewMsgServerImpl(keeper).ExecuteContract(ctx.Context(), types.NewMsgExecuteContract(creator, addr, []byte(`{"release":{}}`), nil)) | ||
}) | ||
} | ||
|
||
func TestMigrateExceedMaxGas(t *testing.T) { | ||
input := CreateTestInput(t) | ||
ctx, accKeeper, bankKeeper, keeper := input.Ctx, input.AccKeeper, input.BankKeeper, input.WasmKeeper | ||
|
||
deposit := sdk.NewCoins(sdk.NewInt64Coin(core.MicroLunaDenom, 100000)) | ||
creator := createFakeFundedAccount(ctx, accKeeper, bankKeeper, deposit) | ||
|
||
wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm") | ||
require.NoError(t, err) | ||
|
||
codeID, err := keeper.StoreCode(ctx, creator, wasmCode) | ||
require.NoError(t, err) | ||
|
||
_, _, bob := keyPubAddr() | ||
_, _, fred := keyPubAddr() | ||
|
||
initMsg := HackatomExampleInitMsg{ | ||
Verifier: fred, | ||
Beneficiary: bob, | ||
} | ||
|
||
initMsgBz, err := json.Marshal(initMsg) | ||
require.NoError(t, err) | ||
|
||
addr, _, err := keeper.InstantiateContract(ctx, codeID, creator, sdk.AccAddress{}, initMsgBz, nil) | ||
|
||
// must panic | ||
require.Panics(t, func() { | ||
params := keeper.GetParams(ctx) | ||
params.MaxContractGas = types.InstanceCost + 1 | ||
keeper.SetParams(ctx, params) | ||
NewMsgServerImpl(keeper).MigrateContract(ctx.Context(), types.NewMsgMigrateContract(creator, addr, codeID, []byte(`{"release":{}}`))) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.