Skip to content

squashed: Expand Number to support full integer range as of 9526a30 #6192

Merged
ximinez merged 42 commits intorelease-3.1from
ximinez/lending-3.1-number
Jan 13, 2026
Merged

squashed: Expand Number to support full integer range as of 9526a30 #6192
ximinez merged 42 commits intorelease-3.1from
ximinez/lending-3.1-number

Conversation

@ximinez
Copy link
Collaborator

@ximinez ximinez commented Jan 8, 2026

High Level Overview of Change

#6025 (ximinez/lending-number-simple) rebased onto #6156 (ximinez/lending-3.1).

Changes after the one noted will need to be manually synced across the two branches.

Tapanito and others added 19 commits December 18, 2025 11:20
- Originally defined as uint64_t, but the testIssuerLoan() test called
  it with a negative number, causing an overflow to a very large number
  that in some circumstances could be silently cast back to an int64_t,
  but might not be. I believe this is UB, and we don't want to rely on
  that.
- Adds additional unit tests to cover math calculations.
- Removes unused methods.
- add nodiscard to unimpairLoan, and check result in LoanPay
- add a check to verify that issuer exists
- improve LoanManage error code for dust amounts
- In overpayment results, the management fee was being calculated twice:
  once as part of the value change, and as part of the fees paid.
  Exclude it from the value change.
Co-authored-by: Shawn Xie <35279399+shawnxie999@users.noreply.github.com>
- Round the initial total value computation upward, unless there is
  0-interest.
- Rename getVaultScale to getAssetsTotalScale, and convert one incorrect
  computation to use it.
- Use adjustImpreciseNumber for LossUnrealized.
- Add some logging to computeLoanProperties.
- Adds loan state to LoanProperties.
- Cleans up computeLoanProperties.
- Fixes missing management fee from overpayment.
* Replace accountHolds with accountSpendable when checking
for account funds in VaultDeposit and LoanBrokerCoverDeposit
- Updates or fixes a couple of things I noticed while reviewing changes
  to the spec.
- Rename sfPreviousPaymentDate to sfPreviousPaymentDueDate.
- Make the vault asset cap check added in #6124 a little more robust:
  1. Check in preflight if the vault is _already_ over the limit.
  2. Prevent overflow when checking with the loan value. (Subtract
     instead of adding, in case the values are near maxint. Both return
     the same result. Also add a unit test so each case is covered.
@ximinez ximinez requested a review from a team as a code owner January 8, 2026 19:29
Tapanito and others added 2 commits January 8, 2026 18:37
- Fixes LoanManage tfBAD_LEDGER case by capping the amount of FLC to use to cover a loss at the amount of cover available.
- Check if the Vault pseudo-account is frozen in LoanBrokerSet
@codecov
Copy link

codecov bot commented Jan 9, 2026

Codecov Report

❌ Patch coverage is 96.03340% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.4%. Comparing base (91d239f) to head (e32455d).
⚠️ Report is 1 commits behind head on release-3.1.

Files with missing lines Patch % Lines
src/libxrpl/basics/Number.cpp 97.6% 5 Missing ⚠️
src/libxrpl/protocol/STAmount.cpp 72.2% 5 Missing ⚠️
src/xrpld/app/tx/detail/LoanSet.cpp 70.6% 5 Missing ⚠️
include/xrpl/basics/Number.h 97.2% 2 Missing ⚠️
src/libxrpl/protocol/Issue.cpp 0.0% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##           release-3.1   #6192    +/-   ##
============================================
  Coverage         79.3%   79.4%            
============================================
  Files              837     839     +2     
  Lines            71444   71716   +272     
  Branches          8245    8255    +10     
============================================
+ Hits             56665   56913   +248     
- Misses           14779   14803    +24     
Files with missing lines Coverage Δ
include/xrpl/protocol/AmountConversions.h 87.7% <100.0%> (ø)
include/xrpl/protocol/IOUAmount.h 100.0% <100.0%> (ø)
include/xrpl/protocol/Issue.h 100.0% <ø> (ø)
include/xrpl/protocol/MPTIssue.h 100.0% <ø> (ø)
include/xrpl/protocol/Protocol.h 100.0% <ø> (ø)
include/xrpl/protocol/SField.h 100.0% <ø> (ø)
include/xrpl/protocol/STAmount.h 96.2% <100.0%> (+0.3%) ⬆️
include/xrpl/protocol/STNumber.h 100.0% <ø> (ø)
include/xrpl/protocol/STTakesAsset.h 100.0% <100.0%> (ø)
include/xrpl/protocol/SystemParameters.h 100.0% <ø> (ø)
... and 26 more

... and 6 files with indirect coverage changes

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Tapanito and others added 7 commits January 8, 2026 20:31
…6117)

- Check the trust line limit is not exceeded for a withdraw to a third party Destination account.
- Remove redundant "else" because of early return.
- Add a concept "Integral64" to use in the template parameter for
  normalizeToRange.
- Add documentation to STTakesAsset, associateAsset, and add explanation
  for how `STNumber` uses it.
- Assert that `numberToJson` is always outside of a Transaction context.
- Remove copy of `InitialFibSeqPct` and the assert in
  `Number::operator*=` that prompted me to add it.
- Add amendment gating to `STAmount::operator=`
- Adds a mechanism for the vault owner to burn user shares when the vault is stuck. If the Vault has 0 AssetsAvailable and Total, the owner may submit a VaultClawback to reclaim the worthless fees, and thus allow the Vault to be deleted. The Amount must be left off (unless the owner is the asset issuer), specified as 0 Shares, or specified as the number of Shares held.
Tapanito and others added 2 commits January 10, 2026 00:55
- Directly access member variables instead of "external view" mantissa()
  and exponent() in computations.
- Remove some overly restrictive asserts.
- Update the testLoanBrokerSetDebtMaximum test to use an MPT asset
  (otherwise, large limits get truncated), and more operationally safe
  math.
…nez/lending-3.1

* mywork/ximinez/vault-fix-3.1:
  VaultClawback: Burn shares of an empty vault (6120)
  fix: Inner batch transactions never have valid signatures (6069)
  fix: Reorder Batch Preflight Errors (6176)
- Add minimum grace period validation (#6133)
- VaultClawback: Burn shares of an empty vault (#6120)
- LoanSet transaction added in #6120 failed the minimum grace period
  added by #6133.
…z/lending-3.1-number

* mywork/ximinez/lending-3.1:
  Fix test failures from interaction between 6120 and 6133
  VaultClawback: Burn shares of an empty vault (6120)
  fix: Inner batch transactions never have valid signatures (6069)
  fix: Reorder Batch Preflight Errors (6176)
@ximinez ximinez force-pushed the ximinez/lending-3.1-number branch from 9108a4c to fce4cbe Compare January 11, 2026 04:37
Tapanito and others added 3 commits January 11, 2026 01:03
- The previous fix removed the guarantee that addition mantissas were
  less than maxRep. Without that guaranee, and with large enough
  mantissas, the total can be greater than max uint64_t, causing an
  overflow.  - Please please please let this be the last bug!
@ximinez ximinez force-pushed the ximinez/lending-3.1-number branch from abd8d68 to f5a64b2 Compare January 11, 2026 20:35
Comment on lines 306 to 587
if (xm < 0)
{
xm = -xm;
xn = -1;
}
auto ym = y.mantissa();
auto ye = y.exponent();
int yn = 1;
if (ym < 0)
{
ym = -ym;
yn = -1;
}
"xrpl::Number::operator+=(Number) : is normal");
// *n = negative
// *s = sign
// *m = mantissa
// *e = exponent

// Need to use uint128_t, because large mantissas can overflow when added
// together.
bool xn = negative_;
uint128_t xm = mantissa_;
auto xe = exponent_;

bool yn = y.negative_;
uint128_t ym = y.mantissa_;
auto ye = y.exponent_;
Copy link
Collaborator

@shawnxie999 shawnxie999 Jan 12, 2026

Choose a reason for hiding this comment

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

trying to understand the change. The conversions when the number is negative( xm = -xm; and ym = -ym;) are removed. Wouldn't that cause problem when one number is negative and the other is positive? (also isn't this a breaking change?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's internal representation now so it's always positive.

Copy link
Collaborator

Choose a reason for hiding this comment

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

right, but should it be using internal rep if the two numbers have different signs?

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 not following. Why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

trying to understand the change. The conversions when the number is negative( xm = -xm; and ym = -ym;) are removed. Wouldn't that cause problem when one number is negative and the other is positive? (also isn't this a breaking change?)

Good question. It's safe because in both cases xn and yn are used to keep track of the sign of the original mantissas. The difference is that now I can grab them directly from the member variables instead of having to test, e.g. if (xm < 0).

So remember, before this refactoring, mantissa_ was an int64_t and mantissa() returned it unaltered.

  • If x is non-negative, then xn == 1 and xm == mantissa_ and thus xm >= 0.
  • If x is negative, then xn == -1 and xm == -mantissa_ and thus xm > 0.

After the change, mantissa_ will never be negative. xn changes from an int to a bool.

  • If x is non-negative, then xn == false, and xm == mantissa_, and xm >= 0.
  • If x is negative, then xn == true, and still xm == mantissa_, and xm >= 0.

During the computation in both cases, xn may be altered depending on how x relates to y, and at the end when the result is being put back together, the old code did mantissa_ = xn * xm, and the new code just assigns xn to negative_ and xm to mantissa_.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the explanation! I looked through the function again and it makes sense

@ximinez ximinez requested a review from shawnxie999 January 12, 2026 17:32
Comment on lines 306 to 587
if (xm < 0)
{
xm = -xm;
xn = -1;
}
auto ym = y.mantissa();
auto ye = y.exponent();
int yn = 1;
if (ym < 0)
{
ym = -ym;
yn = -1;
}
"xrpl::Number::operator+=(Number) : is normal");
// *n = negative
// *s = sign
// *m = mantissa
// *e = exponent

// Need to use uint128_t, because large mantissas can overflow when added
// together.
bool xn = negative_;
uint128_t xm = mantissa_;
auto xe = exponent_;

bool yn = y.negative_;
uint128_t ym = y.mantissa_;
auto ye = y.exponent_;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the explanation! I looked through the function again and it makes sense

- Reduce code duplication in LoanBrokerDelete
- Reorder "canWithdraw" parameters to put "view" first
- Combine accountSpendable into accountHolds
- Avoid copies by taking a reference for the claw amount
- Return function results directly
- Fix typo for "parseLoan" in ledger_entry RPC
- Improve some comments and unused variables
- No need for late payment components lambda
- Add explanatory comment for computeLoanProperties
- Add comment linking computeRawLoanState to spec
- Fix typo: TrueTotalPrincipalOutstanding
- Fix missed ripple -> xrpl update
- Remove unnecessary "else"s.
- Clean up std::visit in accountHolds.
…/lending-3.1-number

* XRPLF/ximinez/lending-3.1:
  Address review feedback from Lending Protocol re-review (6161)
Base automatically changed from ximinez/lending-3.1 to release-3.1 January 12, 2026 23:08
ximinez and others added 2 commits January 12, 2026 18:38
…-3.1-number

* XRPLF/release-3.1:
  Improve and fix bugs in Lending Protocol - XLS-66 (6156)
Co-authored-by: Shawn Xie <35279399+shawnxie999@users.noreply.github.com>
@ximinez ximinez force-pushed the ximinez/lending-3.1-number branch 2 times, most recently from 7b7075a to e32455d Compare January 13, 2026 00:13
@ximinez ximinez enabled auto-merge (squash) January 13, 2026 00:15
@ximinez ximinez merged commit 1ead5a7 into release-3.1 Jan 13, 2026
60 of 65 checks passed
@ximinez ximinez deleted the ximinez/lending-3.1-number branch January 13, 2026 01:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants