-
Notifications
You must be signed in to change notification settings - Fork 0
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
update calc_open_short
to match solidity
#116
Conversation
f046c80
to
0972415
Compare
7fa84a4
to
760d443
Compare
0c23fd0
to
2ff193d
Compare
The Looking at the current failure & the one reported in #121 I'm thinking the amount it is off is quite large -- not just a matter of bumping some error tolerance. update: it looks like the logic changed in calc_max_short quite some time ago (#537) to something we don't actually want, where it is returning a valid bond amount even if update 2: After quite a bit of digging I was able to get the test to pass consistently after increasing the number of iterations. I think it just doesn't reach the solution in 7 iterations some of the time. |
# Resolved Issues Partially addresses #121 & #136 # Description These changes arose in my other pr (#116) while trying to debug issues and high tolerances in `short::max`. There are a lot of commits in order to separate out meaningful changes from trivial ones. A majority of the changes are code reorganization, docstring fixes, and some additional sanity checks. Major changes were: - `fuzz_calculate_max_short` - was renamed to more accurately reflect what it is testing -- max short in the budget constrained regime - was rewritten to ensure the budget is constrained. I believe the reason the failures were "intermittent" before was because it was not always hitting the failure case. It hits it a lot now. - the tolerance was updated so that it consistently passes, to give us an idea of how much it is off from what we want. - `calculate_max_short` this code was incorrect: ``` let absolute_max_deposit = match self.calculate_open_short(max_bond_amount, open_vault_share_price) { Ok(d) => d, Err(_) => return Ok(max_bond_amount), ``` we do **not** want to return the bond amount if `calculate_open_short` is throwing an error. I updated it. I ran the tests locally with `FUZZ_RUNS=1_000` and `FAST_FUZZ_RUNS=50_000` without any tests failing. --------- Co-authored-by: Alex Towle <jalextowle@gmail.com>
036fba9
to
cb7290a
Compare
110e3a3
to
3bcdd03
Compare
# Resolved Issues Working towards #29 # Description This pulls a bunch of cleanup changes out of #116 to be reviewed independently. - fix a bug introduced in #142 where test tolerances were high because the variable rate was causing short amounts to change between the rust & sol calls. - improve docstrings & error messaging throughout - modify `solvency_after_short_derivative` to return `Result<T>` instead of `Result<Option<T>>` - modify max behavior to throw errors if there is no valid max, instead of returning `0` - add safety bounds on max short guesses to ensure it is always >= the min txn amount - modify sol differential max short tests to use `calculate_absolute_max_short` instead of `calculate_max_short` since the solidity implementation does not consider budget - removed the `fuzz_calculate_absolute_max_short_execute` in favor of a test that does the same but additionally checks that the pool is drained when that absolute max short is executed. It also includes some differential checks for rust vs solidity. This one is now called `fuzz_calculate_max_short_without_budget_then_open_short ` - adds a new `SLOW_FUZZ_RUNS` constant for one of the max tests bc it was slow as hell.
closing in favor of #152 |
Resolved Issues
#29
#121
Description
This PR updates the Rust open short and derivative implementations to more closely match Solidity.
Lower tolerances on tests 🎉
fuzz_calc_open_short
.fuzz_short_deposit_derivative
andfuzz_short_principal_derivative
tests. I use a larger epsilon and smaller tolerance, which I think is what we prefer (more distant points should match linearity assumptions better due to the lack of monotonicity coming frompow
).Increased tolerances on tests 😭
fuzz_calculate_max_short_no_budget
from 1e12 to 1e16. I compared the rust & solidity implementations, but they're too different from each other to find any bugs by pattern matching. I'm not sure what is going on here, and would suggest we open a new issue to investigate. (more on this below)fuzz_error_open_short_max_txn_amount
(per this comment), but could not (it is still 100M). This is probably related to lingering issues with calculating the max short.fuzz_calculate_max_short
, but I think the more likely scenario is the test wasn't set up to consistently fail for it's condition. I modified the test in my last commit and now it is consistently passing.(relatively) Inconsequential updates
empirical_derivative_epsilon
parameter. I also changed the tolerance check to be<=
instead of<
for the same reason.calc_max_short
in the preamble & open short tests. While on working this PR I was getting frustrated because the open short tests were failing due to errors in max short. This is a better setup in general, so that our tests behave more like unit tests that are easier to debug than integration tests.long_amount_derivative
tocalculate_open_long_derivative
andshort_deposit_derivative
tocalculate_open_short_derivative
.state
member variables. This is a departure from Solidity but (imo) justified because the Rust fns are all implemented on thestate
struct. The preferred pattern (and what we do elsewhere) is to update state and call the fn instead of passing different values as fn args.Important items to check
fuzz_calculate_max_short_no_budget
might be due to a difference inmax_short_guess
or an error in the solidity short derivative fn. I still think this is best investigated in a follow-up issue, but I'm noting it here as a clue for the future.Math outline
The below formulation follows the logic outlined in
HyperdriveShort.sol
. I made some slight changes to variable names and ordering of presentation.