xrpld
Loading...
Searching...
No Matches
STAmount_test.cpp
1
2#include <xrpl/basics/Number.h>
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/basics/random.h>
5#include <xrpl/beast/hash/uhash.h>
6#include <xrpl/beast/unit_test/suite.h>
7#include <xrpl/beast/utility/Zero.h>
8#include <xrpl/json/json_forwards.h>
9#include <xrpl/json/json_value.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/IOUAmount.h>
13#include <xrpl/protocol/Indexes.h>
14#include <xrpl/protocol/Issue.h>
15#include <xrpl/protocol/MPTAmount.h>
16#include <xrpl/protocol/MPTIssue.h>
17#include <xrpl/protocol/Protocol.h>
18#include <xrpl/protocol/Rate.h>
19#include <xrpl/protocol/Rules.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/STAmount.h>
22#include <xrpl/protocol/Serializer.h>
23#include <xrpl/protocol/UintTypes.h>
24#include <xrpl/protocol/XRPAmount.h>
25
26#include <cstdint>
27#include <exception>
28#include <limits>
29#include <stdexcept>
30#include <string>
31#include <type_traits>
32#include <typeinfo>
33#include <unordered_set>
34
35namespace xrpl {
36
38{
39public:
40 static STAmount
42 {
43 Serializer ser;
44 s.add(ser);
45
46 SerialIter sit(ser.slice());
47 return STAmount(sit, sfGeneric);
48 }
49
50 //--------------------------------------------------------------------------
51 static STAmount
52 roundSelf(STAmount const& amount)
53 {
54 if (amount.native())
55 return amount;
56
57 std::uint64_t mantissa = amount.mantissa();
58 std::uint64_t const valueDigits = mantissa % 1000000000;
59
60 if (valueDigits == 1)
61 {
62 mantissa--;
63
64 if (mantissa < STAmount::kMinValue)
65 return {amount.asset(), mantissa, amount.exponent(), amount.negative()};
66
67 return {
68 amount.asset(),
69 mantissa,
70 amount.exponent(),
71 amount.negative(),
73 }
74
75 if (valueDigits == 999999999)
76 {
77 mantissa++;
78
79 if (mantissa > STAmount::kMaxValue)
80 return {amount.asset(), mantissa, amount.exponent(), amount.negative()};
81
82 return {
83 amount.asset(),
84 mantissa,
85 amount.exponent(),
86 amount.negative(),
88 }
89
90 return amount;
91 }
92
93 void
94 roundTest(int n, int d, int m)
95 {
96 // check STAmount rounding
97 STAmount const num(noIssue(), n);
98 STAmount const den(noIssue(), d);
99 STAmount const mul(noIssue(), m);
100 STAmount const quot = divide(STAmount(n), STAmount(d), noIssue());
101 STAmount const res = roundSelf(multiply(quot, mul, noIssue()));
102
103 BEAST_EXPECT(!res.native());
104
105 STAmount const cmp(noIssue(), (n * m) / d);
106
107 BEAST_EXPECT(!cmp.native());
108
109 BEAST_EXPECT(cmp.get<Issue>().currency == res.get<Issue>().currency);
110
111 if (res != cmp)
112 {
113 log << "(" << num.getText() << "/" << den.getText() << ") X " << mul.getText() << " = "
114 << res.getText() << " not " << cmp.getText();
115 fail("Rounding");
116 return;
117 }
118 }
119
120 void
121 mulTest(int a, int b)
122 {
123 STAmount const aa(noIssue(), a);
124 STAmount const bb(noIssue(), b);
125 STAmount const prod1(multiply(aa, bb, noIssue()));
126
127 BEAST_EXPECT(!prod1.native());
128
129 STAmount const prod2(
130 noIssue(), static_cast<std::uint64_t>(a) * static_cast<std::uint64_t>(b));
131
132 if (prod1 != prod2)
133 {
134 log << "nn(" << aa.getFullText() << " * " << bb.getFullText()
135 << ") = " << prod1.getFullText() << " not " << prod2.getFullText();
136 fail("Multiplication result is not exact");
137 }
138 }
139
140 //--------------------------------------------------------------------------
141
142 void
143 testSetValue(std::string const& value, Issue const& issue, bool success = true)
144 {
145 try
146 {
147 STAmount const amount = amountFromString(issue, value);
148 BEAST_EXPECT(amount.getText() == value);
149 }
150 catch (std::exception const&)
151 {
152 BEAST_EXPECT(!success);
153 }
154 }
155
156 void
158 {
159 {
160 testcase("set value (native)");
161
162 Issue const xrp(xrpIssue());
163
164 // fractional XRP (i.e. drops)
165 testSetValue("1", xrp);
166 testSetValue("22", xrp);
167 testSetValue("333", xrp);
168 testSetValue("4444", xrp);
169 testSetValue("55555", xrp);
170 testSetValue("666666", xrp);
171
172 // 1 XRP up to 100 billion, in powers of 10 (in drops)
173 testSetValue("1000000", xrp);
174 testSetValue("10000000", xrp);
175 testSetValue("100000000", xrp);
176 testSetValue("1000000000", xrp);
177 testSetValue("10000000000", xrp);
178 testSetValue("100000000000", xrp);
179 testSetValue("1000000000000", xrp);
180 testSetValue("10000000000000", xrp);
181 testSetValue("100000000000000", xrp);
182 testSetValue("1000000000000000", xrp);
183 testSetValue("10000000000000000", xrp);
184 testSetValue("100000000000000000", xrp);
185
186 // Invalid native values:
187 testSetValue("1.1", xrp, false);
188 testSetValue("100000000000000001", xrp, false);
189 testSetValue("1000000000000000000", xrp, false);
190 }
191
192 {
193 testcase("set value (iou)");
194
195 Issue const usd(Currency(0x5553440000000000), AccountID(0x4985601));
196
197 testSetValue("1", usd);
198 testSetValue("10", usd);
199 testSetValue("100", usd);
200 testSetValue("1000", usd);
201 testSetValue("10000", usd);
202 testSetValue("100000", usd);
203 testSetValue("1000000", usd);
204 testSetValue("10000000", usd);
205 testSetValue("100000000", usd);
206 testSetValue("1000000000", usd);
207 testSetValue("10000000000", usd);
208
209 testSetValue("1234567.1", usd);
210 testSetValue("1234567.12", usd);
211 testSetValue("1234567.123", usd);
212 testSetValue("1234567.1234", usd);
213 testSetValue("1234567.12345", usd);
214 testSetValue("1234567.123456", usd);
215 testSetValue("1234567.1234567", usd);
216 testSetValue("1234567.12345678", usd);
217 testSetValue("1234567.123456789", usd);
218 }
219 }
220
221 //--------------------------------------------------------------------------
222
223 void
225 {
226 testcase("native currency");
227
228 STAmount const zeroSt;
229 STAmount const one(1);
230 STAmount const hundred(100);
231
232 // VFALCO NOTE Why repeat "STAmount fail" so many times??
233 unexpected(serializeAndDeserialize(zeroSt) != zeroSt, "STAmount fail");
234 unexpected(serializeAndDeserialize(one) != one, "STAmount fail");
235 unexpected(serializeAndDeserialize(hundred) != hundred, "STAmount fail");
236 unexpected(!zeroSt.native(), "STAmount fail");
237 unexpected(!hundred.native(), "STAmount fail");
238 unexpected(zeroSt != beast::kZero, "STAmount fail");
239 unexpected(one == beast::kZero, "STAmount fail");
240 unexpected(hundred == beast::kZero, "STAmount fail");
241 unexpected((zeroSt < zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
242 unexpected(!(zeroSt < one), "STAmount fail");
243 unexpected(!(zeroSt < hundred), "STAmount fail");
244 unexpected((one < zeroSt), "STAmount fail");
245 unexpected((one < one), "STAmount fail"); // NOLINT(misc-redundant-expression)
246 unexpected(!(one < hundred), "STAmount fail");
247 unexpected((hundred < zeroSt), "STAmount fail");
248 unexpected((hundred < one), "STAmount fail");
249 unexpected((hundred < hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
250 unexpected((zeroSt > zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
251 unexpected((zeroSt > one), "STAmount fail");
252 unexpected((zeroSt > hundred), "STAmount fail");
253 unexpected(!(one > zeroSt), "STAmount fail");
254 unexpected((one > one), "STAmount fail"); // NOLINT(misc-redundant-expression)
255 unexpected((one > hundred), "STAmount fail");
256 unexpected(!(hundred > zeroSt), "STAmount fail");
257 unexpected(!(hundred > one), "STAmount fail");
258 unexpected((hundred > hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
259 unexpected(!(zeroSt <= zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
260 unexpected(!(zeroSt <= one), "STAmount fail");
261 unexpected(!(zeroSt <= hundred), "STAmount fail");
262 unexpected((one <= zeroSt), "STAmount fail");
263 unexpected(!(one <= one), "STAmount fail"); // NOLINT(misc-redundant-expression)
264 unexpected(!(one <= hundred), "STAmount fail");
265 unexpected((hundred <= zeroSt), "STAmount fail");
266 unexpected((hundred <= one), "STAmount fail");
267 unexpected(!(hundred <= hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
268 unexpected(!(zeroSt >= zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
269 unexpected((zeroSt >= one), "STAmount fail");
270 unexpected((zeroSt >= hundred), "STAmount fail");
271 unexpected(!(one >= zeroSt), "STAmount fail");
272 unexpected(!(one >= one), "STAmount fail"); // NOLINT(misc-redundant-expression)
273 unexpected((one >= hundred), "STAmount fail");
274 unexpected(!(hundred >= zeroSt), "STAmount fail");
275 unexpected(!(hundred >= one), "STAmount fail");
276 unexpected(!(hundred >= hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
277 unexpected(!(zeroSt == zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
278 unexpected((zeroSt == one), "STAmount fail");
279 unexpected((zeroSt == hundred), "STAmount fail");
280 unexpected((one == zeroSt), "STAmount fail");
281 unexpected(!(one == one), "STAmount fail"); // NOLINT(misc-redundant-expression)
282 unexpected((one == hundred), "STAmount fail");
283 unexpected((hundred == zeroSt), "STAmount fail");
284 unexpected((hundred == one), "STAmount fail");
285 unexpected(!(hundred == hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
286 unexpected((zeroSt != zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
287 unexpected(!(zeroSt != one), "STAmount fail");
288 unexpected(!(zeroSt != hundred), "STAmount fail");
289 unexpected(!(one != zeroSt), "STAmount fail");
290 unexpected((one != one), "STAmount fail"); // NOLINT(misc-redundant-expression)
291 unexpected(!(one != hundred), "STAmount fail");
292 unexpected(!(hundred != zeroSt), "STAmount fail");
293 unexpected(!(hundred != one), "STAmount fail");
294 unexpected((hundred != hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
295 unexpected(STAmount().getText() != "0", "STAmount fail");
296 unexpected(STAmount(31).getText() != "31", "STAmount fail");
297 unexpected(STAmount(310).getText() != "310", "STAmount fail");
298 unexpected(to_string(Currency()) != "XRP", "cHC(XRP)");
299 Currency c;
300 unexpected(!toCurrency(c, "USD"), "create USD currency");
301 unexpected(to_string(c) != "USD", "check USD currency");
302
303 std::string const cur = "015841551A748AD2C1F76FF6ECB0CCCD00000000";
304 unexpected(!toCurrency(c, cur), "create custom currency");
305 unexpected(to_string(c) != cur, "check custom currency");
306 }
307
308 //--------------------------------------------------------------------------
309
310 void
312 {
313 testcase("custom currency");
314
315 STAmount const zeroSt(noIssue());
316 STAmount const one(noIssue(), 1);
317 STAmount const hundred(noIssue(), 100);
318
319 unexpected(serializeAndDeserialize(zeroSt) != zeroSt, "STAmount fail");
320 unexpected(serializeAndDeserialize(one) != one, "STAmount fail");
321 unexpected(serializeAndDeserialize(hundred) != hundred, "STAmount fail");
322 unexpected(zeroSt.native(), "STAmount fail");
323 unexpected(hundred.native(), "STAmount fail");
324 unexpected(zeroSt != beast::kZero, "STAmount fail");
325 unexpected(one == beast::kZero, "STAmount fail");
326 unexpected(hundred == beast::kZero, "STAmount fail");
327 unexpected((zeroSt < zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
328 unexpected(!(zeroSt < one), "STAmount fail");
329 unexpected(!(zeroSt < hundred), "STAmount fail");
330 unexpected((one < zeroSt), "STAmount fail");
331 unexpected((one < one), "STAmount fail"); // NOLINT(misc-redundant-expression)
332 unexpected(!(one < hundred), "STAmount fail");
333 unexpected((hundred < zeroSt), "STAmount fail");
334 unexpected((hundred < one), "STAmount fail");
335 unexpected((hundred < hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
336 unexpected((zeroSt > zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
337 unexpected((zeroSt > one), "STAmount fail");
338 unexpected((zeroSt > hundred), "STAmount fail");
339 unexpected(!(one > zeroSt), "STAmount fail");
340 unexpected((one > one), "STAmount fail"); // NOLINT(misc-redundant-expression)
341 unexpected((one > hundred), "STAmount fail");
342 unexpected(!(hundred > zeroSt), "STAmount fail");
343 unexpected(!(hundred > one), "STAmount fail");
344 unexpected((hundred > hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
345 unexpected(!(zeroSt <= zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
346 unexpected(!(zeroSt <= one), "STAmount fail");
347 unexpected(!(zeroSt <= hundred), "STAmount fail");
348 unexpected((one <= zeroSt), "STAmount fail");
349 unexpected(!(one <= one), "STAmount fail"); // NOLINT(misc-redundant-expression)
350 unexpected(!(one <= hundred), "STAmount fail");
351 unexpected((hundred <= zeroSt), "STAmount fail");
352 unexpected((hundred <= one), "STAmount fail");
353 unexpected(!(hundred <= hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
354 unexpected(!(zeroSt >= zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
355 unexpected((zeroSt >= one), "STAmount fail");
356 unexpected((zeroSt >= hundred), "STAmount fail");
357 unexpected(!(one >= zeroSt), "STAmount fail");
358 unexpected(!(one >= one), "STAmount fail"); // NOLINT(misc-redundant-expression)
359 unexpected((one >= hundred), "STAmount fail");
360 unexpected(!(hundred >= zeroSt), "STAmount fail");
361 unexpected(!(hundred >= one), "STAmount fail");
362 unexpected(!(hundred >= hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
363 unexpected(!(zeroSt == zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
364 unexpected((zeroSt == one), "STAmount fail");
365 unexpected((zeroSt == hundred), "STAmount fail");
366 unexpected((one == zeroSt), "STAmount fail");
367 unexpected(!(one == one), "STAmount fail"); // NOLINT(misc-redundant-expression)
368 unexpected((one == hundred), "STAmount fail");
369 unexpected((hundred == zeroSt), "STAmount fail");
370 unexpected((hundred == one), "STAmount fail");
371 unexpected(!(hundred == hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
372 unexpected((zeroSt != zeroSt), "STAmount fail"); // NOLINT(misc-redundant-expression)
373 unexpected(!(zeroSt != one), "STAmount fail");
374 unexpected(!(zeroSt != hundred), "STAmount fail");
375 unexpected(!(one != zeroSt), "STAmount fail");
376 unexpected((one != one), "STAmount fail"); // NOLINT(misc-redundant-expression)
377 unexpected(!(one != hundred), "STAmount fail");
378 unexpected(!(hundred != zeroSt), "STAmount fail");
379 unexpected(!(hundred != one), "STAmount fail");
380 unexpected((hundred != hundred), "STAmount fail"); // NOLINT(misc-redundant-expression)
381 unexpected(STAmount(noIssue()).getText() != "0", "STAmount fail");
382 unexpected(STAmount(noIssue(), 31).getText() != "31", "STAmount fail");
383 unexpected(STAmount(noIssue(), 31, 1).getText() != "310", "STAmount fail");
384 unexpected(STAmount(noIssue(), 31, -1).getText() != "3.1", "STAmount fail");
385 unexpected(STAmount(noIssue(), 31, -2).getText() != "0.31", "STAmount fail");
387 multiply(STAmount(noIssue(), 20), STAmount(3), noIssue()).getText() != "60",
388 "STAmount multiply fail 1");
390 multiply(STAmount(noIssue(), 20), STAmount(3), xrpIssue()).getText() != "60",
391 "STAmount multiply fail 2");
393 multiply(STAmount(20), STAmount(3), noIssue()).getText() != "60",
394 "STAmount multiply fail 3");
396 multiply(STAmount(20), STAmount(3), xrpIssue()).getText() != "60",
397 "STAmount multiply fail 4");
398
399 if (divide(STAmount(noIssue(), 60), STAmount(3), noIssue()).getText() != "20")
400 {
401 log << "60/3 = " << divide(STAmount(noIssue(), 60), STAmount(3), noIssue()).getText();
402 fail("STAmount divide fail");
403 }
404 else
405 {
406 pass();
407 }
408
410 divide(STAmount(noIssue(), 60), STAmount(3), xrpIssue()).getText() != "20",
411 "STAmount divide fail");
412
414 divide(STAmount(noIssue(), 60), STAmount(noIssue(), 3), noIssue()).getText() != "20",
415 "STAmount divide fail");
416
418 divide(STAmount(noIssue(), 60), STAmount(noIssue(), 3), xrpIssue()).getText() != "20",
419 "STAmount divide fail");
420
421 STAmount const a1(noIssue(), 60);
422 STAmount const a2(noIssue(), 10, -1);
423
425 divide(a2, a1, noIssue()) != amountFromQuality(getRate(a1, a2)),
426 "STAmount setRate(getRate) fail");
427
429 divide(a1, a2, noIssue()) != amountFromQuality(getRate(a2, a1)),
430 "STAmount setRate(getRate) fail");
431 }
432
433 //--------------------------------------------------------------------------
434
435 void
437 {
438 testcase("arithmetic");
439
440 // Test currency multiplication and division operations such as
441 // convertToDisplayAmount, convertToInternalAmount, getRate, getClaimed,
442 // and getNeeded
443
445 getRate(STAmount(1), STAmount(10)) !=
446 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
447 "STAmount getRate fail 1");
448
450 getRate(STAmount(10), STAmount(1)) !=
451 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
452 "STAmount getRate fail 2");
453
455 getRate(STAmount(noIssue(), 1), STAmount(noIssue(), 10)) !=
456 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
457 "STAmount getRate fail 3");
458
460 getRate(STAmount(noIssue(), 10), STAmount(noIssue(), 1)) !=
461 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
462 "STAmount getRate fail 4");
463
465 getRate(STAmount(noIssue(), 1), STAmount(10)) !=
466 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
467 "STAmount getRate fail 5");
468
470 getRate(STAmount(noIssue(), 10), STAmount(1)) !=
471 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
472 "STAmount getRate fail 6");
473
475 getRate(STAmount(1), STAmount(noIssue(), 10)) !=
476 (((100ull - 14) << (64 - 8)) | 1000000000000000ull),
477 "STAmount getRate fail 7");
478
480 getRate(STAmount(10), STAmount(noIssue(), 1)) !=
481 (((100ull - 16) << (64 - 8)) | 1000000000000000ull),
482 "STAmount getRate fail 8");
483
484 roundTest(1, 3, 3);
485 roundTest(2, 3, 9);
486 roundTest(1, 7, 21);
487 roundTest(1, 2, 4);
488 roundTest(3, 9, 18);
489 roundTest(7, 11, 44);
490
491 for (int i = 0; i <= 100000; ++i)
492 {
493 mulTest(randInt(10000000), randInt(10000000));
494 }
495 }
496
497 //--------------------------------------------------------------------------
498
499 void
501 {
502 testcase("underflow");
503
504 STAmount const bigNative(STAmount::kMaxNative / 2);
505 STAmount const bigValue(
507 STAmount const smallValue(
509 STAmount const zeroSt(noIssue(), 0);
510
511 STAmount const smallXSmall = multiply(smallValue, smallValue, noIssue());
512
513 BEAST_EXPECT(smallXSmall == beast::kZero);
514
515 STAmount bigDsmall = divide(smallValue, bigValue, noIssue());
516
517 BEAST_EXPECT(bigDsmall == beast::kZero);
518
519 BEAST_EXPECT(bigDsmall == beast::kZero);
520
521 bigDsmall = divide(smallValue, bigValue, xrpIssue());
522
523 BEAST_EXPECT(bigDsmall == beast::kZero);
524
525 bigDsmall = divide(smallValue, bigNative, xrpIssue());
526
527 BEAST_EXPECT(bigDsmall == beast::kZero);
528
529 // very bad offer
530 std::uint64_t r = getRate(smallValue, bigValue);
531
532 BEAST_EXPECT(r == 0);
533
534 // very good offer
535 r = getRate(bigValue, smallValue);
536
537 BEAST_EXPECT(r == 0);
538 }
539
540 //--------------------------------------------------------------------------
541
542 void
544 {
545 // VFALCO TODO There are no actual tests here, just printed output?
546 // Change this to actually do something.
547 }
548
549 void
551 {
553
554 {
555 STAmount const stnum{sfNumber};
556 BEAST_EXPECT(stnum.getSType() == STI_AMOUNT);
557 BEAST_EXPECT(stnum.getText() == "0");
558 BEAST_EXPECT(stnum.isDefault() == true);
559 BEAST_EXPECT(stnum.value() == Number{0});
560 }
561
562 {
563 BEAST_EXPECT(amountFromJson(sfNumber, json::Value(42)) == XRPAmount(42));
564 BEAST_EXPECT(amountFromJson(sfNumber, json::Value(-42)) == XRPAmount(-42));
565
566 BEAST_EXPECT(amountFromJson(sfNumber, json::UInt(42)) == XRPAmount(42));
567
568 BEAST_EXPECT(amountFromJson(sfNumber, "-123") == XRPAmount(-123));
569
570 BEAST_EXPECT(amountFromJson(sfNumber, "123") == XRPAmount(123));
571 BEAST_EXPECT(amountFromJson(sfNumber, "-123") == XRPAmount(-123));
572
573 BEAST_EXPECT(amountFromJson(sfNumber, "3.14e2") == XRPAmount(314));
574 BEAST_EXPECT(amountFromJson(sfNumber, "-3.14e2") == XRPAmount(-314));
575
576 BEAST_EXPECT(amountFromJson(sfNumber, "0") == XRPAmount(0));
577 BEAST_EXPECT(amountFromJson(sfNumber, "-0") == XRPAmount(0));
578
579 constexpr auto kIMin = std::numeric_limits<int>::min();
580 BEAST_EXPECT(amountFromJson(sfNumber, kIMin) == XRPAmount(kIMin));
581 BEAST_EXPECT(amountFromJson(sfNumber, std::to_string(kIMin)) == XRPAmount(kIMin));
582
583 constexpr auto kIMax = std::numeric_limits<int>::max();
584 BEAST_EXPECT(amountFromJson(sfNumber, kIMax) == XRPAmount(kIMax));
585 BEAST_EXPECT(amountFromJson(sfNumber, std::to_string(kIMax)) == XRPAmount(kIMax));
586
587 constexpr auto kUMax = std::numeric_limits<unsigned int>::max();
588 BEAST_EXPECT(amountFromJson(sfNumber, kUMax) == XRPAmount(kUMax));
589 BEAST_EXPECT(amountFromJson(sfNumber, std::to_string(kUMax)) == XRPAmount(kUMax));
590
591 // XRP does not handle fractional part
592 try
593 {
594 auto const _ = amountFromJson(sfNumber, "0.0");
595 BEAST_EXPECT(false);
596 }
597 catch (std::runtime_error const& e)
598 {
599 std::string const expected = "XRP and MPT must be specified as integral amount.";
600 BEAST_EXPECT(e.what() == expected);
601 }
602
603 // XRP does not handle fractional part
604 try
605 {
606 auto const _ = amountFromJson(sfNumber, "1000e-2");
607 BEAST_EXPECT(false);
608 }
609 catch (std::runtime_error const& e)
610 {
611 std::string const expected = "XRP and MPT must be specified as integral amount.";
612 BEAST_EXPECT(e.what() == expected);
613 }
614
615 // Obvious non-numbers tested here
616 try
617 {
618 auto const _ = amountFromJson(sfNumber, "");
619 BEAST_EXPECT(false);
620 }
621 catch (std::runtime_error const& e)
622 {
623 std::string const expected = "'' is not a number";
624 BEAST_EXPECT(e.what() == expected);
625 }
626
627 try
628 {
629 auto const _ = amountFromJson(sfNumber, "e");
630 BEAST_EXPECT(false);
631 }
632 catch (std::runtime_error const& e)
633 {
634 std::string const expected = "'e' is not a number";
635 BEAST_EXPECT(e.what() == expected);
636 }
637
638 try
639 {
640 auto const _ = amountFromJson(sfNumber, "1e");
641 BEAST_EXPECT(false);
642 }
643 catch (std::runtime_error const& e)
644 {
645 std::string const expected = "'1e' is not a number";
646 BEAST_EXPECT(e.what() == expected);
647 }
648
649 try
650 {
651 auto const _ = amountFromJson(sfNumber, "e2");
652 BEAST_EXPECT(false);
653 }
654 catch (std::runtime_error const& e)
655 {
656 std::string const expected = "'e2' is not a number";
657 BEAST_EXPECT(e.what() == expected);
658 }
659
660 try
661 {
662 auto const _ = amountFromJson(sfNumber, json::Value());
663 BEAST_EXPECT(false);
664 }
665 catch (std::runtime_error const& e)
666 {
667 std::string const expected = "XRP may not be specified with a null Json value";
668 BEAST_EXPECT(e.what() == expected);
669 }
670
671 try
672 {
673 auto const _ = amountFromJson(
674 sfNumber,
675 "123456789012345678901234567890123456789012345678901234"
676 "5678"
677 "901234567890123456789012345678901234567890123456789012"
678 "3456"
679 "78901234567890123456789012345678901234567890");
680 BEAST_EXPECT(false);
681 }
682 catch (std::bad_cast const& e)
683 {
684 BEAST_EXPECT(true);
685 }
686
687 // We do not handle leading zeros
688 try
689 {
690 auto const _ = amountFromJson(sfNumber, "001");
691 BEAST_EXPECT(false);
692 }
693 catch (std::runtime_error const& e)
694 {
695 std::string const expected = "'001' is not a number";
696 BEAST_EXPECT(e.what() == expected);
697 }
698
699 try
700 {
701 auto const _ = amountFromJson(sfNumber, "000.0");
702 BEAST_EXPECT(false);
703 }
704 catch (std::runtime_error const& e)
705 {
706 std::string const expected = "'000.0' is not a number";
707 BEAST_EXPECT(e.what() == expected);
708 }
709
710 // We do not handle dangling dot
711 try
712 {
713 auto const _ = amountFromJson(sfNumber, ".1");
714 BEAST_EXPECT(false);
715 }
716 catch (std::runtime_error const& e)
717 {
718 std::string const expected = "'.1' is not a number";
719 BEAST_EXPECT(e.what() == expected);
720 }
721
722 try
723 {
724 auto const _ = amountFromJson(sfNumber, "1.");
725 BEAST_EXPECT(false);
726 }
727 catch (std::runtime_error const& e)
728 {
729 std::string const expected = "'1.' is not a number";
730 BEAST_EXPECT(e.what() == expected);
731 }
732
733 try
734 {
735 auto const _ = amountFromJson(sfNumber, "1.e3");
736 BEAST_EXPECT(false);
737 }
738 catch (std::runtime_error const& e)
739 {
740 std::string const expected = "'1.e3' is not a number";
741 BEAST_EXPECT(e.what() == expected);
742 }
743 }
744 }
745
746 void
748 {
749 testcase("STAmount to XRPAmount conversions");
750
751 Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
752 Issue const xrp{xrpIssue()};
753
754 for (std::uint64_t drops = 100000000000000000; drops != 1; drops = drops / 10)
755 {
756 auto const t = amountFromString(xrp, std::to_string(drops));
757 auto const s = t.xrp();
758 BEAST_EXPECT(s.drops() == drops);
759 BEAST_EXPECT(t == STAmount(XRPAmount(drops)));
760 BEAST_EXPECT(s == XRPAmount(drops));
761 }
762
763 try
764 {
765 auto const t = amountFromString(usd, "136500");
766 fail(to_string(t.xrp()));
767 }
768 catch (std::logic_error const&)
769 {
770 pass();
771 }
772 catch (std::exception const&)
773 {
774 fail("wrong exception");
775 }
776 }
777
778 void
780 {
781 testcase("STAmount to IOUAmount conversions");
782
783 Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
784 Issue const xrp{xrpIssue()};
785
786 for (std::uint64_t dollars = 10000000000; dollars != 1; dollars = dollars / 10)
787 {
788 auto const t = amountFromString(usd, std::to_string(dollars));
789 auto const s = t.iou();
790 BEAST_EXPECT(t == STAmount(s, usd));
791 BEAST_EXPECT(s.mantissa() == t.mantissa());
792 BEAST_EXPECT(s.exponent() == t.exponent());
793 }
794
795 try
796 {
797 auto const t = amountFromString(xrp, "136500");
798 fail(to_string(t.iou()));
799 }
800 catch (std::logic_error const&)
801 {
802 pass();
803 }
804 catch (std::exception const&)
805 {
806 fail("wrong exception");
807 }
808 }
809
810 void
812 {
813 testcase("can add xrp");
814
815 // Adding zero
816 {
817 STAmount const amt1(XRPAmount(0));
818 STAmount const amt2(XRPAmount(1000));
819 BEAST_EXPECT(canAdd(amt1, amt2) == true);
820 }
821
822 // Adding zero
823 {
824 STAmount const amt1(XRPAmount(1000));
825 STAmount const amt2(XRPAmount(0));
826 BEAST_EXPECT(canAdd(amt1, amt2) == true);
827 }
828
829 // Adding two positive XRP amounts
830 {
831 STAmount const amt1(XRPAmount(500));
832 STAmount const amt2(XRPAmount(1500));
833 BEAST_EXPECT(canAdd(amt1, amt2) == true);
834 }
835
836 // Adding two negative XRP amounts
837 {
838 STAmount const amt1(XRPAmount(-500));
839 STAmount const amt2(XRPAmount(-1500));
840 BEAST_EXPECT(canAdd(amt1, amt2) == true);
841 }
842
843 // Adding a positive and a negative XRP amount
844 {
845 STAmount const amt1(XRPAmount(1000));
846 STAmount const amt2(XRPAmount(-1000));
847 BEAST_EXPECT(canAdd(amt1, amt2) == true);
848 }
849
850 // Overflow check for max XRP amounts
851 {
853 STAmount const amt2(XRPAmount(1));
854 BEAST_EXPECT(canAdd(amt1, amt2) == false);
855 BEAST_EXPECT(except<std::overflow_error>([&] { (void)(amt1 + amt2); }));
856 }
857
858 // Overflow check for min XRP amounts
859 {
860 STAmount const amt1(
862 true);
863 STAmount const amt2(XRPAmount(-2));
864 BEAST_EXPECT(canAdd(amt1, amt2) == false);
865 BEAST_EXPECT(except<std::overflow_error>([&] { (void)(amt1 + amt2); }));
866 }
867 }
868
869 void
871 {
872 testcase("can add iou");
873
874 Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
875 Issue const eur{Currency(0x4555520000000000), AccountID(0x4985601)};
876
877 // Adding two IOU amounts
878 {
879 STAmount const amt1(usd, 500);
880 STAmount const amt2(usd, 1500);
881 BEAST_EXPECT(canAdd(amt1, amt2) == true);
882 }
883
884 // Adding a positive and a negative IOU amount
885 {
886 STAmount const amt1(usd, 1000);
887 STAmount const amt2(usd, -1000);
888 BEAST_EXPECT(canAdd(amt1, amt2) == true);
889 }
890
891 // Overflow check for max IOU amounts
892 {
894 STAmount const amt2(usd, 1);
895 BEAST_EXPECT(canAdd(amt1, amt2) == false);
896 }
897
898 // Overflow check for min IOU amounts
899 {
901 STAmount const amt2(usd, -1);
902 BEAST_EXPECT(canAdd(amt1, amt2) == false);
903 }
904
905 // Adding XRP and IOU
906 {
907 STAmount const amt1(XRPAmount(1));
908 STAmount const amt2(usd, 1);
909 BEAST_EXPECT(canAdd(amt1, amt2) == false);
910 }
911
912 // Adding different IOU issues (non zero)
913 {
914 STAmount const amt1(usd, 1000);
915 STAmount const amt2(eur, 500);
916 BEAST_EXPECT(canAdd(amt1, amt2) == false);
917 }
918
919 // Adding different IOU issues (zero)
920 {
921 STAmount const amt1(usd, 0);
922 STAmount const amt2(eur, 500);
923 BEAST_EXPECT(canAdd(amt1, amt2) == false);
924 }
925 }
926
927 void
929 {
930 testcase("can add mpt");
931
932 MPTIssue const mpt{MPTIssue{makeMptID(1, AccountID(0x4985601))}};
933 MPTIssue const mpt2{MPTIssue{makeMptID(2, AccountID(0x4985601))}};
934
935 // Adding zero
936 {
937 STAmount const amt1(mpt, 0);
938 STAmount const amt2(mpt, 1000);
939 BEAST_EXPECT(canAdd(amt1, amt2) == true);
940 }
941
942 // Adding zero
943 {
944 STAmount const amt1(mpt, 1000);
945 STAmount const amt2(mpt, 0);
946 BEAST_EXPECT(canAdd(amt1, amt2) == true);
947 }
948
949 // Adding two positive MPT amounts
950 {
951 STAmount const amt1(mpt, 500);
952 STAmount const amt2(mpt, 1500);
953 BEAST_EXPECT(canAdd(amt1, amt2) == true);
954 }
955
956 // Adding two negative MPT amounts
957 {
958 STAmount const amt1(mpt, -500);
959 STAmount const amt2(mpt, -1500);
960 BEAST_EXPECT(canAdd(amt1, amt2) == true);
961 }
962
963 // Adding a positive and a negative MPT amount
964 {
965 STAmount const amt1(mpt, 1000);
966 STAmount const amt2(mpt, -1000);
967 BEAST_EXPECT(canAdd(amt1, amt2) == true);
968 }
969
970 // Overflow check for max MPT amounts
971 {
973 STAmount const amt2(mpt, 1);
974 BEAST_EXPECT(canAdd(amt1, amt2) == false);
975 BEAST_EXPECT(except<std::overflow_error>([&] { (void)(amt1 + amt2); }));
976 }
977
978 // Overflow check for min MPT amounts
979 // Note: Cannot check min MPT overflow because you cannot initialize the
980 // STAmount with a negative MPT amount.
981
982 // Adding MPT and XRP
983 {
984 STAmount const amt1(XRPAmount(1000));
985 STAmount const amt2(mpt, 1000);
986 BEAST_EXPECT(canAdd(amt1, amt2) == false);
987 }
988
989 // Adding different MPT issues (non zero)
990 {
991 STAmount const amt1(mpt2, 500);
992 STAmount const amt2(mpt, 500);
993 BEAST_EXPECT(canAdd(amt1, amt2) == false);
994 }
995
996 // Adding different MPT issues (non zero)
997 {
998 STAmount const amt1(mpt2, 0);
999 STAmount const amt2(mpt, 500);
1000 BEAST_EXPECT(canAdd(amt1, amt2) == false);
1001 }
1002 }
1003
1004 void
1006 {
1007 testcase("MPT transfer rate rounding uses Number arithmetic");
1008
1009 MPTIssue const asset{makeMptID(1, AccountID(0x4985601))};
1010 Rate const transferRate{1'500'000'000};
1011 STAmount const largeAmount{asset, UINT64_C(1'230'000'000'000'000'000)};
1012 STAmount const scaledAmount{asset, UINT64_C(1'845'000'000'000'000'000)};
1013
1014 auto const rules = [](bool const mptV2) {
1015 // Rules keeps a reference to the presets set, so use static
1016 // storage here rather than a local temporary.
1017 static std::unordered_set<UInt256, beast::Uhash<>> const kNoFeatures;
1018 static std::unordered_set<UInt256, beast::Uhash<>> const kMptV2Features{
1019 featureMPTokensV2,
1020 };
1021 return Rules{mptV2 ? kMptV2Features : kNoFeatures};
1022 };
1023
1024 auto const throwsOverflow = [&](auto&& f, bool expected = true) {
1025 bool threw = false;
1026 try
1027 {
1028 f();
1029 }
1030 catch (std::overflow_error const&)
1031 {
1032 threw = true;
1033 }
1034 BEAST_EXPECT(threw == expected);
1035 };
1036
1037 {
1038 CurrentTransactionRulesGuard const rg(rules(false));
1039
1040 throwsOverflow([&] { (void)multiplyRound(largeAmount, transferRate, asset, true); });
1041 throwsOverflow([&] { (void)divideRound(scaledAmount, transferRate, asset, true); });
1042 }
1043
1044 {
1045 CurrentTransactionRulesGuard const rg(rules(true));
1046
1047 throwsOverflow(
1048 [&] { (void)multiplyRound(largeAmount, transferRate, asset, true); }, false);
1049 throwsOverflow(
1050 [&] { (void)divideRound(scaledAmount, transferRate, asset, true); }, false);
1051 }
1052
1053 {
1054 CurrentTransactionRulesGuard const rg(rules(true));
1055 STAmount const one{asset, 1};
1056 STAmount const two{asset, 2};
1057
1058 BEAST_EXPECT(multiplyRound(one, transferRate, asset, true) == two);
1059 BEAST_EXPECT(multiplyRound(one, transferRate, asset, false) == one);
1060 BEAST_EXPECT(divideRound(two, transferRate, asset, true) == two);
1061 BEAST_EXPECT(divideRound(two, transferRate, asset, false) == one);
1062
1063 BEAST_EXPECT(multiplyRound(largeAmount, transferRate, asset, true) == scaledAmount);
1064 BEAST_EXPECT(divideRound(scaledAmount, transferRate, asset, true) == largeAmount);
1065 }
1066
1067 {
1068 // mulRound with an integral (XRP) operand whose mantissa is below
1069 // kMinValue exercises the legacy value-scaling loop that normalizes
1070 // the mantissa before multiply. The MPTokensV2 Number path is
1071 // not taken here because the target asset is an IOU.
1072 Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
1073 STAmount const iouVal{usd, 5};
1074 STAmount const xrpVal{XRPAmount{7}}; // integral, mantissa < kMinValue
1075
1076 auto const up = mulRound(iouVal, xrpVal, usd, /*roundUp*/ true);
1077 auto const down = mulRound(iouVal, xrpVal, usd, /*roundUp*/ false);
1078 BEAST_EXPECT(down.signum() > 0);
1079 BEAST_EXPECT(up >= down);
1080 }
1081
1082 {
1083 // An MPT operand with a non-MPT result. The legacy path assumes
1084 // 16-digit mantissas and overflows on a 63-bit MPT mantissa; under
1085 // MPTokensV2 any MPT operand, not only an MPT result, takes the
1086 // Number path. This is OfferCreate::flowCross recomputing the
1087 // remainder of a partially crossed large MPT offer, and
1088 // Quality::ceilIn/ceilOut partially filling one.
1089 Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
1090 STAmount const bigMpt{asset, UINT64_C(5'000'000'000'000'000'000)};
1091 STAmount const milli{noIssue(), UINT64_C(1'000'000'000'000'000), -18}; // 1e-3
1092 STAmount const kilo{noIssue(), UINT64_C(1'000'000'000'000'000), -12}; // 1e3
1093 STAmount const tenMicro{noIssue(), UINT64_C(1'000'000'000'000'000), -20}; // 1e-5
1094 STAmount const usdResult{usd, UINT64_C(5'000'000'000'000'000)};
1095 XRPAmount const xrpResult{50'000'000'000'000};
1096
1097 {
1098 CurrentTransactionRulesGuard const rg(rules(false));
1099 throwsOverflow([&] { (void)mulRound(bigMpt, milli, usd, true); });
1100 throwsOverflow([&] { (void)mulRound(bigMpt, tenMicro, xrpIssue(), true); });
1101 throwsOverflow([&] { (void)divRound(bigMpt, kilo, usd, true); });
1102 throwsOverflow([&] { (void)divRoundStrict(bigMpt, kilo, usd, false); });
1103 }
1104
1105 {
1106 CurrentTransactionRulesGuard const rg(rules(true));
1107 BEAST_EXPECT(mulRound(bigMpt, milli, usd, true) == usdResult);
1108 BEAST_EXPECT(mulRound(bigMpt, milli, usd, false) == usdResult);
1109 BEAST_EXPECT(mulRound(bigMpt, tenMicro, xrpIssue(), true) == STAmount{xrpResult});
1110 BEAST_EXPECT(divRound(bigMpt, kilo, usd, true) == usdResult);
1111 BEAST_EXPECT(divRoundStrict(bigMpt, kilo, usd, false) == usdResult);
1112 }
1113 }
1114 }
1115
1116 void
1118 {
1119 testcase("can subtract xrp");
1120
1121 // Subtracting zero
1122 {
1123 STAmount const amt1(XRPAmount(1000));
1124 STAmount const amt2(XRPAmount(0));
1125 BEAST_EXPECT(canSubtract(amt1, amt2) == true);
1126 }
1127
1128 // Subtracting zero
1129 {
1130 STAmount const amt1(XRPAmount(0));
1131 STAmount const amt2(XRPAmount(1000));
1132 BEAST_EXPECT(canSubtract(amt1, amt2) == false);
1133 }
1134
1135 // Subtracting two positive XRP amounts
1136 {
1137 STAmount const amt1(XRPAmount(1500));
1138 STAmount const amt2(XRPAmount(500));
1139 BEAST_EXPECT(canSubtract(amt1, amt2) == true);
1140 }
1141
1142 // Subtracting two negative XRP amounts
1143 {
1144 STAmount const amt1(XRPAmount(-1500));
1145 STAmount const amt2(XRPAmount(-500));
1146 BEAST_EXPECT(canSubtract(amt1, amt2) == true);
1147 }
1148
1149 // Subtracting a positive and a negative XRP amount
1150 {
1151 STAmount const amt1(XRPAmount(1000));
1152 STAmount const amt2(XRPAmount(-1000));
1153 BEAST_EXPECT(canSubtract(amt1, amt2) == true);
1154 }
1155
1156 // Underflow check for min XRP amounts
1157 {
1158 STAmount const amt1(
1160 true);
1161 STAmount const amt2(XRPAmount(2));
1162 BEAST_EXPECT(canSubtract(amt1, amt2) == false);
1163 BEAST_EXPECT(except<std::overflow_error>([&] { (void)(amt1 - amt2); }));
1164 }
1165
1166 // Overflow check for max XRP amounts
1167 {
1169 STAmount const amt2(XRPAmount(-1));
1170 BEAST_EXPECT(canSubtract(amt1, amt2) == false);
1171 BEAST_EXPECT(except<std::overflow_error>([&] { (void)(amt1 - amt2); }));
1172 }
1173 }
1174
1175 void
1177 {
1178 testcase("can subtract iou");
1179 Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
1180 Issue const eur{Currency(0x4555520000000000), AccountID(0x4985601)};
1181
1182 // Subtracting two IOU amounts
1183 {
1184 STAmount const amt1(usd, 1500);
1185 STAmount const amt2(usd, 500);
1186 BEAST_EXPECT(canSubtract(amt1, amt2) == true);
1187 }
1188
1189 // Subtracting XRP and IOU
1190 {
1191 STAmount const amt1(XRPAmount(1000));
1192 STAmount const amt2(usd, 1000);
1193 BEAST_EXPECT(canSubtract(amt1, amt2) == false);
1194 }
1195
1196 // Subtracting different IOU issues (non zero)
1197 {
1198 STAmount const amt1(usd, 1000);
1199 STAmount const amt2(eur, 500);
1200 BEAST_EXPECT(canSubtract(amt1, amt2) == false);
1201 }
1202
1203 // Subtracting different IOU issues (zero)
1204 {
1205 STAmount const amt1(usd, 0);
1206 STAmount const amt2(eur, 500);
1207 BEAST_EXPECT(canSubtract(amt1, amt2) == false);
1208 }
1209 }
1210
1211 void
1213 {
1214 testcase("can subtract mpt");
1215
1216 MPTIssue const mpt{MPTIssue{makeMptID(1, AccountID(0x4985601))}};
1217 MPTIssue const mpt2{MPTIssue{makeMptID(2, AccountID(0x4985601))}};
1218
1219 // Subtracting zero
1220 {
1221 STAmount const amt1(mpt, 1000);
1222 STAmount const amt2(mpt, 0);
1223 BEAST_EXPECT(canSubtract(amt1, amt2) == true);
1224 }
1225
1226 // Subtracting zero
1227 {
1228 STAmount const amt1(mpt, 0);
1229 STAmount const amt2(mpt, 1000);
1230 BEAST_EXPECT(canSubtract(amt1, amt2) == false);
1231 }
1232
1233 // Subtracting two positive MPT amounts
1234 {
1235 STAmount const amt1(mpt, 1500);
1236 STAmount const amt2(mpt, 500);
1237 BEAST_EXPECT(canSubtract(amt1, amt2) == true);
1238 }
1239
1240 // Subtracting two negative MPT amounts
1241 {
1242 STAmount const amt1(mpt, -1500);
1243 STAmount const amt2(mpt, -500);
1244 BEAST_EXPECT(canSubtract(amt1, amt2) == true);
1245 }
1246
1247 // Subtracting a positive and a negative MPT amount
1248 {
1249 STAmount const amt1(mpt, 1000);
1250 STAmount const amt2(mpt, -1000);
1251 BEAST_EXPECT(canSubtract(amt1, amt2) == true);
1252 }
1253
1254 // Underflow check for min MPT amounts
1255 // Note: Cannot check min MPT underflow because you cannot initialize
1256 // the STAmount with a negative MPT amount.
1257
1258 // Overflow check for max positive MPT amounts (should fail)
1259 {
1261 STAmount const amt2(mpt, -2);
1262 BEAST_EXPECT(canSubtract(amt1, amt2) == false);
1263 }
1264
1265 // Subtracting MPT and XRP
1266 {
1267 STAmount const amt1(XRPAmount(1000));
1268 STAmount const amt2(mpt, 1000);
1269 BEAST_EXPECT(canSubtract(amt1, amt2) == false);
1270 }
1271
1272 // Subtracting different MPT issues (non zero)
1273 {
1274 STAmount const amt1(mpt, 1000);
1275 STAmount const amt2(mpt2, 500);
1276 BEAST_EXPECT(canSubtract(amt1, amt2) == false);
1277 }
1278
1279 // Subtracting different MPT issues (zero)
1280 {
1281 STAmount const amt1(mpt, 0);
1282 STAmount const amt2(mpt2, 500);
1283 BEAST_EXPECT(canSubtract(amt1, amt2) == false);
1284 }
1285 }
1286
1287 void
1289 {
1290 testcase("isZeroAtScale");
1291
1292 Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
1293
1294 // IOU: 10 IOU — mantissa = kMinValue (10^15), exponent = -14.
1295 // One ULP at this scale is 10^-14; half-ULP is 5*10^-15.
1296 {
1297 STAmount const ref{usd, STAmount::kMinValue, -14};
1298 int const refScale = ref.exponent(); // -14
1299 BEAST_EXPECT(refScale == -14);
1300
1301 // Zero rounds to zero at any scale.
1302 STAmount const iouZero{usd, 0};
1303 BEAST_EXPECT(iouZero.isZeroAtScale(refScale));
1304
1305 // Sub-ULP: 1e-16 IOU (mantissa = kMinValue, exponent = -31).
1306 // Far below half-ULP → rounds to zero.
1307 STAmount const subUlp{usd, STAmount::kMinValue, -31};
1308 BEAST_EXPECT(subUlp.isZeroAtScale(refScale));
1309
1310 // One ULP: 1e-14 IOU (mantissa = kMinValue, exponent = -29).
1311 // Exactly the smallest representable unit at refScale → not zero.
1312 STAmount const oneUlp{usd, STAmount::kMinValue, -29};
1313 BEAST_EXPECT(!oneUlp.isZeroAtScale(refScale));
1314
1315 // The reference value itself: exponent == scale → returned
1316 // unchanged → not zero.
1317 BEAST_EXPECT(!ref.isZeroAtScale(refScale));
1318
1319 // A much larger value: certainly not zero at this scale.
1320 STAmount const large{usd, STAmount::kMinValue, 0}; // 1e15 IOU
1321 BEAST_EXPECT(!large.isZeroAtScale(refScale));
1322
1323 // When scale equals the value's own exponent, roundToScale
1324 // short-circuits and returns the value unchanged.
1325 BEAST_EXPECT(!subUlp.isZeroAtScale(subUlp.exponent()));
1326 BEAST_EXPECT(!oneUlp.isZeroAtScale(oneUlp.exponent()));
1327
1328 // Half-ULP boundary. roundToScale forms (value + ref) - ref
1329 // where ref = 10 IOU has mantissa 1e15 (LSB 0, even).
1330 // Number's default rounding is to-nearest-even, so an exact
1331 // half-ULP tie rounds toward the even-LSB neighbour — the
1332 // reference itself — and the round-trip result is zero.
1333 // Just below half-ULP rounds the same way; just above
1334 // clears half-ULP and bumps the mantissa to 1e15 + 1.
1335 STAmount const justBelowHalf{usd, STAmount::kMinValue * 4, -30};
1336 BEAST_EXPECT(justBelowHalf.isZeroAtScale(refScale));
1337
1338 STAmount const halfUlp{usd, STAmount::kMinValue * 5, -30};
1339 BEAST_EXPECT(halfUlp.isZeroAtScale(refScale));
1340
1341 STAmount const justAboveHalf{usd, STAmount::kMinValue * 6, -30};
1342 BEAST_EXPECT(!justAboveHalf.isZeroAtScale(refScale));
1343
1344 // Large magnitude gap: dust value far below an enormous scale.
1345 // 1e-80 with scale +15 — the value vanishes utterly.
1346 STAmount const dust{usd, STAmount::kMinValue, -95};
1347 BEAST_EXPECT(dust.isZeroAtScale(15));
1348
1349 // Negative values mirror positive behaviour.
1350 STAmount const negSubUlp{usd, STAmount::kMinValue, -31, true};
1351 BEAST_EXPECT(negSubUlp.isZeroAtScale(refScale));
1352
1353 STAmount const negOneUlp{usd, STAmount::kMinValue, -29, true};
1354 BEAST_EXPECT(!negOneUlp.isZeroAtScale(refScale));
1355 }
1356
1357 // XRP is integral — roundToScale short-circuits, value is preserved.
1358 {
1359 STAmount const xrp{XRPAmount{1}};
1360 BEAST_EXPECT(!xrp.isZeroAtScale(-14));
1361 BEAST_EXPECT(!xrp.isZeroAtScale(0));
1362
1363 STAmount const xrpZero{XRPAmount{0}};
1364 BEAST_EXPECT(xrpZero.isZeroAtScale(-14));
1365 }
1366
1367 // MPT is integral — same short-circuit behaviour as XRP.
1368 {
1369 MPTIssue const mpt{makeMptID(1, AccountID(0x4985601))};
1370 STAmount const mptAmt{mpt, 1};
1371 BEAST_EXPECT(!mptAmt.isZeroAtScale(0));
1372 BEAST_EXPECT(!mptAmt.isZeroAtScale(-14));
1373
1374 STAmount const mptZero{mpt, 0};
1375 BEAST_EXPECT(mptZero.isZeroAtScale(0));
1376 }
1377 }
1378
1379 //--------------------------------------------------------------------------
1380
1381 void
1383 {
1384 testcase("MPT operand with IOU result does not wrap negative");
1385
1386 // A partially funded offer selling 5e17 MPT for 1,000 USD, with the
1387 // owner holding 4.8e17 MPT, is reduced by multiplying the funds by the
1388 // offer's rate (2e-15 USD per MPT). The legacy 16-digit path takes the
1389 // unscaled MPT mantissa: 4.8e17 * 2e15 / 1e14 = 9.6e18, which fits in
1390 // uint64 but not in int64, so the IOU result wrapped to a negative
1391 // amount without throwing. Under MPTokensV2 any MPT operand uses Number
1392 // arithmetic.
1393 MPTIssue const s{makeMptID(1, AccountID(0x4985601))};
1394 Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
1395 STAmount const funds{s, UINT64_C(480'000'000'000'000'000)};
1396 STAmount const rate{noIssue(), UINT64_C(2'000'000'000'000'000), -30};
1397 STAmount const invRate{noIssue(), UINT64_C(5'000'000'000'000'000), -1};
1398 STAmount const expected{usd, 960};
1399
1400 auto const rules = [](bool const mptV2) {
1401 // Rules keeps a reference to the presets set, so use static
1402 // storage here rather than a local temporary.
1403 static std::unordered_set<UInt256, beast::Uhash<>> const kNoFeatures;
1404 static std::unordered_set<UInt256, beast::Uhash<>> const kMptV2Features{
1405 featureMPTokensV2,
1406 };
1407 return Rules{mptV2 ? kMptV2Features : kNoFeatures};
1408 };
1409
1410 {
1411 CurrentTransactionRulesGuard const rg(rules(false));
1412
1413 BEAST_EXPECT(mulRoundStrict(funds, rate, usd, false).signum() < 0);
1414 BEAST_EXPECT(mulRound(funds, rate, usd, false).signum() < 0);
1415 BEAST_EXPECT(divRoundStrict(funds, invRate, usd, false).signum() < 0);
1416 }
1417
1418 {
1419 CurrentTransactionRulesGuard const rg(rules(true));
1420
1421 for (bool const roundUp : {false, true})
1422 {
1423 BEAST_EXPECT(mulRoundStrict(funds, rate, usd, roundUp) == expected);
1424 BEAST_EXPECT(mulRound(funds, rate, usd, roundUp) == expected);
1425 BEAST_EXPECT(divRoundStrict(funds, invRate, usd, roundUp) == expected);
1426 BEAST_EXPECT(divRound(funds, invRate, usd, roundUp) == expected);
1427 }
1428 }
1429 }
1430
1431 void
1433 {
1434 testcase("getRate with a large integral numerator");
1435
1436 MPTIssue const asset{makeMptID(1, AccountID(0x4985601))};
1437 Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
1438
1439 auto const rules = [](bool const mptV2) {
1440 static std::unordered_set<uint256, beast::Uhash<>> const kNoFeatures;
1441 // MPTokensV2 with the large Number mantissa (SAV enables it).
1442 static std::unordered_set<uint256, beast::Uhash<>> const kMptV2Features{
1443 featureMPTokensV2,
1444 featureSingleAssetVault,
1445 fixCleanup3_3_0,
1446 };
1447 return Rules{mptV2 ? kMptV2Features : kNoFeatures};
1448 };
1449
1450 // TakerPays / TakerGets = 184467440737095516 / 1e-7. The legacy
1451 // divide() quotient is 2^64 - 11, which the signed IOU mantissa reads
1452 // as -11: getRate records 1.1e6 instead of ~1.8e24.
1453 STAmount const dust{usd, UINT64_C(1'000'000'000'000'000), -22}; // 1e-7
1454 STAmount const largeMpt{asset, UINT64_C(184'467'440'737'095'516)};
1455 STAmount const trueRate{noIssue(), Number{largeMpt} / Number{dust}};
1456
1457 // Quotient above 2^64: muldiv throws, so getRate returns 0
1458 // pre-amendment.
1459 STAmount const one{usd, UINT64_C(1'000'000'000'000'000), -15};
1460 STAmount const hugeMpt{asset, UINT64_C(5'000'000'000'000'000'000)};
1461 STAmount const hugeMptRate{noIssue(), UINT64_C(5'000'000'000'000'000), 3};
1462
1463 // Over the smallest IOU the rate is beyond the IOU exponent range, so
1464 // getRate returns 0 either way.
1466
1467 // An integral result keeps every digit, so it must come out exact.
1468 STAmount const maxMpt{asset, static_cast<std::uint64_t>(kMaxMpTokenAmount)};
1469 STAmount const oneAndHalf{noIssue(), UINT64_C(1'500'000'000'000'000), -15};
1470 STAmount const e18Mpt{asset, UINT64_C(1'000'000'000'000'000'000)};
1471
1472 // XRP numerator in the same band: 9.5e16 drops over a mantissa of
1473 // 1e15 gives a legacy quotient of 9.5e18.
1474 STAmount const hugeXrp{XRPAmount{95'000'000'000'000'000}};
1475 STAmount const hugeXrpRate{noIssue(), UINT64_C(9'500'000'000'000'000), 1};
1476
1477 {
1478 CurrentTransactionRulesGuard const rg(rules(false));
1479
1480 BEAST_EXPECT(
1481 amountFromQuality(getRate(dust, largeMpt)) ==
1482 STAmount(noIssue(), UINT64_C(1'100'000'000'000'000), -9));
1483 BEAST_EXPECT(getRate(one, hugeMpt) == 0);
1484 BEAST_EXPECT(getRate(tiny, hugeMpt) == 0);
1485 BEAST_EXPECT(getRate(one, hugeXrp) != 0);
1486 BEAST_EXPECT(amountFromQuality(getRate(one, hugeXrp)) != hugeXrpRate);
1487
1488 bool threw = false;
1489 try
1490 {
1491 (void)divide(hugeMpt, one, asset);
1492 }
1493 catch (std::overflow_error const&)
1494 {
1495 threw = true;
1496 }
1497 BEAST_EXPECT(threw);
1498 }
1499
1500 {
1501 CurrentTransactionRulesGuard const rg(rules(true));
1502
1503 BEAST_EXPECT(amountFromQuality(getRate(dust, largeMpt)) == trueRate);
1504 BEAST_EXPECT(amountFromQuality(getRate(one, hugeMpt)) == hugeMptRate);
1505 BEAST_EXPECT(getRate(tiny, hugeMpt) == 0);
1506 BEAST_EXPECT(amountFromQuality(getRate(one, hugeXrp)) == hugeXrpRate);
1507 BEAST_EXPECT(!divide(largeMpt, dust, noIssue()).negative());
1508
1509 BEAST_EXPECT(divide(hugeMpt, one, asset) == hugeMpt);
1510 BEAST_EXPECT(divide(maxMpt, one, asset) == maxMpt);
1511 BEAST_EXPECT(
1512 divide(STAmount{asset, UINT64_C(9'000'000'000'000'000'000)}, oneAndHalf, asset) ==
1513 STAmount(asset, UINT64_C(6'000'000'000'000'000'000)));
1514
1515 // An unscaled MPT denominator no longer moves the +5 nudge into
1516 // the 15th digit: 9e15 / 1e18 is exactly 0.009.
1517 BEAST_EXPECT(
1518 divide(STAmount{usd, UINT64_C(9'000'000'000'000'000)}, e18Mpt, noIssue()) ==
1519 STAmount(noIssue(), UINT64_C(9'000'000'000'000'000), -18));
1520 }
1521
1522 // No rules (RPC): same as post-amendment.
1523 BEAST_EXPECT(amountFromQuality(getRate(dust, largeMpt)) == trueRate);
1524 BEAST_EXPECT(amountFromQuality(getRate(one, hugeMpt)) == hugeMptRate);
1525 BEAST_EXPECT(amountFromQuality(getRate(one, hugeXrp)) == hugeXrpRate);
1526 }
1527
1528 void
1530 {
1531 testcase("Rounded-up MPT operand with an IOU result below the minimum");
1532
1533 MPTIssue const asset{makeMptID(1, AccountID(0x4985601))};
1534 Issue const usd{Currency(0x5553440000000000), AccountID(0x4985601)};
1535
1536 auto const rules = [](bool const mptV2) {
1537 static std::unordered_set<uint256, beast::Uhash<>> const kNoFeatures;
1538 static std::unordered_set<uint256, beast::Uhash<>> const kMptV2Features{
1539 featureMPTokensV2,
1540 };
1541 return Rules{mptV2 ? kMptV2Features : kNoFeatures};
1542 };
1543
1544 // 1 MPT / 1e95 = 1e-95, below the smallest IOU (1e-81). Rounding up
1545 // must still give the smallest positive IOU.
1546 STAmount const oneMpt{asset, 1};
1548 STAmount const smallestUsd{usd, STAmount::kMinValue, STAmount::kMinOffset};
1549
1550 {
1551 CurrentTransactionRulesGuard const rg(rules(false));
1552 BEAST_EXPECT(divRound(oneMpt, huge, usd, true) == smallestUsd);
1553 }
1554
1555 {
1556 // An MPT operand now takes the Number path even though the result
1557 // is an IOU. Materializing 1e-95 as an IOU flushes to zero, so
1558 // roundNumberResult snaps it up to the smallest positive IOU.
1559 CurrentTransactionRulesGuard const rg(rules(true));
1560 BEAST_EXPECT(divRound(oneMpt, huge, usd, true) == smallestUsd);
1561 BEAST_EXPECT(divRoundStrict(oneMpt, huge, usd, true) == smallestUsd);
1562 }
1563 }
1564
1565 void
1589};
1590
1592
1593} // namespace xrpl
A testsuite class.
Definition suite.h:52
bool except(F &&f, String const &reason)
Definition suite.h:465
bool unexpected(Condition shouldBeFalse, String const &reason)
DEPRECATED.
Definition suite.h:516
void pass()
Record a successful test condition.
Definition suite.h:532
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
LogOs< char > log
Logging output stream.
Definition suite.h:150
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:121
RAII class to set and restore the current transaction rules.
Definition Rules.h:113
A currency issued by an account.
Definition Issue.h:18
Currency currency
Definition Issue.h:20
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
Rules controlling protocol behavior.
Definition Rules.h:40
static STAmount roundSelf(STAmount const &amount)
void testSetValue(std::string const &value, Issue const &issue, bool success=true)
void run() override
Runs the suite.
void mulTest(int a, int b)
static STAmount serializeAndDeserialize(STAmount const &s)
void roundTest(int n, int d, int m)
constexpr TIss const & get() const
std::string getFullText() const override
void add(Serializer &s) const override
std::uint64_t mantissa() const noexcept
Definition STAmount.h:490
std::string getText() const override
static constexpr int kMinOffset
Definition STAmount.h:61
SerializedTypeID getSType() const override
bool negative() const noexcept
Definition STAmount.h:484
bool isDefault() const override
bool native() const noexcept
Definition STAmount.h:471
Asset const & asset() const
Definition STAmount.h:496
int exponent() const noexcept
Definition STAmount.h:459
bool isZeroAtScale(int scale) const
Checks if this amount evaluates to zero when constrained to a specific accounting scale.
static constexpr std::uint64_t kMinValue
Definition STAmount.h:65
static constexpr std::uint64_t kMaxValue
Definition STAmount.h:67
static constexpr std::uint64_t kMaxNative
Definition STAmount.h:69
static constexpr int kMaxOffset
Definition STAmount.h:62
STAmount const & value() const noexcept
Definition STAmount.h:610
Slice slice() const noexcept
Definition Serializer.h:141
T is_convertible_v
T max(T... args)
T min(T... args)
constexpr Zero kZero
Definition Zero.h:30
unsigned int UInt
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
STAmount divide(STAmount const &amount, Rate const &rate)
Definition Rate2.cpp:69
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
STAmount mulRoundStrict(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
SField const sfGeneric
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
STAmount amountFromString(Asset const &asset, std::string const &amount)
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
STAmount divRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
bool canAdd(STAmount const &amt1, STAmount const &amt2)
Safely checks if two STAmount values can be added without overflow, underflow, or precision loss.
STAmount amountFromQuality(std::uint64_t rate)
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
STAmount multiplyRound(STAmount const &amount, Rate const &rate, bool roundUp)
Definition Rate2.cpp:45
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
STAmount amountFromJson(SField const &name, json::Value const &v)
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:219
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
STAmount mulRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
bool canSubtract(STAmount const &amt1, STAmount const &amt2)
Determines if it is safe to subtract one STAmount from another.
STAmount divRoundStrict(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition Issue.h:118
STAmount multiply(STAmount const &amount, Number const &frac, Number::RoundingMode rm)
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
Definition Protocol.h:297
STAmount divideRound(STAmount const &amount, Rate const &rate, bool roundUp)
Definition Rate2.cpp:80
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
Represents a transfer rate.
Definition Rate.h:21
T to_string(T... args)
T what(T... args)