-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
fix(math): revert to correct version of ApproxRoot from a former state breaking change #17725
Conversation
This reverts commit 952328a.
utACK. This has been in math since math/v1.0.1. |
math/dec.go
Outdated
if root != 2 { | ||
prev = guess.Power(root - 1) | ||
} | ||
for iter := 0; delta.Abs().GT(LegacySmallestDec()) && iter < maxApproxRootIterations; iter++ { |
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.
We can start by cheaper comparisons firstly of just integers then onto the .Abs().GT so
for iter := 0; iter < maxApproxRootIterations && delta.Abs().GT(legacySmallestDec); iter++ {
and even more LegacySmallestDec()
never changes so we can initiate it as a global so
var legacySmallestDec = LegacySmallestDec()
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.
The verdict @facundomedica: we can't just retract the v1.0.1 cosmossdk.io/math module because: |
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.
Discussed internally with Orijtech.
We will most probably merge this and contact the one party depending on the reverted behavior (Lava).
Could you make a changelog a bit more descriptive?
@julienrbrt changelog improved. Could you please also re-ACK the extra commits I added post approval? They should not affect state compatibility but keep performance benefits |
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.
LGTM, thank you @p0mvn!
@p0mvn could we please update the title to something more descriptive about the fix like fix(math): revert to correct version of ApproxRoot from a former state breaking change and then the commit message and PR message to This PR fixes a state break that was introduced in a performanice optimization
PR #16141 from May 2023 (4 months ago)
Upon Osmosis switching to math/1.1.2, we started getting app hashes in the module responsible for swaps.
Debugging has led to realizing that old ApproxRoot and new ApproxRoot produced different results.
For example, before the change in this PR, we would have a sqrt producing:
base : "1.000000011823380862"
output: "1.000000005911690413"
After:
base: "1.000000011823380862"
output: "1.000000005911690414"
The discrepancy stems from this PR: https://github.com/cosmos/cosmos-sdk/pull/16141 which was later https://github.com/osmosis-labs/cosmos-sdk/pull/468 due to the state break but not in the upstream (here).
The change in this PR essentially copies: https://github.com/osmosis-labs/cosmos-sdk/pull/468
It also adds a testcase proving that the state-break has been fixed. |
Tagged a rc, so you can already integrate: https://github.com/cosmos/cosmos-sdk/releases/tag/math%2Fv1.1.3-rc.0 |
…ions This fuzzer performs a roundtrip comparison for square roots comparing the value from: * squaring LegacyDec.ApproxRoot(2) against itself then checking if the value is very close to the original whole value * comparing against the Go standard library's math/big.Float.Sqrt and panicking if we find a deviation of greater than 5% It's great to note that the results from this library so far from fuzzing show a higher precision/accuracy than math/big.Float.Sqrt for example with: LegacyDec.ApproxRoot(100000000000000000.000000000000000000, 2) Stdlib sqrt: 316227766.000000000000000000 cosmossdk.io/math.*Dec.ApproxSqrt: 316227766.016837933199889354 Python3.7.6: math.sqrt(100000000000000000): 316227766.01683795 LegacyDec.ApproxRoot(10000700000000000.000000000000000000, 2) Stdlib sqrt: 100003499.900000000000000000 cosmossdk.io/math.*Dec.ApproxSqrt: 100003499.938752143656215533 Python3.7.6: math.sqrt(10000700000000000): 100003499.93875214 Updates PR #16141 Updates PR #17725
…ions This fuzzer performs a roundtrip comparison for square roots comparing the value from: * squaring LegacyDec.ApproxRoot(2) against itself then checking if the value is very close to the original whole value * comparing against the Go standard library's math/big.Float.Sqrt and panicking if we find a deviation of greater than 5% It's great to note that the results from this library so far from fuzzing show a higher precision/accuracy than math/big.Float.Sqrt for example with: LegacyDec.ApproxRoot(100000000000000000.000000000000000000, 2) Stdlib sqrt: 316227766.000000000000000000 cosmossdk.io/math.*Dec.ApproxSqrt: 316227766.016837933199889354 Python3.7.6: math.sqrt(100000000000000000): 316227766.01683795 LegacyDec.ApproxRoot(10000700000000000.000000000000000000, 2) Stdlib sqrt: 100003499.900000000000000000 cosmossdk.io/math.*Dec.ApproxSqrt: 100003499.938752143656215533 Python3.7.6: math.sqrt(10000700000000000): 100003499.93875214 Updates PR #16141 Updates PR #17725
This PR fixes a state break that was introduced in a performanice optimization
PR #16141 from May 2023 (4 months ago)
Upon Osmosis switching to math/1.1.2, we started getting app hashes in the module responsible for swaps.
Debugging has led to realizing that old ApproxRoot and new ApproxRoot produced different results.
For example, before the change in this PR, we would have a sqrt producing:
base : "1.000000011823380862"
output: "1.000000005911690413"
After:
base: "1.000000011823380862"
output: "1.000000005911690414"
The discrepancy stems from this PR: #16141 which was later osmosis-labs#468 due to the state break but not in the upstream (here).
The change in this PR essentially copies: osmosis-labs#468
It also adds a testcase proving that the state-break has been fixed.