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

AMM Unit tests: rounding down of equal asset deposit LPToken calculation #4982

Merged
merged 12 commits into from
Apr 18, 2024

Conversation

ckeshava
Copy link
Collaborator

@ckeshava ckeshava commented Apr 5, 2024

High Level Overview of Change

This PR exercises a specific portion of the AMMHelpers.cpp file. It triggers a downward rounding of LPTokens in adjustLPTokens function in the AMMHelpers.cpp file.

Context of Change

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Performance (increase or change in throughput and/or latency)
  • Tests (you added tests for code that already exists, or your new feature included in this PR)
  • Documentation update
  • Chore (no impact to binary, e.g. .gitignore, formatting, dropping support for older tooling)
  • Release

API Impact

  • Public API: New feature (new methods and/or new fields)
  • Public API: Breaking change (in general, breaking changes should only impact the next api_version)
  • libxrpl change (any change that may affect libxrpl or dependents of libxrpl)
  • Peer protocol change (must be backward compatible or bump the peer protocol version)
    No impact on performance. No change to APIs

// testClawback();
// testAMMID();
// testSelection();
// testFixDefaultInnerObj();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why comment out all of the tests?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

my bad, fixed it

@codecov-commenter
Copy link

codecov-commenter commented Apr 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.9%. Comparing base (24a275b) to head (8f7dbfb).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           develop   #4982   +/-   ##
=======================================
  Coverage     70.9%   70.9%           
=======================================
  Files          796     796           
  Lines        66727   66727           
  Branches     10981   10976    -5     
=======================================
+ Hits         47333   47338    +5     
+ Misses       19394   19389    -5     

see 2 files with indirect coverage changes

Impacted file tree graph

@ckeshava ckeshava requested a review from HowardHinnant April 5, 2024 19:03
@HowardHinnant
Copy link
Contributor

This looks good to me. But I was hoping it would hit these lines: https://app.codecov.io/gh/XRPLF/rippled/pull/4982/blob/src/ripple/app/misc/impl/AMMHelpers.cpp#L171. And I don't have a good explanation as to why it didn't. Nevertheless, this looks like a good addition to me.

@ckeshava
Copy link
Collaborator Author

ckeshava commented Apr 5, 2024

@HowardHinnant I have updated the numbers so that the execution hits the necessary lines.

I expected 10'000.1 to be rounded down to 10'000 and consequently hit the said line. I think my debugger is malfunctioning.

@seelabs seelabs requested review from gregtatcam and seelabs and removed request for gregtatcam April 8, 2024 20:23
BEAST_EXPECT(expectLedgerEntryRoot(env, carol, XRP(30'000) -
depositXRP - txfee(env, 1)));
});

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe my brain isn't working today, but I'm not following how this tests rounding down. Where would the calculation differ if we rounded to nearest, say? Can you add some clarifying comments to help me follow this test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The values computed by AMMHelpers.cpp:adjustLPTokens would be different, if we used the nearest rounding mode. I have tagged that line above ^

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm still not sure how the test is showing this difference. I think what I'm looking for is a comment in the test that explains how the test proves that the rounding is working as we intended. The test may be doing that, but I'm not sure how. I think I need a comment to explain how the test works.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fwiw, I checked the test coverage of AMMHelpers.cpp before and after this PR and this PR improves the coverage in this file, specifically in adjustAmountsByLPTokens.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks howard.
@seelabs I have added a test case and asserts on line 1155 to show the rounding down behavior

// digits, it is possible that lpTokensActual is rounded down. This is to
// ensure that we don't create/burn more LPTokens (depending on
// whether it's AMMDeposit or AMMWithdraw transaction) than what the trader
// asked for.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@seelabs This is one of the points in the codebase where LPTokens are rounded down. Check the function above adjustLPTokens.

Consequently, lpTokensActual < lpTokens by a very small margin.

BEAST_EXPECT(ammAlice.expectBalances(
XRP(10'000) + depositXRP,
USD(10'000) + depositUSD,
IOUAmount{1, 7} + newLPTokens));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't understand how lines 1156 and 1177 can be true at the same. I'm guessing it's due to the loss of precision in the last few digits.

newLPTokens == deltaLPTokens (numerically)
initLPTokens + newLPTokens = finalLPTokens, but finalLPToken - initLPToken < deltaLPTokens

Copy link
Collaborator

@seelabs seelabs left a comment

Choose a reason for hiding this comment

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

👍 nice job on this!

@seelabs seelabs added the Passed Passed code review & PR owner thinks it's ready to merge. Perf sign-off may still be required. label Apr 10, 2024
@ckeshava
Copy link
Collaborator Author

Hello, the commit message could be:
Unit test to examine the rounding behavior of equal-asset deposits with tfLPToken flag

@ximinez

@ximinez ximinez merged commit df3aa84 into XRPLF:develop Apr 18, 2024
17 checks passed
sophiax851 pushed a commit to sophiax851/rippled that referenced this pull request Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Passed Passed code review & PR owner thinks it's ready to merge. Perf sign-off may still be required.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants