-
Notifications
You must be signed in to change notification settings - Fork 608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make apply func if no err, out of gas panic, match std behavior #3746
Merged
ValarDragon
merged 5 commits into
main
from
dev/improve_applyfuncifnoerr_out_of_gas_logging
Feb 1, 2023
+85
−3
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7071a99
Remove logging for apply func if no err, out of gas
ValarDragon ff63435
Add test case
ValarDragon 0cf2b8b
Add changelog
ValarDragon c891ba2
Actually Add changelog
ValarDragon d1e825b
Merge branch 'main' into dev/improve_applyfuncifnoerr_out_of_gas_logging
ValarDragon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about types.ErrorNegativeGasConsumed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh wow didn't know that existed, let me think about it
great catch