mirror of
https://github.com/XRPLF/rippled.git
synced 2026-06-04 09:16:47 +00:00
Compare commits
13 Commits
dangell7/c
...
pratik/cov
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11560fb51b | ||
|
|
4e907d9662 | ||
|
|
5f6bb93b5c | ||
|
|
2b4d8ef3a2 | ||
|
|
13358f5042 | ||
|
|
e1946278f9 | ||
|
|
c4115ee4be | ||
|
|
e2ba7a728b | ||
|
|
a5db99fb96 | ||
|
|
20678e6bc4 | ||
|
|
42eadf0416 | ||
|
|
c327fc1ee2 | ||
|
|
1297f0bc52 |
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user