Compare commits

...

13 Commits

Author SHA1 Message Date
Pratik Mankawde
11560fb51b test case for divergence 2026-05-19 15:42:45 +01:00
Vito
4e907d9662 test: review fixes for cover precision guard
- Document XRP/MPT and IOU short-circuit behaviour in
  STAmount::isZeroAtScale
- Disambiguate duplicate "Cover precision guard: Deposit" testcase
  names in LoanBroker_test
- Extend testIsZeroAtScale with half-ULP boundary, large-magnitude
  gap, and negative-value cases
- Tighten ASSERT in canApplyToBrokerCover to compare full assets
  (covers MPT, not just issuer)
2026-05-19 12:31:18 +02:00
Vito
5f6bb93b5c fix: post merge bugs 2026-05-18 19:15:26 +02:00
Vito
2b4d8ef3a2 curse you clang-tidy 2026-05-18 19:03:39 +02:00
Vito
13358f5042 fix: post merge bugs 2026-05-18 14:34:57 +02:00
Vito
e1946278f9 Merge remote-tracking branch 'origin/develop' into tapanito/lending-cover-precision 2026-05-18 14:34:32 +02:00
Vito
c4115ee4be chore: clang-tidy and minor AI comments 2026-05-18 12:20:26 +02:00
Vito
e2ba7a728b fix: Round deposit cover to LoanBroker scale 2026-05-15 16:23:41 +02:00
Vito
a5db99fb96 chore: please clang-tidy 2026-05-14 15:44:18 +02:00
Vito
20678e6bc4 test: Add isZeroAtScale coverage to STAmount_test
Tests the new STAmount::isZeroAtScale method across IOU, XRP, and MPT
amounts, covering the zero, sub-ULP, one-ULP, and scale-equals-exponent
short-circuit cases.
2026-05-14 15:30:12 +02:00
Vito
42eadf0416 review: Address code review feedback on cover precision guard
- Capitalize guard helper comments in Deposit and Withdraw transactors
- Fix canApplyToBrokerCover signature to use SLE::const_ref (matches header)
- Add alice balance invariant check to Withdraw no-op test case
- Add MPT positive test: verify 1-unit MPT passes the precision guard
2026-05-14 15:04:37 +02:00
Vito
c327fc1ee2 fix: Reject sub-ULP cover amounts with tecPRECISION_LOSS (fixCleanup3_2_0)
Add STAmount::isZeroAtScale() and canApplyToBrokerCover() to detect
amounts that round to zero at sfCoverAvailable's precision scale, then
call the guard in LoanBrokerCoverDeposit, LoanBrokerCoverWithdraw, and
LoanBrokerCoverClawback preclaim. Without the guard a sub-ULP deposit,
withdrawal, or clawback would silently succeed while moving no funds.
2026-05-14 14:44:16 +02:00
Vito
1297f0bc52 test: Add LoanBroker cover precision guard tests
Exercises the canApplyToBrokerCover guard introduced in
LoanBrokerCoverDeposit, LoanBrokerCoverWithdraw, and
LoanBrokerCoverClawback: three test cases (Deposit, Withdraw, Clawback)
run twice via runTestCases(all_) and runTestCases(all_ - fixCleanup3_2_0).

With the amendment on, a sub-ULP IOU amount (1e-16, which rounds to zero
at the sfCoverAvailable scale of 10 IOU) is rejected with
tecPRECISION_LOSS. Without it, the transaction silently succeeds and
sfCoverAvailable is verified to be unchanged.
2026-05-14 14:43:38 +02:00
9 changed files with 492 additions and 3 deletions

View File

@@ -4,8 +4,39 @@
#include <xrpl/protocol/Rules.h>
#include <xrpl/protocol/st.h>
#include <string_view>
namespace xrpl {
/**
* Broker cover preclaim precision guard (fixCleanup3_2_0).
*
* Prevents a "silent sub-ULP no-op" where a deposit, withdrawal, or clawback
* amount is so small that it rounds to zero at `sfCoverAvailable`'s scale.
* Without this guard, both the pseudo trust-line and `sfCoverAvailable` would
* identically absorb the rounded zero, resulting in a successful transaction
* (tesSUCCESS) where no funds actually moved.
*
* @param view Apply view (rules used for amendment gating).
* @param sleBroker The loan broker SLE (read-only).
* @param vaultAsset The underlying vault asset (the broker's cover asset).
* @param amount The effective subtraction/addition amount.
* @param j Journal for logging.
* @param logPrefix Transactor name for log diagnostics.
*
* @return `tecPRECISION_LOSS` if the request rounds to zero at cover scale.
* `tesSUCCESS` if the amendment is disabled, the amount is true zero,
* or the request is safely supra-ULP.
*/
[[nodiscard]] TER
canApplyToBrokerCover(
ReadView const& view,
SLE::const_ref sleBroker,
Asset const& vaultAsset,
STAmount const& amount,
beast::Journal j,
std::string_view logPrefix);
// Lending protocol has dependencies, so capture them here.
bool
checkLendingProtocolDependencies(Rules const& rules, STTx const& tx);

View File

@@ -184,6 +184,24 @@ public:
[[nodiscard]] STAmount const&
value() const noexcept;
/**
* Checks if this amount evaluates to zero when constrained to a specific
* accounting scale.
*
* For XRP and MPT `roundToScale` is a no-op, returns true only when the amount itself is zero.
* The `scale` argument is ignored in that case.
* For IOU, the amount is rounded to the given scale (using the current rounding mode)
* and the result is checked for zero; if `scale <= exponent()`, `roundToScale` short-circuits
* and returns the value unchanged, so this returns false for any non-zero amount.
*
* @param scale The target accounting scale to evaluate against.
* @return `true` if this amount rounds to zero at the given scale, `false` otherwise.
*
* @see roundToScale
*/
[[nodiscard]] bool
isZeroAtScale(int scale) const;
//--------------------------------------------------------------------------
//
// Operators

View File

@@ -8,6 +8,7 @@
#include <xrpl/beast/utility/Zero.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/ledger/ApplyView.h>
#include <xrpl/ledger/ReadView.h>
#include <xrpl/ledger/View.h>
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/Feature.h>
@@ -24,10 +25,41 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <string_view>
#include <utility>
namespace xrpl {
[[nodiscard]] TER
canApplyToBrokerCover(
ReadView const& view,
SLE::const_ref sleBroker,
Asset const& vaultAsset,
STAmount const& amount,
beast::Journal j,
std::string_view logPrefix)
{
XRPL_ASSERT(
sleBroker && sleBroker->getType() == ltLOAN_BROKER,
"xrpl::canApplyToBrokerCover : valid LoanBroker sle");
XRPL_ASSERT(
vaultAsset == amount.asset() && amount > beast::kZero,
"xrpl::canApplyToBrokerCover : valid amount for asset");
if (!view.rules().enabled(fixCleanup3_2_0))
return tesSUCCESS;
int const coverScale = scale(sleBroker->at(sfCoverAvailable), vaultAsset);
if (amount.isZeroAtScale(coverScale))
{
JLOG(j.warn()) << logPrefix << ": amount " << amount.getFullText()
<< " rounds to zero at cover scale " << coverScale;
return tecPRECISION_LOSS;
}
return tesSUCCESS;
}
bool
checkLendingProtocolDependencies(Rules const& rules, STTx const& tx)
{

View File

@@ -1738,4 +1738,9 @@ divRoundStrict(STAmount const& num, STAmount const& den, Asset const& asset, boo
return divRoundImpl<NumberRoundModeGuard>(num, den, asset, roundUp);
}
[[nodiscard]] bool
STAmount::isZeroAtScale(int scale) const
{
return roundToScale(*this, scale).signum() == 0;
}
} // namespace xrpl

View File

@@ -291,6 +291,10 @@ LoanBrokerCoverClawback::preclaim(PreclaimContext const& ctx)
}
STAmount const& clawAmount = *findClawAmount;
if (auto const ret = canApplyToBrokerCover(
ctx.view, sleBroker, vaultAsset, clawAmount, ctx.j, "LoanBrokerCoverClawback"))
return ret;
// Explicitly check the balance of the trust line / MPT to make sure the
// balance is actually there. It should always match `sfCoverAvailable`, so
// if there isn't, this is an internal error.

View File

@@ -1,9 +1,11 @@
#include <xrpl/tx/transactors/lending/LoanBrokerCoverDeposit.h>
#include <xrpl/basics/Log.h>
#include <xrpl/basics/Number.h>
#include <xrpl/beast/utility/Zero.h>
#include <xrpl/ledger/helpers/LendingHelpers.h>
#include <xrpl/ledger/helpers/TokenHelpers.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/SField.h>
#include <xrpl/protocol/STAmount.h>
@@ -106,8 +108,6 @@ LoanBrokerCoverDeposit::doApply()
auto const& tx = ctx_.tx;
auto const brokerID = tx[sfLoanBrokerID];
auto const amount = tx[sfAmount];
auto broker = view().peek(keylet::loanbroker(brokerID));
if (!broker)
return tecINTERNAL; // LCOV_EXCL_LINE
@@ -117,9 +117,25 @@ LoanBrokerCoverDeposit::doApply()
return tecINTERNAL; // LCOV_EXCL_LINE
auto const vaultAsset = vault->at(sfAsset);
auto const brokerPseudoID = broker->at(sfAccount);
bool const fix320Enabled = view().rules().enabled(fixCleanup3_2_0);
auto const amount = [&]() -> STAmount {
if (!fix320Enabled)
return tx[sfAmount];
return roundToScale(
tx[sfAmount],
scale(broker->at(sfCoverAvailable), vaultAsset),
Number::RoundingMode::Downward);
}();
if (fix320Enabled && amount == beast::kZero)
{
JLOG(ctx_.journal.warn()) << "LoanBrokerCoverDeposit: deposit amount: " << tx[sfAmount]
<< " is zero at loan broker scale";
return tecPRECISION_LOSS;
}
// Transfer assets from depositor to pseudo-account.
if (auto ter =
accountSend(view(), accountID_, brokerPseudoID, amount, j_, WaiveTransferFee::Yes))

View File

@@ -93,6 +93,11 @@ LoanBrokerCoverWithdraw::preclaim(PreclaimContext const& ctx)
if (amount.asset() != vaultAsset)
return tecWRONG_ASSET;
// Helper handles both IOU and MPT correctly without explicit branching.
if (auto const ret = canApplyToBrokerCover(
ctx.view, sleBroker, vaultAsset, amount, ctx.j, "LoanBrokerCoverWithdraw"))
return ret;
// The broker's pseudo-account is the source of funds.
auto const pseudoAccountID = sleBroker->at(sfAccount);
// Cannot transfer a non-transferable Asset

View File

@@ -55,6 +55,7 @@
#include <optional>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
namespace xrpl::test {
@@ -1823,6 +1824,287 @@ class LoanBroker_test : public beast::unit_test::Suite
testRIPD4274MPT();
}
// Exercises canApplyToBrokerCover (fixCleanup3_2_0): a deposit, withdraw,
// or clawback whose amount rounds to zero at sfCoverAvailable's precision
// scale must be rejected with tecPRECISION_LOSS once the amendment is on,
// and must silently succeed without changing sfCoverAvailable when off.
void
testCoverPrecisionGuard()
{
using namespace jtx;
using namespace loanBroker;
Account const issuer{"issuer"};
Account const alice{"alice"};
// sfCoverAvailable = 10 IOU → STAmount exponent = -14.
// Anything < 5e-15 rounds to zero at that scale.
// 1e-16 is the representative sub-ULP probe amount.
// Shared setup: funds accounts, creates a vault + broker with 10 IOU
// cover, and returns {brokerKeylet, iou}.
auto const setup = [&](Env& env) -> std::pair<Keylet, PrettyAsset> {
Vault const vault{env};
env.fund(XRP(100'000), issuer, alice);
env.close();
env(fset(issuer, asfAllowTrustLineClawback));
env.close();
PrettyAsset const iou = issuer["IOU"];
env(trust(alice, iou(1'000'000)));
env.close();
env(pay(issuer, alice, iou(1'000)));
env.close();
auto [createTx, vaultKeylet] = vault.create({.owner = alice, .asset = iou});
env(createTx);
env.close();
auto const brokerKeylet = keylet::loanbroker(alice.id(), env.seq(alice));
env(set(alice, vaultKeylet.key));
env.close();
env(coverDeposit(alice, brokerKeylet.key, iou(10)));
env.close();
return {brokerKeylet, iou};
};
auto runTestCases = [&](FeatureBitset features) {
TER const expected =
features[fixCleanup3_2_0] ? TER{tecPRECISION_LOSS} : TER{tesSUCCESS};
{
testcase("Cover precision guard: Deposit zero-at-scale");
Env env{*this, features};
auto const [brokerKeylet, iou] = setup(env);
PrettyAmount const subUlpAmt = iou(Number{1, -16});
auto const coverBefore = env.le(brokerKeylet)->at(sfCoverAvailable);
env(coverDeposit(alice, brokerKeylet.key, subUlpAmt), Ter(expected));
env.close();
if (expected == tesSUCCESS)
{
if (auto const broker = env.le(brokerKeylet); BEAST_EXPECT(broker))
BEAST_EXPECT(broker->at(sfCoverAvailable) == coverBefore);
}
}
{
testcase("Cover precision guard: Deposit rounds down");
// Both cases succeed; post-fix the amount is rounded DOWN to
// cover scale first, so the delta differs from pre-fix
// Input: 1.8e-14 IOU (sub-scale at cover scale -14)
// Pre-fix: 10 + 1.8e-14 → round-to-nearest →
// 10.00000000000002 → delta 2e-14
// Post-fix: roundToScale(1.8e-14, -14, Downward) = 1e-14;
// 10 + 1e-14 = 10.00000000000001 → delta 1e-14
Env env{*this, features};
auto const [brokerKeylet, iou] = setup(env);
PrettyAmount const subUlpAmt = iou(Number{18, -15});
auto const coverBefore = env.le(brokerKeylet)->at(sfCoverAvailable);
env(coverDeposit(alice, brokerKeylet.key, subUlpAmt), Ter(tesSUCCESS));
env.close();
auto const brokerAfter = env.le(brokerKeylet);
if (!BEAST_EXPECT(brokerAfter))
return;
Number const delta = features[fixCleanup3_2_0] ? Number{1, -14} : Number{2, -14};
BEAST_EXPECT(brokerAfter->at(sfCoverAvailable) - coverBefore == delta);
}
{
testcase("Cover precision guard: Withdraw");
Env env{*this, features};
auto const [brokerKeylet, iou] = setup(env);
PrettyAmount const subUlpAmt = iou(Number{1, -16});
auto const coverBefore = env.le(brokerKeylet)->at(sfCoverAvailable);
auto const aliceBalanceBefore = env.balance(alice, iou);
env(coverWithdraw(alice, brokerKeylet.key, subUlpAmt), Ter(expected));
env.close();
if (expected == tesSUCCESS)
{
if (auto const broker = env.le(brokerKeylet); BEAST_EXPECT(broker))
BEAST_EXPECT(broker->at(sfCoverAvailable) == coverBefore);
BEAST_EXPECT(env.balance(alice, iou) == aliceBalanceBefore);
}
}
{
testcase("Cover precision guard: Clawback");
Env env{*this, features};
auto const [brokerKeylet, iou] = setup(env);
PrettyAmount const subUlpAmt = iou(Number{1, -16});
auto const coverBefore = env.le(brokerKeylet)->at(sfCoverAvailable);
env(coverClawback(issuer),
kLoanBrokerId(brokerKeylet.key),
kAmount(subUlpAmt),
Ter(expected));
env.close();
if (expected == tesSUCCESS)
{
if (auto const broker = env.le(brokerKeylet); BEAST_EXPECT(broker))
BEAST_EXPECT(broker->at(sfCoverAvailable) == coverBefore);
}
}
};
runTestCases(all_);
runTestCases(all_ - fixCleanup3_2_0);
// ============================================================
// Asymmetry exploit (TER verdict): Deposit uses Downward rounding
// in doApply (rejects when amount rounds to zero); Withdraw and
// Clawback use ToNearest via canApplyToBrokerCover in preclaim
// (rejects when amount is closer to zero than to one ULP).
//
// With sfCoverAvailable = 10 IOU, cover scale = -14:
// half-ULP = 5e-15, one ULP = 1e-14.
//
// Probe = 6e-15 (above half-ULP, below one ULP):
// Deposit Downward(6e-15, -14) = 0 → tecPRECISION_LOSS
// Withdraw isZeroAtScale → ToNearest → 1e-14 ≠ 0 → tesSUCCESS
// Clawback isZeroAtScale → ToNearest → 1e-14 ≠ 0 → tesSUCCESS
//
// Same amendment, same input, opposite TER across transactors.
// Note: production doApply for Withdraw/Clawback consumes the
// ORIGINAL 6e-15 (no roundToScale); the actual on-ledger delta is
// 6e-15 because STAmount IOU has 16-digit mantissa precision and
// 10 - 6e-15 = 9.999999999999994 fits exactly without rounding.
// ============================================================
{
testcase("Cover precision asymmetry: Deposit rejects but Withdraw/Clawback accept");
Env env{*this, all_};
auto const [brokerKeylet, iou] = setup(env);
// 6e-15: above half-ULP, below one ULP at cover scale -14.
PrettyAmount const probe = iou(Number{6, -15});
auto const coverInitial = env.le(brokerKeylet)->at(sfCoverAvailable);
auto const aliceBalInitial = env.balance(alice, iou);
// (1) Deposit rejects under amendment ON.
env(coverDeposit(alice, brokerKeylet.key, probe), Ter(tecPRECISION_LOSS));
env.close();
BEAST_EXPECT(env.le(brokerKeylet)->at(sfCoverAvailable) == coverInitial);
// (2) Withdraw on identical input under same amendment: accepts.
// Asymmetry: Deposit rejected this exact input, Withdraw
// passes the guard and mutates state.
env(coverWithdraw(alice, brokerKeylet.key, probe), Ter(tesSUCCESS));
env.close();
auto const coverAfterWithdraw = env.le(brokerKeylet)->at(sfCoverAvailable);
// doApply does `sfCoverAvailable -= amount` on the ORIGINAL
// 6e-15. STAmount subtraction at 16-digit mantissa is exact
// here, so the delta equals the input.
BEAST_EXPECT((coverInitial - coverAfterWithdraw == Number{6, -15}));
// (3) Clawback on identical input under same amendment: accepts.
env(coverClawback(issuer),
kLoanBrokerId(brokerKeylet.key),
kAmount(probe),
Ter(tesSUCCESS));
env.close();
auto const coverAfterClawback = env.le(brokerKeylet)->at(sfCoverAvailable);
BEAST_EXPECT((coverAfterWithdraw - coverAfterClawback == Number{6, -15}));
// Cumulative drift after Withdraw + Clawback.
BEAST_EXPECT((coverInitial - coverAfterClawback == Number{12, -15}));
// Alice's IOU balance starts at ~990 (1000 minted - 10 deposited
// as cover). Withdraw transfers 6e-15 from broker pseudo to
// alice; Clawback transfers 6e-15 from broker pseudo to issuer
// (NOT from alice). Adding 6e-15 to a 990 STAmount underflows
// 16-digit precision (990 + 6e-15 → renormalizes back to 990),
// so alice's stored balance is unchanged.
BEAST_EXPECT(env.balance(alice, iou) == aliceBalInitial);
}
// ============================================================
// Asymmetry exploit (on-ledger delta): identical 1.8e-14 input
// under amendment ON.
//
// Deposit: Downward pre-rounds 1.8e-14 → 1e-14 in doApply, then
// sfCoverAvailable += 1e-14. delta = +1e-14.
// Withdraw: guard passes (ToNearest(1.8e-14) = 2e-14 ≠ 0); doApply
// does `sfCoverAvailable -= 1.8e-14` on the ORIGINAL.
// STAmount IOU 16-digit mantissa: 10 - 1.8e-14 =
// 9.999999999999982 (exact). delta = 1.8e-14.
//
// Same input, same amendment, 1.8x difference in effective
// magnitude (1e-14 vs 1.8e-14). Trust-line vs sfCoverAvailable
// invariant cannot be guaranteed under non-symmetric rounding.
// ============================================================
{
testcase("Cover precision asymmetry: Deposit and Withdraw deltas diverge");
Env env{*this, all_};
auto const [brokerKeylet, iou] = setup(env);
PrettyAmount const probe = iou(Number{18, -15});
auto const coverPreDeposit = env.le(brokerKeylet)->at(sfCoverAvailable);
env(coverDeposit(alice, brokerKeylet.key, probe), Ter(tesSUCCESS));
env.close();
auto const coverPostDeposit = env.le(brokerKeylet)->at(sfCoverAvailable);
Number const depositDelta = coverPostDeposit - coverPreDeposit;
env(coverWithdraw(alice, brokerKeylet.key, probe), Ter(tesSUCCESS));
env.close();
auto const coverPostWithdraw = env.le(brokerKeylet)->at(sfCoverAvailable);
Number const withdrawDelta = coverPostDeposit - coverPostWithdraw;
// Deposit Downward pre-round → +1e-14
// Withdraw original amount, no round → -1.8e-14
BEAST_EXPECT((depositDelta == Number{1, -14}));
BEAST_EXPECT((withdrawDelta == Number{18, -15}));
BEAST_EXPECT(depositDelta != withdrawDelta);
}
// MPT amounts are integers; scale is 0; the guard never rejects a
// positive integer amount. Verify all three callsites pass with amendment on.
{
testcase("Cover precision guard: MPT min amount passes");
Env env{*this, all_};
env.fund(XRP(100'000), issuer, alice);
env.close();
MPTTester mptt{env, issuer, kMptInitNoFund};
mptt.create({.flags = tfMPTCanClawback | tfMPTCanTransfer | tfMPTCanLock});
env.close();
PrettyAsset const mptAsset = mptt["MPT"];
mptt.authorize({.account = alice});
env.close();
env(pay(issuer, alice, mptAsset(100)));
env.close();
Vault const vault{env};
auto [createTx, vaultKeylet] = vault.create({.owner = alice, .asset = mptAsset});
env(createTx);
env.close();
auto const brokerKeylet = keylet::loanbroker(alice.id(), env.seq(alice));
env(set(alice, vaultKeylet.key));
env.close();
env(coverDeposit(alice, brokerKeylet.key, mptAsset(10)));
env.close();
env(coverDeposit(alice, brokerKeylet.key, mptAsset(1)), Ter(tesSUCCESS));
env.close();
env(coverWithdraw(alice, brokerKeylet.key, mptAsset(1)), Ter(tesSUCCESS));
env.close();
env(coverClawback(issuer),
kLoanBrokerId(brokerKeylet.key),
kAmount(mptAsset(1)),
Ter(tesSUCCESS));
env.close();
}
}
public:
void
run() override
@@ -1843,6 +2125,7 @@ public:
testAmB06VaultFreezeCheckMissing();
testRIPD4274();
testCoverPrecisionGuard();
// TODO: Write clawback failure tests with an issuer / MPT that doesn't
// have the right flags set.

View File

@@ -1205,6 +1205,100 @@ public:
//--------------------------------------------------------------------------
void
testIsZeroAtScale()
{
testcase("isZeroAtScale");
Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
// IOU: 10 IOU — mantissa = kMinValue (10^15), exponent = -14.
// One ULP at this scale is 10^-14; half-ULP is 5*10^-15.
{
STAmount const ref{usd, STAmount::kMinValue, -14};
int const refScale = ref.exponent(); // -14
BEAST_EXPECT(refScale == -14);
// Zero rounds to zero at any scale.
STAmount const iouZero{usd, 0};
BEAST_EXPECT(iouZero.isZeroAtScale(refScale));
// Sub-ULP: 1e-16 IOU (mantissa = kMinValue, exponent = -31).
// Far below half-ULP → rounds to zero.
STAmount const subUlp{usd, STAmount::kMinValue, -31};
BEAST_EXPECT(subUlp.isZeroAtScale(refScale));
// One ULP: 1e-14 IOU (mantissa = kMinValue, exponent = -29).
// Exactly the smallest representable unit at refScale → not zero.
STAmount const oneUlp{usd, STAmount::kMinValue, -29};
BEAST_EXPECT(!oneUlp.isZeroAtScale(refScale));
// The reference value itself: exponent == scale → returned
// unchanged → not zero.
BEAST_EXPECT(!ref.isZeroAtScale(refScale));
// A much larger value: certainly not zero at this scale.
STAmount const large{usd, STAmount::kMinValue, 0}; // 1e15 IOU
BEAST_EXPECT(!large.isZeroAtScale(refScale));
// When scale equals the value's own exponent, roundToScale
// short-circuits and returns the value unchanged.
BEAST_EXPECT(!subUlp.isZeroAtScale(subUlp.exponent()));
BEAST_EXPECT(!oneUlp.isZeroAtScale(oneUlp.exponent()));
// Half-ULP boundary. roundToScale forms (value + ref) - ref
// where ref = 10 IOU has mantissa 1e15 (LSB 0, even).
// Number's default rounding is to-nearest-even, so an exact
// half-ULP tie rounds toward the even-LSB neighbour — the
// reference itself — and the round-trip result is zero.
// Just below half-ULP rounds the same way; just above
// clears half-ULP and bumps the mantissa to 1e15 + 1.
STAmount const justBelowHalf{usd, STAmount::kMinValue * 4, -30};
BEAST_EXPECT(justBelowHalf.isZeroAtScale(refScale));
STAmount const halfUlp{usd, STAmount::kMinValue * 5, -30};
BEAST_EXPECT(halfUlp.isZeroAtScale(refScale));
STAmount const justAboveHalf{usd, STAmount::kMinValue * 6, -30};
BEAST_EXPECT(!justAboveHalf.isZeroAtScale(refScale));
// Large magnitude gap: dust value far below an enormous scale.
// 1e-80 with scale +15 — the value vanishes utterly.
STAmount const dust{usd, STAmount::kMinValue, -95};
BEAST_EXPECT(dust.isZeroAtScale(15));
// Negative values mirror positive behaviour.
STAmount const negSubUlp{usd, STAmount::kMinValue, -31, true};
BEAST_EXPECT(negSubUlp.isZeroAtScale(refScale));
STAmount const negOneUlp{usd, STAmount::kMinValue, -29, true};
BEAST_EXPECT(!negOneUlp.isZeroAtScale(refScale));
}
// XRP is integral — roundToScale short-circuits, value is preserved.
{
STAmount const xrp{XRPAmount{1}};
BEAST_EXPECT(!xrp.isZeroAtScale(-14));
BEAST_EXPECT(!xrp.isZeroAtScale(0));
STAmount const xrpZero{XRPAmount{0}};
BEAST_EXPECT(xrpZero.isZeroAtScale(-14));
}
// MPT is integral — same short-circuit behaviour as XRP.
{
MPTIssue const mpt{makeMptID(1, AccountID(0x4985601))};
STAmount const mptAmt{mpt, 1};
BEAST_EXPECT(!mptAmt.isZeroAtScale(0));
BEAST_EXPECT(!mptAmt.isZeroAtScale(-14));
STAmount const mptZero{mpt, 0};
BEAST_EXPECT(mptZero.isZeroAtScale(0));
}
}
//--------------------------------------------------------------------------
void
run() override
{
@@ -1223,6 +1317,7 @@ public:
testCanSubtractXRP();
testCanSubtractIOU();
testCanSubtractMPT();
testIsZeroAtScale();
}
};