mirror of
https://github.com/XRPLF/rippled.git
synced 2026-01-25 09:05:24 +00:00
Compare commits
4 Commits
develop
...
ximinez/le
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
958a7c12c6 | ||
|
|
20d9cb89dd | ||
|
|
e105d59b90 | ||
|
|
8cae6b0adc |
@@ -2758,15 +2758,13 @@ protected:
|
||||
state.paymentRemaining,
|
||||
broker.params.managementFeeRate);
|
||||
|
||||
BEAST_EXPECTS(
|
||||
paymentComponents.specialCase ==
|
||||
detail::PaymentSpecialCase::final ||
|
||||
paymentComponents.trackedValueDelta <=
|
||||
roundedPeriodicPayment,
|
||||
"Delta: " +
|
||||
to_string(paymentComponents.trackedValueDelta) +
|
||||
", periodic payment: " +
|
||||
to_string(roundedPeriodicPayment));
|
||||
BEAST_EXPECT(
|
||||
paymentComponents.trackedValueDelta ==
|
||||
roundedPeriodicPayment ||
|
||||
(paymentComponents.specialCase ==
|
||||
detail::PaymentSpecialCase::final &&
|
||||
paymentComponents.trackedValueDelta <
|
||||
roundedPeriodicPayment));
|
||||
|
||||
xrpl::LoanState const nextTrueState =
|
||||
computeTheoreticalLoanState(
|
||||
|
||||
@@ -1057,18 +1057,37 @@ computePaymentComponents(
|
||||
"xrpl::detail::computePaymentComponents",
|
||||
"excess non-negative");
|
||||
};
|
||||
// Helper to reduce deltas when they collectively exceed a limit.
|
||||
// Order matters: we prefer to reduce interest first (most flexible),
|
||||
// then management fee, then principal (least flexible).
|
||||
auto giveTo =
|
||||
[](Number& component, Number& shortage, Number const& maximum) {
|
||||
if (shortage > beast::zero)
|
||||
{
|
||||
// Put as much of the shortage as we can into the provided part
|
||||
// and the total
|
||||
auto part = std::min(maximum - component, shortage);
|
||||
component += part;
|
||||
shortage -= part;
|
||||
}
|
||||
// If the shortage goes negative, we put too much, which should be
|
||||
// impossible
|
||||
XRPL_ASSERT_PARTS(
|
||||
shortage >= beast::zero,
|
||||
"ripple::detail::computePaymentComponents",
|
||||
"excess non-negative");
|
||||
};
|
||||
auto addressExcess = [&takeFrom](LoanStateDeltas& deltas, Number& excess) {
|
||||
// This order is based on where errors are the least problematic
|
||||
takeFrom(deltas.interest, excess);
|
||||
takeFrom(deltas.managementFee, excess);
|
||||
takeFrom(deltas.principal, excess);
|
||||
};
|
||||
|
||||
// Check if deltas exceed the total outstanding value. This should never
|
||||
// happen due to earlier caps, but handle it defensively.
|
||||
auto addressShortage = [&giveTo](
|
||||
LoanStateDeltas& deltas,
|
||||
Number& shortage,
|
||||
LoanState const& current) {
|
||||
giveTo(deltas.interest, shortage, current.interestDue);
|
||||
giveTo(deltas.managementFee, shortage, current.managementFeeDue);
|
||||
giveTo(deltas.principal, shortage, current.principalOutstanding);
|
||||
};
|
||||
Number totalOverpayment =
|
||||
deltas.total() - currentLedgerState.valueOutstanding;
|
||||
|
||||
@@ -1097,14 +1116,33 @@ computePaymentComponents(
|
||||
addressExcess(deltas, excess);
|
||||
shortage = -excess;
|
||||
}
|
||||
else if (shortage > beast::zero && totalOverpayment < beast::zero)
|
||||
{
|
||||
// If there's a shortage, and there's room in the loan itself, we can
|
||||
// top up the parts to make the payment correct.
|
||||
shortage = std::min(-totalOverpayment, shortage);
|
||||
addressShortage(deltas, shortage, currentLedgerState);
|
||||
}
|
||||
|
||||
// At this point, shortage >= 0 means we're paying less than the full
|
||||
// periodic payment (due to rounding or component caps).
|
||||
// shortage < 0 would mean we're trying to pay more than allowed (bug).
|
||||
// The shortage should never be negative, which indicates that the parts are
|
||||
// trying to take more than the whole payment. The shortage should not be
|
||||
// positive, either, which indicates that we're not going to take the whole
|
||||
// payment amount. Only the last payment should be allowed to have a
|
||||
// shortage, and that's handled in a special case above.
|
||||
XRPL_ASSERT_PARTS(
|
||||
shortage >= beast::zero,
|
||||
"xrpl::detail::computePaymentComponents",
|
||||
shortage == beast::zero,
|
||||
"ripple::detail::computePaymentComponents",
|
||||
"no shortage or excess");
|
||||
#if LOANCOMPLETE
|
||||
/*
|
||||
// This used to be part of the above assert. It will eventually be removed
|
||||
// if proved accurate
|
||||
||
|
||||
(shortage > beast::zero &&
|
||||
((asset.integral() && shortage < 3) ||
|
||||
(scale - shortage.exponent() > 14)))
|
||||
*/
|
||||
#endif
|
||||
|
||||
// Final validation that all components are valid
|
||||
XRPL_ASSERT_PARTS(
|
||||
|
||||
Reference in New Issue
Block a user