xrpld
Loading...
Searching...
No Matches
tests/libxrpl/basics/XRPAmount.cpp
1#include <xrpl/protocol/XRPAmount.h>
2
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/beast/hash/uhash.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/Rules.h>
8#include <xrpl/protocol/SystemParameters.h>
9
10#include <gtest/gtest.h>
11
12#include <cstdint>
13#include <limits>
14#include <stdexcept>
15#include <unordered_set>
16
17namespace xrpl {
18
19TEST(XRPAmountTest, sig_num)
20{
21 for (auto const i : {-1, 0, 1})
22 {
23 XRPAmount const x(i);
24
25 if (i < 0)
26 {
27 EXPECT_TRUE(x.signum() < 0);
28 }
29 else if (i > 0)
30 {
31 EXPECT_TRUE(x.signum() > 0);
32 }
33 else
34 {
35 EXPECT_EQ(x.signum(), 0);
36 }
37 }
38}
39
40TEST(XRPAmountTest, beast_zero)
41{
42 using beast::kZero;
43
44 for (auto const i : {-1, 0, 1})
45 {
46 XRPAmount const x(i);
47
48 EXPECT_TRUE((i == 0) == (x == kZero));
49 EXPECT_TRUE((i != 0) == (x != kZero));
50 EXPECT_TRUE((i < 0) == (x < kZero));
51 EXPECT_TRUE((i > 0) == (x > kZero));
52 EXPECT_TRUE((i <= 0) == (x <= kZero));
53 EXPECT_TRUE((i >= 0) == (x >= kZero));
54
55 EXPECT_TRUE((0 == i) == (kZero == x));
56 EXPECT_TRUE((0 != i) == (kZero != x));
57 EXPECT_TRUE((0 < i) == (kZero < x));
58 EXPECT_TRUE((0 > i) == (kZero > x));
59 EXPECT_TRUE((0 <= i) == (kZero <= x));
60 EXPECT_TRUE((0 >= i) == (kZero >= x));
61 }
62}
63
64TEST(XRPAmountTest, comparisons)
65{
66 for (auto const i : {-1, 0, 1})
67 {
68 XRPAmount const x(i);
69
70 for (auto const j : {-1, 0, 1})
71 {
72 XRPAmount const y(j);
73
74 EXPECT_EQ((i == j), (x == y));
75 EXPECT_EQ((i != j), (x != y));
76 EXPECT_EQ((i < j), (x < y));
77 EXPECT_EQ((i > j), (x > y));
78 EXPECT_EQ((i <= j), (x <= y));
79 EXPECT_EQ((i >= j), (x >= y));
80 }
81 }
82}
83
84TEST(XRPAmountTest, add_sub)
85{
86 for (auto const i : {-1, 0, 1})
87 {
88 XRPAmount const x(i);
89
90 for (auto const j : {-1, 0, 1})
91 {
92 XRPAmount const y(j);
93
94 EXPECT_EQ(XRPAmount(i + j), (x + y));
95 EXPECT_EQ(XRPAmount(i - j), (x - y));
96
97 EXPECT_EQ((x + y), (y + x)); // addition is commutative
98 }
99 }
100}
101
102TEST(XRPAmountTest, decimal)
103{
104 // Tautology
105 EXPECT_EQ(kDropsPerXrp.decimalXRP(), 1);
106
107 XRPAmount test{1};
108 EXPECT_EQ(test.decimalXRP(), 0.000001);
109
110 test = -test;
111 EXPECT_EQ(test.decimalXRP(), -0.000001);
112
113 test = 100'000'000;
114 EXPECT_EQ(test.decimalXRP(), 100);
115
116 test = -test;
117 EXPECT_EQ(test.decimalXRP(), -100);
118}
119
120TEST(XRPAmountTest, functions)
121{
122 // Explicitly test every defined function for the XRPAmount class
123 // since some of them are templated, but not used anywhere else.
124 auto const make = [&](auto x) -> XRPAmount { return XRPAmount{x}; };
125
126 XRPAmount const defaulted{};
127 (void)defaulted;
128 XRPAmount test{0};
129 EXPECT_EQ(test.drops(), 0);
130
131 test = make(beast::kZero);
132 EXPECT_EQ(test.drops(), 0);
133
135 EXPECT_EQ(test.drops(), 0);
136
137 test = make(100);
138 EXPECT_EQ(test.drops(), 100);
139
140 test = make(100u);
141 EXPECT_EQ(test.drops(), 100);
142
143 XRPAmount const targetSame{200u};
144 test = make(targetSame);
145 EXPECT_EQ(test.drops(), 200);
146 EXPECT_EQ(test, targetSame);
147 EXPECT_TRUE(test < XRPAmount{1000});
148 EXPECT_TRUE(test > XRPAmount{100});
149
150 test = std::int64_t(200);
151 EXPECT_EQ(test.drops(), 200);
152 test = std::uint32_t(300);
153 EXPECT_EQ(test.drops(), 300);
154
155 test = targetSame;
156 EXPECT_EQ(test.drops(), 200);
157 auto testOther = test.dropsAs<std::uint32_t>();
158 EXPECT_TRUE(testOther);
159 EXPECT_EQ(*testOther, 200); // NOLINT(bugprone-unchecked-optional-access)
161 testOther = test.dropsAs<std::uint32_t>();
162 EXPECT_FALSE(testOther);
163 test = -1;
164 testOther = test.dropsAs<std::uint32_t>();
165 EXPECT_FALSE(testOther);
166
167 test = targetSame * 2;
168 EXPECT_EQ(test.drops(), 400);
169 test = 3 * targetSame;
170 EXPECT_EQ(test.drops(), 600);
171 test = 20;
172 EXPECT_EQ(test.drops(), 20);
173
174 test += targetSame;
175 EXPECT_EQ(test.drops(), 220);
176
177 test -= targetSame;
178 EXPECT_EQ(test.drops(), 20);
179
180 test *= 5;
181 EXPECT_EQ(test.drops(), 100);
182 test = 50;
183 EXPECT_EQ(test.drops(), 50);
184 test -= 39;
185 EXPECT_EQ(test.drops(), 11);
186
187 // legal with signed
188 test = -test;
189 EXPECT_EQ(test.drops(), -11);
190 EXPECT_EQ(test.signum(), -1);
191 EXPECT_EQ(to_string(test), "-11");
192
193 EXPECT_TRUE(test);
194 test = 0;
195 EXPECT_FALSE(test);
196 EXPECT_EQ(test.signum(), 0);
197 test = targetSame;
198 EXPECT_EQ(test.signum(), 1);
199 EXPECT_EQ(to_string(test), "200");
200}
201
202class XRPAmountMulRatioTest : public ::testing::Test
203{
204protected:
208};
209
210TEST_F(XRPAmountMulRatioTest, scaling_the_maximum_by_one_is_lossless)
211{
212 // The intermediate product overflows; mulRatio must not lose value anyway.
213 XRPAmount const big(kMaxXrp);
214
215 EXPECT_EQ(big, mulRatio(big, kMaxUInt32, kMaxUInt32, true));
216 // Rounding mode shouldn't matter as the result is exact.
217 EXPECT_EQ(big, mulRatio(big, kMaxUInt32, kMaxUInt32, false));
218}
219
220TEST_F(XRPAmountMulRatioTest, three_quarters_of_the_maximum_avoids_overflow)
221{
222 XRPAmount big(kMaxXrp);
223 big -= 0xf; // Subtract a little so it's divisible by 4
224
225 EXPECT_EQ(mulRatio(big, 3, 4, false).value(), (big.value() / 4) * 3);
226 EXPECT_EQ(mulRatio(big, 3, 4, true).value(), (big.value() / 4) * 3);
227
228 // The premises the two checks above rest on: exact division by 4, and an
229 // input large enough that a naive multiply by 3 really would overflow.
230 EXPECT_EQ(big.value() % 4, 0);
231 EXPECT_GT(big.value(), kMaxXrp / 3);
232 EXPECT_LE(big.value() / 4, kMaxXrp / 3);
233}
234
235TEST_F(XRPAmountMulRatioTest, scaling_the_minimum_by_one_is_lossless)
236{
237 XRPAmount const big(kMinXrp); // NOLINT TODO
238
239 EXPECT_EQ(big, mulRatio(big, kMaxUInt32, kMaxUInt32, true));
240 // Rounding mode shouldn't matter as the result is exact.
241 EXPECT_EQ(big, mulRatio(big, kMaxUInt32, kMaxUInt32, false));
242}
243
244TEST_F(XRPAmountMulRatioTest, three_quarters_of_the_minimum_avoids_overflow)
245{
246 XRPAmount const big(kMinXrp); // NOLINT TODO
247
248 EXPECT_EQ(mulRatio(big, 3, 4, false).value(), (big.value() / 4) * 3);
249 EXPECT_EQ(mulRatio(big, 3, 4, true).value(), (big.value() / 4) * 3);
250
251 EXPECT_EQ(big.value() % 4, 0);
252 EXPECT_LT(big.value(), kMinXrp / 3);
253 EXPECT_GE(big.value() / 4, kMinXrp / 3);
254}
255
256TEST_F(XRPAmountMulRatioTest, a_single_drop_rounds_up_to_a_drop_and_down_to_zero)
257{
258 XRPAmount const tiny(1);
259
260 EXPECT_EQ(tiny, mulRatio(tiny, 1, kMaxUInt32, true));
261 EXPECT_EQ(beast::kZero, mulRatio(tiny, 1, kMaxUInt32, false));
262 EXPECT_EQ(beast::kZero, mulRatio(tiny, kMaxUInt32 - 1, kMaxUInt32, false));
263}
264
265TEST_F(XRPAmountMulRatioTest, a_single_negative_drop_rounds_up_to_zero_and_down_to_a_drop)
266{
267 XRPAmount const tinyNeg(-1);
268
269 // "Up" is towards zero for a negative amount.
270 EXPECT_EQ(beast::kZero, mulRatio(tinyNeg, 1, kMaxUInt32, true));
271 EXPECT_EQ(beast::kZero, mulRatio(tinyNeg, kMaxUInt32 - 1, kMaxUInt32, true));
272 EXPECT_EQ(tinyNeg, mulRatio(tinyNeg, kMaxUInt32 - 1, kMaxUInt32, false));
273}
274
275TEST_F(XRPAmountMulRatioTest, rounding_up_and_down_a_drop_differ_by_one_drop)
276{
277 XRPAmount const one(1);
278
279 auto const roundedUp = mulRatio(one, kMaxUInt32 - 1, kMaxUInt32, true);
280 auto const roundedDown = mulRatio(one, kMaxUInt32 - 1, kMaxUInt32, false);
281 EXPECT_EQ(roundedUp.drops() - roundedDown.drops(), 1);
282}
283
284TEST_F(XRPAmountMulRatioTest, rounding_up_and_down_the_maximum_differ_by_one_drop)
285{
286 XRPAmount const big(kMaxXrp);
287
288 auto const roundedUp = mulRatio(big, kMaxUInt32 - 1, kMaxUInt32, true);
289 auto const roundedDown = mulRatio(big, kMaxUInt32 - 1, kMaxUInt32, false);
290 EXPECT_EQ(roundedUp.drops() - roundedDown.drops(), 1);
291}
292
293TEST_F(XRPAmountMulRatioTest, rounding_up_and_down_a_negative_drop_differ_by_one_drop)
294{
295 XRPAmount const negOne(-1);
296
297 auto const roundedUp = mulRatio(negOne, kMaxUInt32 - 1, kMaxUInt32, true);
298 auto const roundedDown = mulRatio(negOne, kMaxUInt32 - 1, kMaxUInt32, false);
299 EXPECT_EQ(roundedUp.drops() - roundedDown.drops(), 1);
300}
301
302TEST_F(XRPAmountMulRatioTest, dividing_by_zero_throws)
303{
304 XRPAmount const one(1);
305
306 EXPECT_ANY_THROW({ mulRatio(one, 1, 0, true); });
307}
308
309TEST_F(XRPAmountMulRatioTest, overflowing_the_maximum_throws)
310{
311 XRPAmount const big(kMaxXrp);
312
313 EXPECT_ANY_THROW({ mulRatio(big, 2, 1, true); });
314}
315
316TEST_F(XRPAmountMulRatioTest, underflow_saturates_at_the_minimum)
317{
318 // Unlike overflow, underflow clamps rather than throwing.
319 XRPAmount const bigNegative(kMinXrp + 10);
320
321 EXPECT_EQ(mulRatio(bigNegative, 2, 1, true), kMinXrp);
322}
323
324namespace {
325
326constexpr auto kMaxDrops = std::numeric_limits<XRPAmount::value_type>::max();
327constexpr auto kMinDrops = std::numeric_limits<XRPAmount::value_type>::min();
328
329Rules
330makeRules(bool withFix)
331{
332 // Rules keeps a reference to its presets, so they must outlive it.
333 static std::unordered_set<uint256, beast::Uhash<>> const kWithFix{fixCleanup3_5_0};
334 static std::unordered_set<uint256, beast::Uhash<>> const kWithoutFix;
335 return Rules{withFix ? kWithFix : kWithoutFix};
336}
337
338void
339expectBoundariesDoNotThrow()
340{
341 XRPAmount const max(kMaxDrops);
342 XRPAmount const min(kMinDrops);
343
344 EXPECT_EQ(XRPAmount(kMaxDrops - 1) + XRPAmount(1), max);
345 EXPECT_EQ(XRPAmount(kMinDrops + 1) - XRPAmount(1), min);
346 EXPECT_EQ(max + min, XRPAmount(-1));
347 EXPECT_EQ(min - min, XRPAmount(0));
348
349 XRPAmount a(kMaxDrops - 1);
350 a += XRPAmount::value_type{1};
351 EXPECT_EQ(a, max);
352 a -= kMaxDrops;
353 EXPECT_EQ(a, XRPAmount(0));
354
355 EXPECT_EQ(XRPAmount(kMaxDrops / 2) * 2, XRPAmount(kMaxDrops - 1));
356 EXPECT_EQ(2 * XRPAmount(kMaxDrops / 2), XRPAmount(kMaxDrops - 1));
357 EXPECT_EQ(min * 1, min);
358 EXPECT_EQ(min * 0, XRPAmount(0));
359 EXPECT_EQ(0 * min, XRPAmount(0));
360 EXPECT_EQ(max * -1, XRPAmount(-kMaxDrops));
361 EXPECT_EQ(XRPAmount(-2) * -3, XRPAmount(6));
362 EXPECT_EQ(XRPAmount(3) * -2, XRPAmount(-6));
363 EXPECT_EQ(XRPAmount(-3) * 2, XRPAmount(-6));
364
365 XRPAmount b(kMinDrops / 2);
366 b *= 2;
367 EXPECT_EQ(b, min);
368
369 // The largest legal XRP amounts cannot overflow a single operation
370 XRPAmount const maxLegal(kInitialXrp);
371 EXPECT_EQ(maxLegal + maxLegal, XRPAmount(2 * kInitialXrp.drops()));
372 EXPECT_EQ(XRPAmount(0) - maxLegal - maxLegal, XRPAmount(-2 * kInitialXrp.drops()));
373}
374
375void
376expectOverflowsThrow()
377{
378 // += and -= with an XRPAmount leave the lhs unchanged after the throw
379 {
380 XRPAmount a(kMaxDrops);
381 EXPECT_THROW(a += XRPAmount(1), std::overflow_error);
382 EXPECT_EQ(a, XRPAmount(kMaxDrops));
383
384 XRPAmount b(kMinDrops);
385 EXPECT_THROW(b += XRPAmount(-1), std::overflow_error);
386 EXPECT_EQ(b, XRPAmount(kMinDrops));
387
388 XRPAmount c(kMinDrops);
389 EXPECT_THROW(c -= XRPAmount(1), std::overflow_error);
390 EXPECT_EQ(c, XRPAmount(kMinDrops));
391
392 XRPAmount d(kMaxDrops);
393 EXPECT_THROW(d -= XRPAmount(-1), std::overflow_error);
394 EXPECT_EQ(d, XRPAmount(kMaxDrops));
395 }
396
397 // += and -= with a scalar
398 {
399 XRPAmount a(kMaxDrops);
400 EXPECT_THROW(a += XRPAmount::value_type{1}, std::overflow_error);
401 EXPECT_EQ(a, XRPAmount(kMaxDrops));
402
403 XRPAmount b(kMinDrops);
404 EXPECT_THROW(b -= XRPAmount::value_type{1}, std::overflow_error);
405 EXPECT_EQ(b, XRPAmount(kMinDrops));
406
407 XRPAmount c(kMinDrops);
408 EXPECT_THROW(c += XRPAmount::value_type{-1}, std::overflow_error);
409 EXPECT_EQ(c, XRPAmount(kMinDrops));
410
411 XRPAmount d(kMaxDrops);
412 EXPECT_THROW(d -= XRPAmount::value_type{-1}, std::overflow_error);
413 EXPECT_EQ(d, XRPAmount(kMaxDrops));
414 }
415
416 // Binary + and - are built from the compound operators
417 {
418 XRPAmount const max(kMaxDrops);
419 EXPECT_THROW((void)(max + XRPAmount(1)), std::overflow_error);
420 EXPECT_THROW((void)((XRPAmount(0) - max) - max), std::overflow_error);
421 }
422
423 // *= and both orders of binary *
424 {
425 XRPAmount a(kMaxDrops);
426 EXPECT_THROW(a *= 2, std::overflow_error);
427 EXPECT_EQ(a, XRPAmount(kMaxDrops));
428
429 XRPAmount b(kMinDrops);
430 EXPECT_THROW(b *= -1, std::overflow_error);
431 EXPECT_EQ(b, XRPAmount(kMinDrops));
432
433 XRPAmount const half((kMaxDrops / 2) + 1);
434 EXPECT_THROW((void)(half * 2), std::overflow_error);
435 EXPECT_THROW((void)(2 * half), std::overflow_error);
436 EXPECT_THROW((void)(XRPAmount(kMinDrops) * -1), std::overflow_error);
437 EXPECT_THROW((void)(-1 * XRPAmount(kMinDrops)), std::overflow_error);
438 // Mixed signs
439 EXPECT_THROW((void)(XRPAmount(kMaxDrops) * -2), std::overflow_error);
440 EXPECT_THROW((void)(XRPAmount(kMinDrops) * 2), std::overflow_error);
441 }
442}
443
444} // namespace
445
446TEST(XRPAmountTest, overflow_throws_with_fix)
447{
448 CurrentTransactionRulesGuard const rg(makeRules(true));
449 expectOverflowsThrow();
450 expectBoundariesDoNotThrow();
451}
452
453// With no rules set (RPC, pathfinding) the operators throw as if the
454// amendment were enabled.
455TEST(XRPAmountTest, overflow_throws_without_rules)
456{
457 ASSERT_FALSE(getCurrentTransactionRules());
458 expectOverflowsThrow();
459 expectBoundariesDoNotThrow();
460}
461
462// Without the amendment an overflow keeps the legacy signed arithmetic, which
463// is undefined behavior, so only results that do not overflow are checked.
464TEST(XRPAmountTest, boundaries_without_fix)
465{
466 CurrentTransactionRulesGuard const rg(makeRules(false));
467 expectBoundariesDoNotThrow();
468}
469
470} // namespace xrpl
RAII class to set and restore the current transaction rules.
Definition Rules.h:113
Rules controlling protocol behavior.
Definition Rules.h:40
constexpr value_type value() const
Returns the underlying value.
Definition XRPAmount.h:225
std::int64_t value_type
Definition XRPAmount.h:32
constexpr int signum() const noexcept
Return the sign of the amount.
Definition XRPAmount.h:152
T max(T... args)
T min(T... args)
constexpr Zero kZero
Definition Zero.h:30
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TEST(FileUtilitiesTest, get_file_contents)
constexpr XRPAmount kDropsPerXrp
Number of drops per 1 XRP.
Definition XRPAmount.h:247
std::optional< Rules > const & getCurrentTransactionRules()
Definition Rules.cpp:30
TEST_F(HardenedHashTest, user_types)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
IOUAmount mulRatio(IOUAmount const &amt, std::uint32_t num, std::uint32_t den, bool roundUp)
constexpr XRPAmount kInitialXrp
Configure the native currency.