-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make apply func if no err, out of gas panic, match std behavior (#3746)
* Remove logging for apply func if no err, out of gas * Add test case * Add changelog * Actually Add changelog
- Loading branch information
1 parent
3ae728c
commit 62757d3
Showing
3 changed files
with
85 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package osmoutils_test | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/osmosis-labs/osmosis/v13/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(sdk.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()) | ||
}) | ||
} | ||
} |