XRPLF / rippled

Decentralized cryptocurrency blockchain daemon implementing the XRP Ledger protocol in C++
https://xrpl.org
ISC License
4.48k stars 1.45k forks source link

fix: AMM swap rounding #5002

Closed seelabs closed 2 months ago

seelabs commented 2 months ago

High Level Overview of Change

fix amendment: AMM swap should honor invariants:

The AMM has an invariant for swaps where:

new_balance_1*new_balance_2 >= old_balance_1*old_balance_2

Due to rounding, this invariant could sometimes be violated (although by very small amounts).

This patch introduces an amendment fixAMMRounding that changes the rounding to always favor the AMM. Doing this should maintain the invariant.

Type of Change

codecov-commenter commented 2 months ago

Codecov Report

Attention: Patch coverage is 60.52632% with 30 lines in your changes are missing coverage. Please review.

Project coverage is 70.9%. Comparing base (b422e71) to head (b90f58d).

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/XRPLF/rippled/pull/5002/graphs/tree.svg?width=650&height=150&src=pr&token=i2RPGI5xGF&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF)](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF) ```diff @@ Coverage Diff @@ ## develop #5002 +/- ## ========================================= - Coverage 70.9% 70.9% -0.0% ========================================= Files 796 796 Lines 66727 66792 +65 Branches 10976 10996 +20 ========================================= + Hits 47339 47378 +39 - Misses 19388 19414 +26 ``` | [Files](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF) | Coverage Δ | | |---|---|---| | [src/ripple/app/tx/impl/Transactor.cpp](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?src=pr&el=tree&filepath=src%2Fripple%2Fapp%2Ftx%2Fimpl%2FTransactor.cpp&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF#diff-c3JjL3JpcHBsZS9hcHAvdHgvaW1wbC9UcmFuc2FjdG9yLmNwcA==) | `84.5% <100.0%> (+<0.1%)` | :arrow_up: | | [src/ripple/basics/Number.h](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?src=pr&el=tree&filepath=src%2Fripple%2Fbasics%2FNumber.h&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF#diff-c3JjL3JpcHBsZS9iYXNpY3MvTnVtYmVyLmg=) | `100.0% <100.0%> (ø)` | | | [src/ripple/protocol/Feature.h](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?src=pr&el=tree&filepath=src%2Fripple%2Fprotocol%2FFeature.h&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF#diff-c3JjL3JpcHBsZS9wcm90b2NvbC9GZWF0dXJlLmg=) | `100.0% <ø> (ø)` | | | [src/ripple/protocol/Rules.h](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?src=pr&el=tree&filepath=src%2Fripple%2Fprotocol%2FRules.h&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF#diff-c3JjL3JpcHBsZS9wcm90b2NvbC9SdWxlcy5o) | `100.0% <100.0%> (ø)` | | | [src/ripple/protocol/impl/Feature.cpp](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?src=pr&el=tree&filepath=src%2Fripple%2Fprotocol%2Fimpl%2FFeature.cpp&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF#diff-c3JjL3JpcHBsZS9wcm90b2NvbC9pbXBsL0ZlYXR1cmUuY3Bw) | `93.9% <ø> (ø)` | | | [src/ripple/protocol/impl/Rules.cpp](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?src=pr&el=tree&filepath=src%2Fripple%2Fprotocol%2Fimpl%2FRules.cpp&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF#diff-c3JjL3JpcHBsZS9wcm90b2NvbC9pbXBsL1J1bGVzLmNwcA==) | `97.9% <100.0%> (+0.4%)` | :arrow_up: | | [src/ripple/ledger/impl/View.cpp](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?src=pr&el=tree&filepath=src%2Fripple%2Fledger%2Fimpl%2FView.cpp&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF#diff-c3JjL3JpcHBsZS9sZWRnZXIvaW1wbC9WaWV3LmNwcA==) | `91.6% <75.0%> (-0.1%)` | :arrow_down: | | [src/ripple/protocol/AmountConversions.h](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?src=pr&el=tree&filepath=src%2Fripple%2Fprotocol%2FAmountConversions.h&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF#diff-c3JjL3JpcHBsZS9wcm90b2NvbC9BbW91bnRDb252ZXJzaW9ucy5o) | `85.7% <0.0%> (-10.3%)` | :arrow_down: | | [src/ripple/app/misc/AMMHelpers.h](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?src=pr&el=tree&filepath=src%2Fripple%2Fapp%2Fmisc%2FAMMHelpers.h&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF#diff-c3JjL3JpcHBsZS9hcHAvbWlzYy9BTU1IZWxwZXJzLmg=) | `69.0% <53.1%> (-16.1%)` | :arrow_down: | ... and [5 files with indirect coverage changes](https://app.codecov.io/gh/XRPLF/rippled/pull/5002/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF) [![Impacted file tree graph](https://app.codecov.io/gh/XRPLF/rippled/pull/5002/graphs/tree.svg?width=650&height=150&src=pr&token=i2RPGI5xGF&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF)](https://app.codecov.io/gh/XRPLF/rippled/pull/5002?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XRPLF)