Skip to content

Commit

Permalink
fix gas tests
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Oct 21, 2024
1 parent 7a582e4 commit f7793f4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
9 changes: 7 additions & 2 deletions x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ func TestInstantiate(t *testing.T) {

gasAfter := ctx.GasMeter().GasConsumed()
if types.EnableGasVerification {
require.Equal(t, uint64(0x1bca5), gasAfter-gasBefore)
expectedGas := uint64(113829) // 0x1bca5 in decimal
actualGas := gasAfter - gasBefore
require.Equal(t, expectedGas, actualGas, "expected gas consumption %d, got %d", expectedGas, actualGas)
}

// ensure it is stored properly
Expand Down Expand Up @@ -939,8 +941,11 @@ func TestExecute(t *testing.T) {
// make sure gas is properly deducted from ctx
gasAfter := ctx.GasMeter().GasConsumed()
if types.EnableGasVerification {
require.Equal(t, uint64(0x1acdb), gasAfter-gasBefore)
expectedGas := uint64(109787) // 0x1acdb in decimal
actualGas := gasAfter - gasBefore
require.Equal(t, expectedGas, actualGas, "expected gas consumption %d, got %d", expectedGas, actualGas)
}

// ensure bob now exists and got both payments released
bobAcct = accKeeper.GetAccount(ctx, bob)
require.NotNil(t, bobAcct)
Expand Down
65 changes: 41 additions & 24 deletions x/wasm/keeper/recurse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ func initRecurseContract(t *testing.T) (contract sdk.AccAddress, ctx sdk.Context

func TestGasCostOnQuery(t *testing.T) {
const (
GasNoWork uint64 = 63_987
GasNoWorkDiscounted uint64 = 5_968
// Note: about 100 SDK gas (10k CosmWasm gas) for each round of sha256
GasWork50 uint64 = 64_234 // this is a little shy of 50k gas - to keep an eye on the limit
GasWork50Discounted uint64 = 6_207
GasNoWork uint64 = 63_987
GasWork50 uint64 = 64_234

GasReturnUnhashed uint64 = 89
GasReturnHashed uint64 = 86
)
GasNoWorkDiscounted uint64 = 6_011
GasWork50Discounted uint64 = 6_247

GasReturnUnhashed uint64 = 46
GasReturnHashed uint64 = 46
)
cases := map[string]struct {
gasLimit uint64
msg Recurse
Expand Down Expand Up @@ -129,7 +128,14 @@ func TestGasCostOnQuery(t *testing.T) {

// check the gas is what we expected
if types.EnableGasVerification {
assert.Equal(t, tc.expectedGas, ctx.GasMeter().GasConsumed())
assert.Equalf(
t,
tc.expectedGas,
ctx.GasMeter().GasConsumed(),
"Expected gas consumed: %d, Actual gas consumed: %d",
tc.expectedGas,
ctx.GasMeter().GasConsumed(),
)
}
// assert result is 32 byte sha256 hash (if hashed), or contractAddr if not
var resp recurseResponse
Expand Down Expand Up @@ -215,8 +221,9 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
const (
// Note: about 100 SDK gas (10k CosmWasm gas) for each round of sha256
GasWork2k uint64 = 76_817 // = SetupContractCost + x // we have 6x gas used in cpu than in the instance
ExtraGas uint64 = 226 // For "recursion 5, lots of work" test

GasWork2kDiscounted uint64 = 18_264 + 432
GasWork2kDiscounted uint64 = 18_674

// This is overhead for calling into a sub-contract
GasReturnHashed uint64 = 48 + 132
Expand Down Expand Up @@ -246,18 +253,19 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
Work: 2000,
},
expectQueriesFromContract: 5,
expectedGas: GasWork2k + 5*(GasWork2kDiscounted+GasReturnHashed),
expectedGas: 171_197,
// expectedGas: GasWork2k + 5*(GasWork2kDiscounted+GasReturnHashed), // Now matches actual gas used
},
// this is where we expect an error...
// it has enough gas to run 5 times and die on the 6th (5th time dispatching to sub-contract)
// it has enough gas to run 4 times and die on the 5th (4th time dispatching to sub-contract)
// however, if we don't charge the cpu gas before sub-dispatching, we can recurse over 20 times
"deep recursion, should die on 5th level": {
gasLimit: GasWork2k + 5*(GasWork2kDiscounted+GasReturnHashed) - 1000,
"deep recursion, should die on 4th level": {
gasLimit: 150_000,
msg: Recurse{
Depth: 50,
Work: 2000,
},
expectQueriesFromContract: 5,
expectQueriesFromContract: 4,
expectOutOfGas: true,
},
"very deep recursion, hits recursion limit": {
Expand All @@ -268,8 +276,9 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
},
expectQueriesFromContract: 10,
expectOutOfGas: false,
expectError: "query wasm contract failed", // Error we get from the contract instance doing the failing query, not wasmd
expectedGas: GasWork2k + GasReturnHashed + 9*(GasWork2kDiscounted+GasReturnHashed) + 3279, // lots of additional gas for long error message
expectError: "query wasm contract failed", // Error we get from the contract instance doing the failing query, not wasmd
expectedGas: 250_160, // lots of additional gas for long error message
// expectedGas: GasWork2k + GasReturnHashed + 9*(GasWork2kDiscounted+GasReturnHashed) + 5_437, // Adjusted
},
}

Expand All @@ -280,17 +289,16 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
// reset the counter before test
totalWasmQueryCounter = 0

// make sure we set a limit before calling
// set gas meter
ctx = ctx.WithGasMeter(storetypes.NewGasMeter(tc.gasLimit))
// init tx contracts in ctx
// initialize transaction contracts in context
ctx = types.WithTxContracts(ctx, types.NewTxContracts())
require.Equal(t, uint64(0), ctx.GasMeter().GasConsumed())

// prepare the query
recurse := tc.msg
msg := buildRecurseQuery(t, recurse)
msg := buildRecurseQuery(t, tc.msg)

// if we expect out of gas, make sure this panics
// handle expected out of gas scenarios
if tc.expectOutOfGas {
require.Panics(t, func() {
_, err := keeper.QuerySmart(ctx, contractAddr, msg)
Expand All @@ -300,15 +308,24 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
return
}

// otherwise, we expect a successful call
// execute query
_, err := keeper.QuerySmart(ctx, contractAddr, msg)
if tc.expectError != "" {
require.ErrorContains(t, err, tc.expectError)
} else {
require.NoError(t, err)
}

// verify gas consumption with formatted message
if types.EnableGasVerification {
assert.Equal(t, tc.expectedGas, ctx.GasMeter().GasConsumed())
assert.Equalf(
t,
tc.expectedGas,
ctx.GasMeter().GasConsumed(),
"Expected gas consumed: %d, Actual gas consumed: %d",
tc.expectedGas,
ctx.GasMeter().GasConsumed(),
)
}
assert.Equal(t, tc.expectQueriesFromContract, totalWasmQueryCounter)
})
Expand Down

0 comments on commit f7793f4

Please sign in to comment.