Skip to content
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 fixed fraction division underflow #952

Merged
merged 12 commits into from
Sep 17, 2019
Merged

Fix fixed fraction division underflow #952

merged 12 commits into from
Sep 17, 2019

Conversation

m-chrzan
Copy link
Contributor

@m-chrzan m-chrzan commented Sep 12, 2019

Description

FixidityLib's divide can fail when given a divisor that's large (10^24), but still small enough that it might appear naturally within Celo smart contracts. There's different ways of working around this limitation.

getBuyTokenAmount (and similar) in Exchange

This function actually returns an integer, not a fraction, so we can get better accuracy with regular integer division.

isProposalPassing in Governance

A ratio is created for comparison with the constitution. Here we can use exact fraction arithmetic with numerators and denominators since the ratio is used only in this scope and won't have arithmetic done to it in the future, so there's no risk of future overflows.

unitsToValue in StableToken

The inflation factor shouldn't surpass dangerous divisor size.

Quick upper-bound estimation: if annual inflation were 5% (an order of magnitude more than the current initial proposal of 0.5%), in 500 years, the inflation factor would be on the order of 10^10, which is still a safe divisor.

getUpdatedInflationFactor in StableToken

denominator is always 10^decimals which is small enough.

Tested

Unit tests in protocol and contractkit passing.

Other changes

Increased bucket sizes in Exchange tests to test on more realistic values.

Related issues

@m-chrzan m-chrzan requested a review from asaj as a code owner September 12, 2019 23:46
@codecov
Copy link

codecov bot commented Sep 13, 2019

Codecov Report

Merging #952 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #952   +/-   ##
=======================================
  Coverage   66.92%   66.92%           
=======================================
  Files         252      252           
  Lines        7311     7311           
  Branches      418      418           
=======================================
  Hits         4893     4893           
  Misses       2331     2331           
  Partials       87       87
Flag Coverage Δ
#mobile 66.92% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 937bf5a...795e322. Read the comment docs.

@m-chrzan m-chrzan changed the title [WIP] Fix fixed fraction division underflow Fix fixed fraction division underflow Sep 13, 2019
Copy link
Contributor

@mcortesi mcortesi left a comment

Choose a reason for hiding this comment

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

I didn't check the actual math. Seems reasonable though.

A few comments:

  • PR description should live somewhere in the code (as comments) to avoid future changes that breaks this.
  • Seeing that we now unwrap and do int division with token amounts (that have 18 decimals in eth). Maybe we should think wether it makes sense to use fixidity or define boundaries to it's use.

@mcortesi mcortesi removed their assignment Sep 14, 2019
@m-chrzan m-chrzan self-assigned this Sep 16, 2019
@m-chrzan m-chrzan assigned mcortesi and unassigned m-chrzan Sep 16, 2019
@m-chrzan m-chrzan added the automerge Have PR merge automatically when checks pass label Sep 16, 2019
@ashishb ashishb merged commit 833a0be into master Sep 17, 2019
@ashishb ashishb deleted the m-chrzan/fixidity-fix branch September 17, 2019 00:14
aaronmgdr added a commit that referenced this pull request Sep 17, 2019
* master: (132 commits)
  [Wallet] Differentiate dollars and gold transactions (#1021)
  [wip] LinkedList cycle fix (#941)
  Fix slither issues (#572)
  [Wallet] Disable import wallet button when the backup phrase is not valid (#1012)
  Fix fixed fraction division underflow (#952)
  [CircleCI]Run protocol coverage once a day (#1018)
  Expose and test validator set precompiles (#874)
  [celotool] Fund Faucet accounts on network init (#990)
  Add Nexmo as a text provider for MX and US numbers (#1002)
  [CircleCI]Run protocol test with and without coverage (#991)
  Increase memory requests for Alfajores nodes (#966)
  [wallet]Select blockchain url based on testnet (#936)
  Pilot app config and ability to not show testnet banner (#1009)
  Add Linux onboarding steps and a TOC to SETUP.md (#1004)
  Add README-dev.md (#989)
  [Wallet] Exchange flow formatting fixes (#961)
  [celotool] Cleanup genesis block generation (#988)
  [celotool] Some ❤️. Clean up (#948)
  CeloCLI sorted list of Validator Groups should not include groups with zero votes (#907)
  [contractkit] Fix tests (#963)
  ...
aaronmgdr added a commit that referenced this pull request Sep 18, 2019
* master: (72 commits)
  Remove unused Reserve.burnToken function (#984)
  Persistent disks for transaction nodes (#1016)
  [Wallet] Differentiate dollars and gold transactions (#1021)
  [wip] LinkedList cycle fix (#941)
  Fix slither issues (#572)
  [Wallet] Disable import wallet button when the backup phrase is not valid (#1012)
  Fix fixed fraction division underflow (#952)
  [CircleCI]Run protocol coverage once a day (#1018)
  Expose and test validator set precompiles (#874)
  [celotool] Fund Faucet accounts on network init (#990)
  Add Nexmo as a text provider for MX and US numbers (#1002)
  [CircleCI]Run protocol test with and without coverage (#991)
  Increase memory requests for Alfajores nodes (#966)
  [wallet]Select blockchain url based on testnet (#936)
  Pilot app config and ability to not show testnet banner (#1009)
  Add Linux onboarding steps and a TOC to SETUP.md (#1004)
  Add README-dev.md (#989)
  [Wallet] Exchange flow formatting fixes (#961)
  [celotool] Cleanup genesis block generation (#988)
  [celotool] Some ❤️. Clean up (#948)
  ...

# Conflicts:
#	packages/web/src/header/Header.3.tsx
aaronmgdr added a commit that referenced this pull request Sep 18, 2019
* master: (38 commits)
  Remove unused Reserve.burnToken function (#984)
  Persistent disks for transaction nodes (#1016)
  [Wallet] Differentiate dollars and gold transactions (#1021)
  [wip] LinkedList cycle fix (#941)
  Fix slither issues (#572)
  [Wallet] Disable import wallet button when the backup phrase is not valid (#1012)
  Fix fixed fraction division underflow (#952)
  [CircleCI]Run protocol coverage once a day (#1018)
  Expose and test validator set precompiles (#874)
  [celotool] Fund Faucet accounts on network init (#990)
  Add Nexmo as a text provider for MX and US numbers (#1002)
  [CircleCI]Run protocol test with and without coverage (#991)
  Increase memory requests for Alfajores nodes (#966)
  [wallet]Select blockchain url based on testnet (#936)
  Pilot app config and ability to not show testnet banner (#1009)
  Add Linux onboarding steps and a TOC to SETUP.md (#1004)
  Add README-dev.md (#989)
  [Wallet] Exchange flow formatting fixes (#961)
  [celotool] Cleanup genesis block generation (#988)
  [celotool] Some ❤️. Clean up (#948)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Have PR merge automatically when checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exchange SBAT work when buckets are large
4 participants