forked from osmosis-labs/osmosis
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache_ctx_test.go
61 lines (55 loc) · 1.41 KB
/
cache_ctx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package osmoutils_test
import (
"cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
storetypes "cosmossdk.io/store/types"
"github.com/osmosis-labs/osmosis/osmoutils"
)
var expectedOutOfGasError = types.ErrorOutOfGas{Descriptor: "my func"}
func consumeGas(ctx sdk.Context, gas uint64, numTimes int) error {
for i := 0; i < numTimes; i++ {
ctx.GasMeter().ConsumeGas(gas, "my func")
}
return nil
}
func (s *TestSuite) TestCacheCtxConsumeGas() {
// every test case adds 1k gas 10 times
testcases := map[string]struct {
gasLimit uint64
gasUsedPreCtx uint64
gasUsedPostCtx uint64
expectPanic bool
}{
"no gas limit hit": {
gasLimit: 15_000,
gasUsedPreCtx: 111,
gasUsedPostCtx: 111 + 10_000,
expectPanic: false,
},
"gas limit hit": {
gasLimit: 10_000,
gasUsedPreCtx: 111,
gasUsedPostCtx: 111 + 10_000,
expectPanic: true,
},
}
for name, tc := range testcases {
s.Run(name, func() {
ctx := s.ctx.WithGasMeter(storetypes.NewGasMeter(tc.gasLimit))
ctx.GasMeter().ConsumeGas(tc.gasUsedPreCtx, "pre ctx")
var err error
f := func() {
osmoutils.ApplyFuncIfNoError(ctx, func(c sdk.Context) error {
return consumeGas(c, 1000, 10)
})
}
if tc.expectPanic {
s.PanicsWithValue(expectedOutOfGasError, f)
} else {
f()
s.Require().NoError(err)
}
s.Equal(tc.gasUsedPostCtx, ctx.GasMeter().GasConsumed())
})
}
}