Skip to content

Commit

Permalink
fix test, add some more gas due to check gasless
Browse files Browse the repository at this point in the history
  • Loading branch information
tubackkhoa committed Jul 29, 2024
1 parent c694f2f commit 7b361ce
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 68 deletions.
11 changes: 5 additions & 6 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
110 changes: 53 additions & 57 deletions x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions x/wasm/keeper/submsg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -263,23 +263,23 @@ 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,
msg: invalidBankSend,
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,
msg: infiniteLoop,
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,
Expand Down

0 comments on commit 7b361ce

Please sign in to comment.