-
Notifications
You must be signed in to change notification settings - Fork 193
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
refactor(perp): Remove unnecessary panics | #1 #1126
Conversation
GetWhitelistedPairs
… fo CombineErrors
6771802
to
5bed36e
Compare
// Cases for duck typing at runtime | ||
|
||
// case: error | ||
if tcErr := TryCatch(func() { |
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.
Doesn't the type switch statement catch all these cases already?
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.
That's how I thought it would work at first too. The reason it skips those blocks is that any
and interface{}
are actual types. They don't actual mean "any type" but rather "an object of type any
".
Let's say we pass in an argument, arg
, that fits the fmt.Stringer
interface like sdk.Coin
or sdk.AccAddress
. When arg
enters this function, it won't actually have the type of fmt.Stringer
, so it will pass all of the case statements and end up in default.
Then, when it reaches the arg := arg.(fmt.Stringer)
block, it will enter the function with fmt.Stringer
as its value for arg.(type)
. Since arg
actually is an instance of that fits this interface, the TryCatch
will run smoothly.
However, it has to guess by manually attempting to cast as each type. Go assumes the type casting will work and forces the object to have a certain type, then it panics if something doesn't make sense, then defers, then recovers, and finally moves to the next TryCatch
.
This basically shows why Python is so slow.
Co-authored-by: NibiruHeisenberg <[email protected]>
* chore: re-run linter * test(oracle): stop the tyrannical behavior of tally_fuzz_test.go * test(oracle): try assert.NotPanics for keys of length 0 * test(oracle): try assert.NotPanics for keys of length 0 * feat,test(common): Create StringSet for easy set management * test(oracle): Increase coverage and end the collections panics in GetWhitelistedPairs * fix(oracle): small bug in TestFuzz_PickReferencePair * test,feat(common): address PR feedback and add a more generic version fo CombineErrors * linter * feat(oracle): add twap to oracle and unwire pricefeed (#1120) * unwire pricefeed and add twap * refactor(perp): Remove unnecessary panics | #1 (#1126) * remove unnecessary panics in x/perp * change log * chore: re-run linter * test(oracle): stop the tyrannical behavior of tally_fuzz_test.go * test(oracle): try assert.NotPanics for keys of length 0 * feat,test(common): Create StringSet for easy set management * test(oracle): Increase coverage and end the collections panics in GetWhitelistedPairs * fix(oracle): small bug in TestFuzz_PickReferencePair * test,feat(common): address PR feedback and add a more generic version fo CombineErrors * linter * Update x/common/error_test.go Co-authored-by: NibiruHeisenberg <[email protected]> Co-authored-by: NibiruHeisenberg <[email protected]> * fix: distribution module account name (#1131) * refactor(dex): remove unnecessary panics * refactor(stablecoin): remove unnecessary panics Co-authored-by: Matthias <[email protected]> Co-authored-by: NibiruHeisenberg <[email protected]>
* remove unnecessary panics in x/perp * refactor(dex): remove panics in liquidity.go * change log * chore: change log * refactor: remove panics from x/dex (#1133) * chore: re-run linter * test(oracle): stop the tyrannical behavior of tally_fuzz_test.go * test(oracle): try assert.NotPanics for keys of length 0 * test(oracle): try assert.NotPanics for keys of length 0 * feat,test(common): Create StringSet for easy set management * test(oracle): Increase coverage and end the collections panics in GetWhitelistedPairs * fix(oracle): small bug in TestFuzz_PickReferencePair * test,feat(common): address PR feedback and add a more generic version fo CombineErrors * linter * feat(oracle): add twap to oracle and unwire pricefeed (#1120) * unwire pricefeed and add twap * refactor(perp): Remove unnecessary panics | #1 (#1126) * remove unnecessary panics in x/perp * change log * chore: re-run linter * test(oracle): stop the tyrannical behavior of tally_fuzz_test.go * test(oracle): try assert.NotPanics for keys of length 0 * feat,test(common): Create StringSet for easy set management * test(oracle): Increase coverage and end the collections panics in GetWhitelistedPairs * fix(oracle): small bug in TestFuzz_PickReferencePair * test,feat(common): address PR feedback and add a more generic version fo CombineErrors * linter * Update x/common/error_test.go Co-authored-by: NibiruHeisenberg <[email protected]> Co-authored-by: NibiruHeisenberg <[email protected]> * fix: distribution module account name (#1131) * refactor(dex): remove unnecessary panics * refactor(stablecoin): remove unnecessary panics Co-authored-by: Matthias <[email protected]> Co-authored-by: NibiruHeisenberg <[email protected]> Co-authored-by: Matthias <[email protected]> Co-authored-by: NibiruHeisenberg <[email protected]>
…sible form public fns (#1134) * remove unnecessary panics in x/perp * refactor(dex): remove panics in liquidity.go * change log * chore: change log * chore: re-run linter * test(oracle): stop the tyrannical behavior of tally_fuzz_test.go * test(oracle): try assert.NotPanics for keys of length 0 * test(oracle): try assert.NotPanics for keys of length 0 * feat,test(common): Create StringSet for easy set management * test(oracle): Increase coverage and end the collections panics in GetWhitelistedPairs * fix(oracle): small bug in TestFuzz_PickReferencePair * test,feat(common): address PR feedback and add a more generic version fo CombineErrors * linter * refactor(dex): remove unnecessary panics * refactor(stablecoin): remove unnecessary panics * refactor(perp): make calls of public fns from vpool less dangerous in x/perp * checkpoint #wip vpool * fix changelog issue from merge conflict * refactor: remove panics from x/dex (#1133) * chore: re-run linter * test(oracle): stop the tyrannical behavior of tally_fuzz_test.go * test(oracle): try assert.NotPanics for keys of length 0 * test(oracle): try assert.NotPanics for keys of length 0 * feat,test(common): Create StringSet for easy set management * test(oracle): Increase coverage and end the collections panics in GetWhitelistedPairs * fix(oracle): small bug in TestFuzz_PickReferencePair * test,feat(common): address PR feedback and add a more generic version fo CombineErrors * linter * feat(oracle): add twap to oracle and unwire pricefeed (#1120) * unwire pricefeed and add twap * refactor(perp): Remove unnecessary panics | #1 (#1126) * remove unnecessary panics in x/perp * change log * chore: re-run linter * test(oracle): stop the tyrannical behavior of tally_fuzz_test.go * test(oracle): try assert.NotPanics for keys of length 0 * feat,test(common): Create StringSet for easy set management * test(oracle): Increase coverage and end the collections panics in GetWhitelistedPairs * fix(oracle): small bug in TestFuzz_PickReferencePair * test,feat(common): address PR feedback and add a more generic version fo CombineErrors * linter * Update x/common/error_test.go Co-authored-by: NibiruHeisenberg <[email protected]> Co-authored-by: NibiruHeisenberg <[email protected]> * fix: distribution module account name (#1131) * refactor(dex): remove unnecessary panics * refactor(stablecoin): remove unnecessary panics Co-authored-by: Matthias <[email protected]> Co-authored-by: NibiruHeisenberg <[email protected]> * test,refactor(vpool): make it impossible to write invalid pools into state. * refactor: Make it impossible to cause panics in vpool from public fns in the perp module * chore(CHANGELOG) Co-authored-by: Matthias <[email protected]> Co-authored-by: NibiruHeisenberg <[email protected]> Co-authored-by: Kevin Yang <[email protected]>
Description
Remove unnecessary panics in x/perp
TestFuzz_PickReferencePair
stop harassing usCombineErrors
andCombineErrorsGeneric
. The differenceerror
type if they are (1) are strings, (2) fit thefmt.Stringer
interface and have.String()
methods, or (3) are already errors.StringSet
Adds
TryCatch
which is used in later PRs for #1125TryCatch
is an implementation of the try-catch block from languages like C++ and JS.Given a 'callback' function,
TryCatch
defers and recovers from any panics orerrors, allowing one to safely handle multiple panics in succession.
Typically, you'll write something like:
err := TryCatch(aRiskyFunction)()
Usage example:
Note that
TryCatch
doesn't return an error. It returns a function that returnsan error. Only when you call the output of TryCatch will it "feel" like a
try-catch block from other languages.
This means that
TryCatch
can be used to restart go routines that panic as well.Debugging notes on
TestFuzz_ReferencePair
Solution (found this after some tinkering)
The familiar error (cases A and B) comes from iterating over a
collections.Range[string]
that includes the blank key string,""
(hexadecimal 00). SeeTestKeeper_GetVoteTargets
, which now covers this case in its testsCase A - Use whitelisted pair with string length 1
Attempting to
Pair.Insert
a key of length 0 seems valid according to this workflow run.Case B - Use whitelisted pair with string length 0
In this call of
stringKey.Decode
that results fromRemoveInvalidBallots
, we can get a different error in another workflow:Full error stack: