From 7b361ce942ddd7bf36a6368c8d53e4917415b821 Mon Sep 17 00:00:00 2001 From: Pham Tu Date: Mon, 29 Jul 2024 18:25:08 +0700 Subject: [PATCH] fix test, add some more gas due to check gasless --- x/wasm/keeper/keeper.go | 11 ++-- x/wasm/keeper/keeper_test.go | 110 +++++++++++++++++------------------ x/wasm/keeper/submsg_test.go | 10 ++-- 3 files changed, 63 insertions(+), 68 deletions(-) diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index a2a34e0465..816aa56ee7 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -389,7 +389,11 @@ func (k Keeper) instantiate( func (k Keeper) execute(ctx context.Context, contractAddress, caller sdk.AccAddress, msg []byte, coins sdk.Coins) ([]byte, error) { defer telemetry.MeasureSince(time.Now(), "wasm", "contract", "execute") sdkCtx := sdk.UnwrapSDKContext(ctx) - gasConsumed := sdkCtx.GasMeter().GasConsumed() + isGasLess := k.IsGasless(sdkCtx, contractAddress) + // infinite gas meter + if isGasLess { + sdkCtx = sdkCtx.WithGasMeter(storetypes.NewInfiniteGasMeter()) + } contractInfo, codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddress) if err != nil { return nil, err @@ -434,11 +438,6 @@ func (k Keeper) execute(ctx context.Context, contractAddress, caller sdk.AccAddr return nil, errorsmod.Wrap(err, "dispatch") } - // refund gas if gasless - if k.IsGasless(sdkCtx, contractAddress) { - sdkCtx.GasMeter().RefundGas(sdkCtx.GasMeter().GasConsumed()-gasConsumed, "Gasless Contract") - } - return data, nil } diff --git a/x/wasm/keeper/keeper_test.go b/x/wasm/keeper/keeper_test.go index 78aade5b12..f0cfd81fbd 100644 --- a/x/wasm/keeper/keeper_test.go +++ b/x/wasm/keeper/keeper_test.go @@ -959,7 +959,7 @@ func TestExecute(t *testing.T) { // make sure gas is properly deducted from ctx gasAfter := ctx.GasMeter().GasConsumed() if types.EnableGasVerification { - require.Equal(t, uint64(0x1ac76), gasAfter-gasBefore) + require.Equal(t, uint64(0x1b05e), gasAfter-gasBefore) } // ensure bob now exists and got both payments released bobAcct = accKeeper.GetAccount(ctx, bob) @@ -1939,62 +1939,6 @@ func TestUnpinCode(t *testing.T) { assert.Equal(t, exp, em.Events()) } -func TestSetGaslessContract(t *testing.T) { - - mock := wasmtesting.MockWasmEngine{ExecuteFn: func(codeID wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, executeMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.ContractResult, uint64, error) { - return &wasmvmtypes.ContractResult{ - Ok: &wasmvmtypes.Response{ - Attributes: []wasmvmtypes.EventAttribute{{Key: "myKey", Value: "myVal"}}, - }, - }, 0, nil - }} - - ctx, keepers := CreateTestInput(t, false, AvailableCapabilities, WithWasmEngine(&mock)) - k := keepers.WasmKeeper - wasmtesting.MakeInstantiable(&mock) - example := SeedNewContractInstance(t, ctx, keepers, &mock) - - ctx = ctx.WithGasMeter(storetypes.NewGasMeter(20_000_000)) - _, err := k.execute(ctx, example.Contract, RandomAccountAddress(t), []byte(`{}`), nil) - require.NoError(t, err) - - // when - gotErr := k.setGasless(ctx, example.Contract) - - // then - require.NoError(t, gotErr) - assert.True(t, k.IsGasless(ctx, example.Contract)) - - gasConsumed := ctx.GasMeter().GasConsumed() - t.Logf("gas consumed %v", gasConsumed) - - _, err = k.execute(ctx, example.Contract, RandomAccountAddress(t), []byte(`{}`), nil) - require.NoError(t, err) - gasUsed := ctx.GasMeter().GasConsumed() - gasConsumed - // Should be 0 - assert.True(t, gasUsed == 0) -} - -func TestUnsetGaslessContract(t *testing.T) { - ctx, keepers := CreateTestInput(t, false, AvailableCapabilities) - k := keepers.WasmKeeper - mock := wasmtesting.MockWasmEngine{} - wasmtesting.MakeInstantiable(&mock) - example := SeedNewContractInstance(t, ctx, keepers, &mock) - - // when - gotErr := k.setGasless(ctx, example.Contract) - - // then - require.NoError(t, gotErr) - - gotErr = k.unsetGasless(ctx, example.Contract) - - // then - require.NoError(t, gotErr) - assert.False(t, k.IsGasless(ctx, example.Contract)) -} - func TestInitializePinnedCodes(t *testing.T) { ctx, keepers := CreateTestInput(t, false, AvailableCapabilities) k := keepers.WasmKeeper @@ -2760,6 +2704,58 @@ func TestSetContractLabel(t *testing.T) { } } +func TestSetGaslessContract(t *testing.T) { + + mock := wasmtesting.MockWasmEngine{ExecuteFn: func(codeID wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, executeMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.ContractResult, uint64, error) { + return &wasmvmtypes.ContractResult{ + Ok: &wasmvmtypes.Response{ + Attributes: []wasmvmtypes.EventAttribute{{Key: "myKey", Value: "myVal"}}, + }, + }, 0, nil + }} + + ctx, keepers := CreateTestInput(t, false, AvailableCapabilities, WithWasmEngine(&mock)) + k := keepers.WasmKeeper + wasmtesting.MakeInstantiable(&mock) + example := SeedNewContractInstance(t, ctx, keepers, &mock) + + ctx = ctx.WithGasMeter(storetypes.NewGasMeter(20_000_000)) + _, err := k.execute(ctx, example.Contract, RandomAccountAddress(t), []byte(`{}`), nil) + require.NoError(t, err) + + // when + gotErr := k.setGasless(ctx, example.Contract) + ctx = ctx.WithGasMeter(storetypes.NewGasMeter(20_000)) + + // then + require.NoError(t, gotErr) + assert.True(t, k.IsGasless(ctx, example.Contract)) + + _, err = k.execute(ctx, example.Contract, RandomAccountAddress(t), []byte(`{}`), nil) + require.NoError(t, err) + assert.True(t, ctx.GasMeter().GasConsumed() == 5687) +} + +func TestUnsetGaslessContract(t *testing.T) { + ctx, keepers := CreateTestInput(t, false, AvailableCapabilities) + k := keepers.WasmKeeper + mock := wasmtesting.MockWasmEngine{} + wasmtesting.MakeInstantiable(&mock) + example := SeedNewContractInstance(t, ctx, keepers, &mock) + + // when + gotErr := k.setGasless(ctx, example.Contract) + + // then + require.NoError(t, gotErr) + + gotErr = k.unsetGasless(ctx, example.Contract) + + // then + require.NoError(t, gotErr) + assert.False(t, k.IsGasless(ctx, example.Contract)) +} + func attrsToStringMap(attrs []abci.EventAttribute) map[string]string { r := make(map[string]string, len(attrs)) for _, v := range attrs { diff --git a/x/wasm/keeper/submsg_test.go b/x/wasm/keeper/submsg_test.go index 971f0d9648..53f24a0c90 100644 --- a/x/wasm/keeper/submsg_test.go +++ b/x/wasm/keeper/submsg_test.go @@ -243,14 +243,14 @@ func TestDispatchSubMsgErrorHandling(t *testing.T) { "send tokens": { submsgID: 5, msg: validBankSend, - resultAssertions: []assertion{assertReturnedEvents(0), assertGasUsed(110_000, 111_000)}, + resultAssertions: []assertion{assertReturnedEvents(0), assertGasUsed(110_000, 121_000)}, }, "not enough tokens": { submsgID: 6, msg: invalidBankSend, subMsgError: true, // uses less gas than the send tokens (cost of bank transfer) - resultAssertions: []assertion{assertGasUsed(78_000, 81_000), assertErrorString("codespace: sdk, code: 5")}, + resultAssertions: []assertion{assertGasUsed(78_000, 91_000), assertErrorString("codespace: sdk, code: 5")}, }, "out of gas panic with no gas limit": { submsgID: 7, @@ -263,7 +263,7 @@ func TestDispatchSubMsgErrorHandling(t *testing.T) { msg: validBankSend, gasLimit: &subGasLimit, // uses same gas as call without limit (note we do not charge the 40k on reply) - resultAssertions: []assertion{assertReturnedEvents(0), assertGasUsed(110_000, 112_000)}, + resultAssertions: []assertion{assertReturnedEvents(0), assertGasUsed(110_000, 122_000)}, }, "not enough tokens with limit": { submsgID: 16, @@ -271,7 +271,7 @@ func TestDispatchSubMsgErrorHandling(t *testing.T) { subMsgError: true, gasLimit: &subGasLimit, // uses same gas as call without limit (note we do not charge the 40k on reply) - resultAssertions: []assertion{assertGasUsed(78_000, 81_000), assertErrorString("codespace: sdk, code: 5")}, + resultAssertions: []assertion{assertGasUsed(78_000, 91_000), assertErrorString("codespace: sdk, code: 5")}, }, "out of gas caught with gas limit": { submsgID: 17, @@ -279,7 +279,7 @@ func TestDispatchSubMsgErrorHandling(t *testing.T) { subMsgError: true, gasLimit: &subGasLimit, // uses all the subGasLimit, plus the 52k or so for the main contract - resultAssertions: []assertion{assertGasUsed(subGasLimit+75_000, subGasLimit+77_000), assertErrorString("codespace: sdk, code: 11")}, + resultAssertions: []assertion{assertGasUsed(subGasLimit+75_000, subGasLimit+87_000), assertErrorString("codespace: sdk, code: 11")}, }, "instantiate contract gets address in data and events": { submsgID: 21,