Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Use sensible maths for from_rational #13660

Merged
merged 19 commits into from
Mar 24, 2023
Merged

Conversation

gavofyork
Copy link
Member

@gavofyork gavofyork commented Mar 20, 2023

Old maths had all sorts of small edge cases with rounding which broke expectations. Now we just use the stuff which is already proven.

Note one test in staking was relying on the old buggy maths. It had to be corrected with some dirty magic numbers. Not sure if @kianenigma wants to take a look. A test was added in per_things (from_rational_with_rounding_breakage_2) to demonstrate that the old numbers for which this tests behaviour relied on are in fact wrong and do definitely break expectations.

Possibly fix for paritytech/polkadot-sdk#200

@gavofyork gavofyork added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit T1-runtime This PR/Issue is related to the topic “runtime”. labels Mar 20, 2023
@gavofyork gavofyork requested a review from a team March 21, 2023 04:24
@gavofyork gavofyork requested review from kianenigma and ggwpez March 21, 2023 11:00
ggwpez added 2 commits March 21, 2023 13:46
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
ggwpez added 2 commits March 21, 2023 14:01
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@gavofyork
Copy link
Member Author

gavofyork commented Mar 21, 2023

Division by zero should panic if there is no way to return an Err. It panics in regular arithmetic. Returning some finite, strictly incorrect, value could mask major errors (tests would no longer panic) and lead to unintended consequences.

It's reasonable to introduce alternative versions of these functions which return Result and thus need not panic for division-by-zero. If we really want to make the infallible functions not panic on division by zero, I would at least use defensive! to ensure the error does not go unnoticed.

ggwpez added 4 commits March 21, 2023 14:14
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This reverts commit 7e88ac7.
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
@ggwpez
Copy link
Member

ggwpez commented Mar 21, 2023

Division by zero should panic if there is no way to return an Err. It panics in regular arithmetic. Returning some finite, strictly incorrect, value could mask major errors (tests would no longer panic) and lead to unintended consequences.

It's reasonable to introduce alternative versions of these functions which return Result and thus need not panic for division-by-zero. If we really want to make the infallible functions not panic on division by zero, I would at least use defensive! to ensure the error does not go unnoticed.

Yea the case that it wont show up in tests anymore is indeed bad. So having Result functions is probably the only proper way since the code in sp-* cannot use the defensive! from frame-support. Should I create an issue for it?

@gavofyork
Copy link
Member Author

Division by zero should panic if there is no way to return an Err. It panics in regular arithmetic. Returning some finite, strictly incorrect, value could mask major errors (tests would no longer panic) and lead to unintended consequences.
It's reasonable to introduce alternative versions of these functions which return Result and thus need not panic for division-by-zero. If we really want to make the infallible functions not panic on division by zero, I would at least use defensive! to ensure the error does not go unnoticed.

Yea the case that it wont show up in tests anymore is indeed bad. So having Result functions is probably the only proper way since the code in sp-* cannot use the defensive! from frame-support. Should I create an issue for it?

Yes, think so.

ggwpez added 2 commits March 21, 2023 15:11
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Copy link
Member

@ggwpez ggwpez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

% the comment on multiply_rational.
Still need to figure out how to run these tests in the CI.

ggwpez added 2 commits March 21, 2023 15:24
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Copy link
Contributor

@ruseinov ruseinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the logic looks good to me, however we should probably fix the staking test to actually reflect this properly and not via magic numbers.

Copy link
Contributor

@ruseinov ruseinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approving this as fixing the staking test properly could be done as part of another issue

@gavofyork
Copy link
Member Author

bot merge

@paritytech-processbot paritytech-processbot bot merged commit 656b139 into master Mar 24, 2023
@paritytech-processbot paritytech-processbot bot deleted the gav-fix-from-rational branch March 24, 2023 14:48
ggwpez added a commit that referenced this pull request Mar 29, 2023
This reverts #13651 since we merged the
proper fix in #13660

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
breathx pushed a commit to gear-tech/substrate that referenced this pull request Apr 22, 2023
* Use sensible maths for from_rational

* Fixes

* Fixes

* More fixes

* Remove debugging

* Add fuzzer tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Prevent panics

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* docs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Clean up old code

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Test all rounding modes of from_rational

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Clean up code

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Revert "Prevent panics"

This reverts commit 7e88ac7.

* fix imports

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* cleanup

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fuzz test multiply_rational

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix import

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* fmt

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Return None in multiply_rational on zero div

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
nathanwhit pushed a commit to nathanwhit/substrate that referenced this pull request Jul 19, 2023
* Use sensible maths for from_rational

* Fixes

* Fixes

* More fixes

* Remove debugging

* Add fuzzer tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Prevent panics

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* docs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Clean up old code

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Test all rounding modes of from_rational

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Clean up code

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Revert "Prevent panics"

This reverts commit 7e88ac7.

* fix imports

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* cleanup

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fuzz test multiply_rational

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix import

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* fmt

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Return None in multiply_rational on zero div

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit T1-runtime This PR/Issue is related to the topic “runtime”.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants