xrpld
Loading...
Searching...
No Matches
VaultHelpers_test.cpp
1#include <test/jtx/Account.h>
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/unit_test/suite.h>
6#include <xrpl/ledger/helpers/VaultHelpers.h>
7#include <xrpl/protocol/Asset.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/Issue.h>
10#include <xrpl/protocol/MPTIssue.h>
11#include <xrpl/protocol/Protocol.h>
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/STAmount.h>
14#include <xrpl/protocol/STIssue.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STNumber.h> // IWYU pragma: keep
17#include <xrpl/protocol/STTakesAsset.h>
18#include <xrpl/protocol/TER.h>
19#include <xrpl/protocol/UintTypes.h>
20
21#include <algorithm>
22#include <array>
23#include <cstddef>
24#include <cstdint>
25#include <expected>
26#include <memory>
27#include <optional>
28#include <string>
29#include <utility>
30
31namespace xrpl {
32
33// True unit test of clampToAssetsTotalScale. The function under test reads
34// only fields from the vault SLE and never touches a ledger view or Rules, so
35// a bare in-memory ltVAULT SLE is enough; there is no jtx::Env and no
36// transaction submitted anywhere in this file.
37//
38// Number regime: this suite relies on the default thread_local Number
39// mantissa range, which src/libxrpl/basics/Number.cpp initializes to
40// Large330 (19-digit mantissa, post-fixCleanup3_3_0 cusp-rounding behavior):
41//
42// thread_local std::reference_wrapper<MantissaRange const> Number::kRange =
43// MantissaRange::Access::mantissaRange(MantissaRange::MantissaScale::Large330);
44//
45// Unlike transaction processing, this test never constructs a ledger Rules
46// object, so STAmount::operator=(Number const&) always takes its
47// !getCurrentTransactionRules() branch and calls fromNumber, independent
48// of amendment state. testProbeLarge330Regime() below asserts directly on a
49// value that only round-trips exactly under Large330, pinning the regime
50// rather than merely asserting it by comment.
52{
53private:
54 // A single row of the clampToAssetsTotalScale table. assetsTotal and
55 // delta must already be genuine, on-grid STAmount values for asset.
56 struct Case
57 {
58 char const* name = nullptr;
61 std::optional<Number> expected; // nullopt means tecPRECISION_LOSS
62 };
63
64 // Builds a bare ltVAULT SLE. With fixedScale absent, sfLEVersion stays
65 // absent too, preserving the pre-V1.2 Legacy behavior exercised by the
66 // existing clamp table. With fixedScale set, the SLE is stamped
67 // FixedPrecision with that Scale, exercising clampToAssetsTotalScale's
68 // roundToPosteriorVaultScale branch instead.
71 Asset const& asset,
72 Number const& assetsTotal,
73 std::optional<std::uint8_t> fixedScale = std::nullopt)
74 {
76 vault->setFieldIssue(sfAsset, STIssue{sfAsset, asset});
77 vault->at(sfAssetsTotal) = assetsTotal;
78 associateAsset(*vault, asset);
79 if (fixedScale)
80 {
81 vault->at(sfLEVersion) = std::to_underlying(VaultVersion::FixedPrecision);
82 vault->at(sfScale) = *fixedScale;
83 // FixedPrecision derives its total from these two fields.
84 vault->at(sfAssetsAvailable) = assetsTotal;
85 vault->at(sfAssetsDeployed) = Number{0};
86 }
87 return vault;
88 }
89
90 // Runs every case in cases against asset, once per ambient rounding
91 // mode. The function must give the same answer under all four modes,
92 // and its answer must match the hand-derived expected value.
93 // fixedScale, when set, builds a FixedPrecision vault at that Scale
94 // instead of the default Legacy vault.
95 template <std::size_t N>
96 void
98 Asset const& asset,
99 std::array<Case, N> const& cases,
100 std::optional<std::uint8_t> fixedScale = std::nullopt,
101 bool viaClampVaultOutflow = false)
102 {
108 };
109
110 for (auto const& c : cases)
111 {
112 testcase(c.name);
113
114 auto const vault = makeVault(asset, c.assetsTotal, fixedScale);
115 BEAST_EXPECTS(
116 Number(vault->at(sfAssetsTotal)) == c.assetsTotal,
117 std::string(c.name) +
118 ": assetsTotal is not a genuine on-grid STAmount value (associateAsset "
119 "changed it)");
120
121 STAmount const delta{asset, c.delta};
122 BEAST_EXPECTS(
123 Number(delta) == c.delta,
124 std::string(c.name) + ": delta is not a genuine on-grid STAmount value");
125
127 for (auto const mode : modes)
128 {
129 NumberRoundModeGuard const rg(mode);
130 auto const result = viaClampVaultOutflow ? clampVaultOutflow(vault, delta)
131 : clampToAssetsTotalScale(vault, delta);
132
133 // The function must be insensitive to the caller's ambient
134 // rounding mode: every mode must agree with the first one
135 // tried.
136 if (!reference)
137 {
138 reference = result;
139 }
140 else
141 {
142 BEAST_EXPECTS(
143 result.has_value() == reference->has_value(),
144 std::string(c.name) + ": result depends on ambient rounding mode");
145 if (result.has_value() && reference->has_value())
146 {
147 BEAST_EXPECTS(
148 *result == **reference,
149 std::string(c.name) + ": value depends on ambient rounding mode");
150 }
151 else if (!result.has_value() && !reference->has_value())
152 {
153 BEAST_EXPECTS(
154 result.error() == reference->error(),
155 std::string(c.name) + ": error depends on ambient rounding mode");
156 }
157 }
158
159 if (!c.expected)
160 {
161 BEAST_EXPECTS(
162 !result.has_value(),
163 std::string(c.name) + ": expected tecPRECISION_LOSS, got success value " +
164 (result.has_value() ? result->getText() : std::string()));
165 if (!result.has_value())
166 {
167 BEAST_EXPECTS(
168 result.error() == tecPRECISION_LOSS,
169 std::string(c.name) + ": expected tecPRECISION_LOSS, got " +
170 transToken(result.error()));
171 }
172 continue;
173 }
174
175 // c.expected is guaranteed set here: the !c.expected case
176 // above always continues to the next loop iteration.
177 STAmount const expected{
178 asset,
179 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
180 *c.expected,
181 };
182 if (!BEAST_EXPECTS(
183 result.has_value(),
184 std::string(c.name) + ": expected success (" + expected.getText() +
185 "), got " + transToken(result.error())))
186 {
187 continue;
188 }
189
190 BEAST_EXPECTS(
191 *result == expected,
192 std::string(c.name) + ": expected " + expected.getText() + ", got " +
193 result->getText());
194
195 // The result must always be positive...
196 BEAST_EXPECT(Number(*result) > Number{0});
197
198 // ...and never larger in magnitude than the requested delta.
199 BEAST_EXPECT(abs(Number(*result)) <= abs(c.delta));
200
201 // For IOU rows, re-flooring the result on the posterior grid
202 // must be a no-op: the result is already exactly
203 // representable at that scale.
204 //
205 // For debits this holds directly at postScale, because the
206 // result IS roundToScale(magnitude, postScale, Downward) by
207 // construction. For credits the result is
208 // roundedPosterior - assetsTotal, where roundedPosterior
209 // sits exactly on the postScale grid but assetsTotal sits on
210 // its own (possibly finer) natural grid; the difference of a
211 // multiple of 10^postScale and a multiple of 10^assetsScale
212 // is only guaranteed exact at the FINER of the two scales.
213 // Row 7 below ("overcredit fix across a scale boundary") is
214 // exactly this case: assetsTotal's own scale (-15) is finer
215 // than postScale (-14), so checking exactness at postScale
216 // alone fails even though the implementation is correct.
217 if (!asset.integral())
218 {
219 bool const isDebit = c.delta.mantissa() < 0;
220 Number const posterior =
221 isDebit ? c.assetsTotal - Number(*result) : c.assetsTotal + Number(*result);
222 int const postScale = scale(posterior, asset);
223 int const checkScale =
224 isDebit ? postScale : std::min(postScale, scale(c.assetsTotal, asset));
225 STAmount const reFloored =
226 roundToScale(*result, checkScale, Number::RoundingMode::Downward);
227 BEAST_EXPECTS(
228 reFloored == *result,
229 std::string(c.name) + ": result " + result->getText() +
230 " is not exact on the posterior grid (scale " +
231 std::to_string(checkScale) + ")");
232 }
233 }
234 }
235 }
236
237 // Pins the Number mantissa regime this suite relies on. Under Large330,
238 // a 19-digit mantissa (max 10^19-1) is exact where a legacy 16-digit
239 // ("Small", max 10^16-1) regime would have to round it down to 16
240 // significant digits, changing both mantissa and exponent.
241 void
243 {
244 testcase("probe: default Number regime is Large330 (19-digit mantissa)");
245
247
248 // std::numeric_limits<std::int64_t>::max(), 19 significant digits.
249 // This is already inside Large330's [10^18, 10^19-1] range, so
250 // constructing it is a no-op; under "Small" it would have to lose
251 // its low 3 digits.
252 Number const probe{9'223'372'036'854'775'807LL, 0};
253 BEAST_EXPECT(probe.mantissa() == 9'223'372'036'854'775'807LL);
254 BEAST_EXPECT(probe.exponent() == 0);
255 }
256
257 // -------------------------------------------------------------------
258 // IOU debits (delta negative).
259 // -------------------------------------------------------------------
260 void
262 {
263 std::array<Case, 5> const cases{
264 Case{
265 // T = 1000000.000000005, delta = -1e-9.
266 // Posterior = 1000000.000000004, still 16 significant
267 // digits at exponent -9 (no rounding, no decade change).
268 // postScale = -9. magnitude 1e-9 has its own exponent -24
269 // (finer than -9), so it must be actually floored: 1e-9 is
270 // exactly 1 ULP at scale -9, so flooring is a no-op.
271 .name = "IOU debit: on-grid, same decade",
272 .assetsTotal = Number{1'000'000'000'000'005LL, -9},
273 .delta = Number{-1, -9},
274 .expected = Number{1, -9},
275 },
276 Case{
277 // T = 1000000, delta = -7.3e-10.
278 // Posterior = 999999.99999999927 exactly (17 significant
279 // digits: 15 nines, then "27"). Rounding to 16 digits
280 // (ToNearest) rounds the trailing "...92.7" up to
281 // "...93", giving mantissa 9999999999999993 at exponent
282 // -10 -- postScale = -10, ONE DIGIT FINER than the naive
283 // "posterior stays in T's decade at -9" guess, because
284 // subtracting anything positive from an exact power-of-ten
285 // total necessarily drops into the next lower decade
286 // (1000000 has 7 integer digits, 999999.x has 6).
287 // At scale -10 the ULP is 1e-10, and floor(7.3) = 7, so
288 // the debit is NOT sub-ULP: it floors to 7e-10, not to
289 // zero. See discrepancy note in the report.
290 .name = "IOU debit: sub-ULP at the naive scale, but not at the true postScale",
291 .assetsTotal = Number{1'000'000, 0},
292 .delta = Number{-73, -11},
293 .expected = Number{7, -10},
294 },
295 Case{
296 // T = 1000000, delta = -5.3e-9.
297 // Posterior = 999999.9999999947 exactly -- this needs only
298 // 16 significant digits (14 nines, then "47"), so it is
299 // exactly representable with NO rounding at exponent -10.
300 // postScale = -10 (again one digit finer than T's own -9,
301 // for the same power-of-ten-boundary reason as the row
302 // above). At that grid 5.3e-9 is exactly 53 ULPs (integer),
303 // so it floors to itself, unchanged.
304 .name = "IOU debit: exact at the true (finer) postScale",
305 .assetsTotal = Number{1'000'000, 0},
306 .delta = Number{-53, -10},
307 .expected = Number{53, -10},
308 },
309 Case{
310 // T = 1.000000000000000, delta = -7.3e-16.
311 // Posterior = 0.99999999999999927 exactly (17 significant
312 // digits: 15 nines then "27"). Rounding to 16 digits
313 // (ToNearest) gives mantissa 9999999999999993 at exponent
314 // -16 -- postScale = -16. At that grid, 7.3e-16 is 7.3
315 // ULPs (not integral), so it floors to 7e-16, not to
316 // itself. See discrepancy note in the report.
317 .name = "IOU debit: decade-crossing debit, floored (not exact) at finer grid",
318 .assetsTotal = Number{1, 0},
319 .delta = Number{-73, -17},
320 .expected = Number{7, -16},
321 },
322 Case{
323 // T = 1000000, delta = -999999.9999999999 (9.999999999999999e5).
324 // Posterior = 0.0000000001 = 1e-10 exactly. postScale is
325 // the exponent of 1e-10 as a canonical STAmount, i.e. -25 --
326 // far finer than the magnitude's own exponent (-10).
327 // roundToScale short-circuits ("value.exponent() >= scale")
328 // and returns the magnitude unchanged.
329 .name = "IOU debit: near-total debit, unchanged (finer postScale than magnitude)",
330 .assetsTotal = Number{1'000'000, 0},
331 .delta = Number{-9'999'999'999'999'999LL, -10},
332 .expected = Number{9'999'999'999'999'999LL, -10},
333 },
334 };
335
336 runCases(iou, cases);
337 }
338
339 // -------------------------------------------------------------------
340 // IOU credits (delta positive).
341 // -------------------------------------------------------------------
342 void
344 {
345 std::array<Case, 6> const cases{
346 Case{
347 // T = 1000000, delta = +2e-9. Posterior = 1000000.000000002,
348 // exactly 16 significant digits at exponent -9
349 // (postScale = -9, unchanged from T -- addition never
350 // crosses below the 1e6 boundary the way subtraction does).
351 // magnitude is already exact at that scale, so it passes
352 // through unchanged.
353 .name = "IOU credit: on-grid",
354 .assetsTotal = Number{1'000'000, 0},
355 .delta = Number{2, -9},
356 .expected = Number{2, -9},
357 },
358 Case{
359 // T = 9.999999999999999, delta = +5.
360 // Exact posterior = 14.999999999999999 (17 significant
361 // digits: "14" then 15 nines). postScale is computed under
362 // ToNearest at the Number (19-digit) level: normalized
363 // mantissa 1499999999999999900 (exponent -17) divided by
364 // 1000 (to reach 16-digit IOU precision) gives
365 // 1499999999999999.9, which rounds UP to 1500000000000000
366 // -- i.e. exactly 15, at exponent -14. postScale = -14.
367 // Downward-guarded posterior (exact, no rounding needed
368 // since 17 digits < 19): 14.999999999999999. Flooring THAT
369 // to 16 digits at scale -14 (Downward) gives
370 // 1499999999999999 * 10^-14 = 14.99999999999999 (postScale
371 // already matches the STAmount's own exponent, so no
372 // further roundToScale is applied).
373 // actualDelta = 14.99999999999999 - 9.999999999999999
374 // = 4.999999999999991.
375 // This mirrors testBugVaultDepositOvercreditsAcrossScaleBoundary
376 // in VaultBugs_test.cpp (same seed/deposit values), which
377 // asserts post-fix credited <= paid rather than an exact
378 // number; this row pins the exact value.
379 .name = "IOU credit: overcredit fix across a scale boundary",
380 .assetsTotal = Number{9'999'999'999'999'999LL, -15},
381 .delta = Number{5, 0},
382 .expected = Number{4'999'999'999'999'991LL, -15},
383 },
384 Case{
385 // Finding-1 regression: T = 1000000, delta = +9.999999999999999e-10.
386 // The exact sum needs ~25 significant digits (1000000 at
387 // position 6, delta's last digit at position -25), far
388 // beyond Number's 19-digit mantissa.
389 //
390 // postScale (computed under ToNearest): the digits of delta
391 // that land within the 19-digit window (positions -10..-12,
392 // "999") plus an all-nines remainder below position -12
393 // round UP under ToNearest, carrying all the way through
394 // the intervening zeros: the sum rounds to exactly
395 // 1000000.000000001, i.e. postScale = -9.
396 //
397 // But the credit branch computes the *posterior* under a
398 // Downward guard, not ToNearest: positions -10..-12 stay
399 // "999" (no carry), giving posterior = 1000000.000000000999
400 // exactly. Flooring that (Downward) to scale -9 truncates
401 // the "999" entirely, landing back on exactly 1000000 --
402 // i.e. the same as T. actualDelta = 0 => tecPRECISION_LOSS.
403 // This is the ambient-rounding leak the Downward guard on
404 // the credit-side sum exists to close; this row is a
405 // regression test that the guard is doing its job.
406 .name = "IOU credit: Finding-1 regression, ToNearest sum would overcredit",
407 .assetsTotal = Number{1'000'000, 0},
408 .delta = Number{9'999'999'999'999'999LL, -25},
409 .expected = std::nullopt,
410 },
411 Case{
412 // Same shape as the row above, but delta = +9.995e-10 is a
413 // 19-digit half-even tie at the position-(-12) cusp: the
414 // remainder below the retained "999" digits is exactly
415 // 0.5 ULP, and ToNearest ties-to-even rounds the (odd) "9"
416 // up, carrying the same way. Downward-guarded posterior
417 // still truncates to "...000999" and floors back to T, so
418 // the outcome is identical: tecPRECISION_LOSS.
419 .name = "IOU credit: Finding-1 regression, 19-digit half-even tie",
420 .assetsTotal = Number{1'000'000, 0},
421 .delta = Number{9'995, -13},
422 .expected = std::nullopt,
423 },
424 Case{
425 // T = 0, delta = +3.7e-5. Posterior grid is delta's own
426 // scale (postScale = -20, the canonical exponent of
427 // 3.7e-5), so the magnitude is trivially unchanged.
428 .name = "IOU credit: zero-total vault",
429 .assetsTotal = Number{0},
430 .delta = Number{37, -6},
431 .expected = Number{37, -6},
432 },
433 Case{
434 // T = 1000000, delta = +4e-10. Exact sum needs 17
435 // significant digits (leading "1" at position 6, trailing
436 // "4" at position -10); rounding to 16 digits drops the "4"
437 // entirely (0.4 ULP at scale -9 rounds down under both
438 // ToNearest and Downward), so postScale = -9 and the
439 // Downward-guarded posterior floors straight back to T.
440 // actualDelta = 0 => tecPRECISION_LOSS.
441 .name = "IOU credit: sub-ULP credit",
442 .assetsTotal = Number{1'000'000, 0},
443 .delta = Number{4, -10},
444 .expected = std::nullopt,
445 },
446 };
447
448 runCases(iou, cases);
449 }
450
451 // -------------------------------------------------------------------
452 // Integral assets (XRP, MPT): rounding is a no-op, magnitude is
453 // returned unchanged and positive regardless of delta's sign. This is
454 // a regression test for a signed-return bug: the function must not
455 // hand back a negative delta for a debit.
456 // -------------------------------------------------------------------
457 void
458 testIntegralAssets(Asset const& mpt, Asset const& xrp)
459 {
460 std::array<Case, 2> const mptCases{
461 Case{
462 .name = "MPT debit: magnitude is positive, not the signed delta",
463 .assetsTotal = Number{1'000'000},
464 .delta = Number{-5},
465 .expected = Number{5},
466 },
467 Case{
468 .name = "MPT credit: unchanged",
469 .assetsTotal = Number{1'000'000},
470 .delta = Number{7},
471 .expected = Number{7},
472 },
473 };
474 runCases(mpt, mptCases);
475
476 std::array<Case, 2> const xrpCases{
477 Case{
478 .name = "XRP debit: magnitude is positive, not the signed delta",
479 .assetsTotal = Number{100'000},
480 .delta = Number{-3},
481 .expected = Number{3},
482 },
483 Case{
484 .name = "XRP credit: unchanged",
485 .assetsTotal = Number{100'000},
486 .delta = Number{10},
487 .expected = Number{10},
488 },
489 };
490 runCases(xrp, xrpCases);
491 }
492
493 // -------------------------------------------------------------------
494 // FixedPrecision vaults, credits: clampToAssetsTotalScale takes the
495 // roundToPosteriorVaultScale branch instead of the Legacy/CashBasis
496 // scale()-of-the-sum branch; it is FixedPrecision's only credit path
497 // (VaultDeposit). All rows below use a Scale-6 vault (baseScale -6)
498 // with assetsTotal already sitting on that grid, so TowardsZero-
499 // truncating delta itself to scale -6 is the whole story: no case
500 // here forces liveScale to coarsen past baseScale (that scenario needs
501 // a non-unit share price and is exercised by
502 // VaultFixedPrecision_test.cpp's testPartialTowardZeroRounding / dust
503 // tests through real transactions instead).
504 // -------------------------------------------------------------------
505 void
507 {
508 std::uint8_t const fixedScale = 6;
509 Number const onGrid{3'234'567, -6}; // 3.234567, exact at scale -6.
510
511 std::array<Case, 3> const cases{
512 Case{
513 // delta already exact at the base grid: passes through
514 // unchanged, same as a Legacy on-grid credit.
515 .name = "FixedPrecision credit: exact on the base grid",
516 .assetsTotal = onGrid,
517 .delta = Number{2, -6},
518 .expected = Number{2, -6},
519 },
520 Case{
521 // delta = +2.3 base units, truncates to 2 base units.
522 .name = "FixedPrecision credit: truncated toward zero on the base grid",
523 .assetsTotal = onGrid,
524 .delta = Number{23, -7},
525 .expected = Number{2, -6},
526 },
527 Case{
528 // delta = +0.4 base units: sub-ULP at the fixed grid, so
529 // TowardsZero truncates it entirely to zero.
530 .name = "FixedPrecision credit: sub-ULP dust rejected at the base grid",
531 .assetsTotal = onGrid,
532 .delta = Number{4, -7},
533 .expected = std::nullopt,
534 },
535 };
536
537 runCases(iou, cases, fixedScale);
538 }
539
540 // -------------------------------------------------------------------
541 // FixedPrecision vaults, debits: outflows (VaultWithdraw, VaultClawback)
542 // never reach clampToAssetsTotalScale's FixedPrecision branch, which
543 // only accepts non-negative deltas; they go through the public
544 // clampVaultOutflow dispatcher instead, which rounds at
545 // AssetsAvailable's own posterior scale (clampToAvailableScale). These
546 // rows pin the same truncation behavior through that entry point.
547 // -------------------------------------------------------------------
548 void
550 {
551 std::uint8_t const fixedScale = 6;
552 Number const onGrid{3'234'567, -6}; // 3.234567, exact at scale -6.
553
554 std::array<Case, 3> const cases{
555 Case{
556 // delta already exact at the base grid: passes through
557 // unchanged, same as a Legacy on-grid debit.
558 .name = "FixedPrecision debit: exact on the base grid",
559 .assetsTotal = onGrid,
560 .delta = Number{-1, -6},
561 .expected = Number{1, -6},
562 },
563 Case{
564 // delta = -1.7 base units. TowardsZero truncates the
565 // magnitude to 1 base unit -- this is the "books the
566 // truncated amount" behavior VaultFixedPrecision_test's
567 // testPartialTowardZeroRounding exercises end-to-end via
568 // VaultWithdraw; this row pins it at the helper level.
569 .name = "FixedPrecision debit: truncated toward zero on the base grid",
570 .assetsTotal = onGrid,
571 .delta = Number{-17, -7},
572 .expected = Number{1, -6},
573 },
574 Case{
575 // delta = -0.3 base units: sub-ULP at the fixed grid, so
576 // TowardsZero truncates it entirely to zero.
577 .name = "FixedPrecision debit: sub-ULP dust rejected at the base grid",
578 .assetsTotal = onGrid,
579 .delta = Number{-3, -7},
580 .expected = std::nullopt,
581 },
582 };
583
584 runCases(iou, cases, fixedScale, /* viaClampVaultOutflow */ true);
585 }
586
587public:
588 void
589 run() override
590 {
592
593 test::jtx::Account const issuer{"issuer"};
594 Issue const iou{toCurrency("USD"), issuer.id()};
595 MPTIssue const mpt{makeMptID(1, issuer.id())};
596 Issue const xrp = xrpIssue();
597
598 testIouDebits(iou);
599 testIouCredits(iou);
600 testIntegralAssets(mpt, xrp);
603 }
604};
605
606BEAST_DEFINE_TESTSUITE(VaultHelpers, app, xrpl);
607
608} // namespace xrpl
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
bool integral() const
Definition Asset.h:133
A currency issued by an account.
Definition Issue.h:18
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
constexpr rep mantissa() const noexcept
Returns the mantissa of the external view of the Number.
Definition Number.h:696
static MantissaRange::MantissaScale getMantissaScale()
Returns which mantissa scale is currently in use for normalization.
constexpr int exponent() const noexcept
Returns the exponent of the external view of the Number.
Definition Number.h:718
std::string getText() const override
void testFixedPrecisionCredit(Asset const &iou)
void testIntegralAssets(Asset const &mpt, Asset const &xrp)
void testIouCredits(Asset const &iou)
void run() override
Runs the suite.
void testIouDebits(Asset const &iou)
void runCases(Asset const &asset, std::array< Case, N > const &cases, std::optional< std::uint8_t > fixedScale=std::nullopt, bool viaClampVaultOutflow=false)
static std::shared_ptr< SLE > makeVault(Asset const &asset, Number const &assetsTotal, std::optional< std::uint8_t > fixedScale=std::nullopt)
void testFixedPrecisionDebit(Asset const &iou)
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
AccountID id() const
Returns the Account ID.
T make_shared(T... args)
T min(T... args)
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:565
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
std::expected< STAmount, TER > clampVaultOutflow(SLE::ConstRef vault, STAmount const &delta)
Outflow clamp dispatcher for VaultWithdraw and VaultClawback: rounds a negative delta to the scale ap...
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
Definition STAmount.h:819
std::expected< STAmount, TER > clampToAssetsTotalScale(SLE::ConstRef vault, STAmount const &delta)
Adjusts a requested asset change (delta) to match the decimal scale of the updated total vault assets...
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
std::string transToken(TER code)
Definition TER.cpp:259
STAmount roundToScale(STAmount const &value, std::int32_t scale, Number::RoundingMode rounding=Number::getround())
Round an arbitrary precision Amount to the precision of an STAmount that has a given exponent.
BaseUInt< 256 > UInt256
Definition base_uint.h:581
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:219
constexpr Number abs(Number x) noexcept
Definition Number.h:880
@ tecPRECISION_LOSS
Definition TER.h:374
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
std::optional< Number > expected
T to_string(T... args)