xrpld
Loading...
Searching...
No Matches
Quality_test.cpp
1#include <xrpl/basics/base_uint.h>
2#include <xrpl/beast/hash/uhash.h>
3#include <xrpl/beast/unit_test/suite.h>
4#include <xrpl/beast/utility/Zero.h>
5#include <xrpl/protocol/AccountID.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/Issue.h>
8#include <xrpl/protocol/Quality.h>
9#include <xrpl/protocol/Rules.h>
10#include <xrpl/protocol/STAmount.h>
11#include <xrpl/protocol/UintTypes.h>
12#include <xrpl/protocol/XRPAmount.h>
13
14#include <array>
15#include <cstdint>
16#include <functional>
17#include <stdexcept>
18#include <type_traits>
19#include <unordered_set>
20
21namespace xrpl {
22
24{
25public:
26 // Create a raw, non-integral amount from mantissa and exponent
27 STAmount static raw(std::uint64_t mantissa, int exponent)
28 {
29 return STAmount(Issue{Currency(3), AccountID(3)}, mantissa, exponent);
30 }
31
32 template <class Integer>
33 static STAmount
34 amount(Integer integer)
36 {
37 static_assert(std::is_integral_v<Integer>);
38 return STAmount(integer, false);
39 }
40
41 template <class Integer>
42 static STAmount
43 amount(Integer integer)
45 {
46 static_assert(std::is_integral_v<Integer>);
47 if (integer < 0)
48 return STAmount(-integer, true);
49 return STAmount(integer, false);
50 }
51
52 template <class In, class Out>
53 static Amounts
54 amounts(In in, Out out)
55 {
56 return Amounts(amount(in), amount(out));
57 }
58
59 template <class In1, class Out1, class Int, class In2, class Out2>
60 void
61 ceilIn(Quality const& q, In1 in, Out1 out, Int limit, In2 inExpected, Out2 outExpected)
62 {
63 auto const expectResult(amounts(inExpected, outExpected));
64 auto const actualResult(q.ceilIn(amounts(in, out), amount(limit)));
65
66 BEAST_EXPECT(actualResult == expectResult);
67 }
68
69 template <class In1, class Out1, class Int, class In2, class Out2>
70 void
71 ceilOut(Quality const& q, In1 in, Out1 out, Int limit, In2 inExpected, Out2 outExpected)
72 {
73 auto const expectResult(amounts(inExpected, outExpected));
74 auto const actualResult(q.ceilOut(amounts(in, out), amount(limit)));
75
76 BEAST_EXPECT(actualResult == expectResult);
77 }
78
79 void
81 {
82 testcase("ceil_in");
83
84 {
85 // 1 in, 1 out:
86 Quality const q(Amounts(amount(1), amount(1)));
87
88 ceilIn(
89 q,
90 1,
91 1, // 1 in, 1 out
92 1, // limit: 1
93 1,
94 1); // 1 in, 1 out
95
96 ceilIn(
97 q,
98 10,
99 10, // 10 in, 10 out
100 5, // limit: 5
101 5,
102 5); // 5 in, 5 out
103
104 ceilIn(
105 q,
106 5,
107 5, // 5 in, 5 out
108 10, // limit: 10
109 5,
110 5); // 5 in, 5 out
111 }
112
113 {
114 // 1 in, 2 out:
115 Quality const q(Amounts(amount(1), amount(2)));
116
117 ceilIn(
118 q,
119 40,
120 80, // 40 in, 80 out
121 40, // limit: 40
122 40,
123 80); // 40 in, 20 out
124
125 ceilIn(
126 q,
127 40,
128 80, // 40 in, 80 out
129 20, // limit: 20
130 20,
131 40); // 20 in, 40 out
132
133 ceilIn(
134 q,
135 40,
136 80, // 40 in, 80 out
137 60, // limit: 60
138 40,
139 80); // 40 in, 80 out
140 }
141
142 {
143 // 2 in, 1 out:
144 Quality const q(Amounts(amount(2), amount(1)));
145
146 ceilIn(
147 q,
148 40,
149 20, // 40 in, 20 out
150 20, // limit: 20
151 20,
152 10); // 20 in, 10 out
153
154 ceilIn(
155 q,
156 40,
157 20, // 40 in, 20 out
158 40, // limit: 40
159 40,
160 20); // 40 in, 20 out
161
162 ceilIn(
163 q,
164 40,
165 20, // 40 in, 20 out
166 50, // limit: 40
167 40,
168 20); // 40 in, 20 out
169 }
170 }
171
172 void
174 {
175 testcase("ceil_out");
176
177 {
178 // 1 in, 1 out:
179 Quality const q(Amounts(amount(1), amount(1)));
180
181 ceilOut(
182 q,
183 1,
184 1, // 1 in, 1 out
185 1, // limit 1
186 1,
187 1); // 1 in, 1 out
188
189 ceilOut(
190 q,
191 10,
192 10, // 10 in, 10 out
193 5, // limit 5
194 5,
195 5); // 5 in, 5 out
196
197 ceilOut(
198 q,
199 10,
200 10, // 10 in, 10 out
201 20, // limit 20
202 10,
203 10); // 10 in, 10 out
204 }
205
206 {
207 // 1 in, 2 out:
208 Quality const q(Amounts(amount(1), amount(2)));
209
210 ceilOut(
211 q,
212 40,
213 80, // 40 in, 80 out
214 40, // limit 40
215 20,
216 40); // 20 in, 40 out
217
218 ceilOut(
219 q,
220 40,
221 80, // 40 in, 80 out
222 80, // limit 80
223 40,
224 80); // 40 in, 80 out
225
226 ceilOut(
227 q,
228 40,
229 80, // 40 in, 80 out
230 100, // limit 100
231 40,
232 80); // 40 in, 80 out
233 }
234
235 {
236 // 2 in, 1 out:
237 Quality const q(Amounts(amount(2), amount(1)));
238
239 ceilOut(
240 q,
241 40,
242 20, // 40 in, 20 out
243 20, // limit 20
244 40,
245 20); // 40 in, 20 out
246
247 ceilOut(
248 q,
249 40,
250 20, // 40 in, 20 out
251 40, // limit 40
252 40,
253 20); // 40 in, 20 out
254
255 ceilOut(
256 q,
257 40,
258 20, // 40 in, 20 out
259 10, // limit 10
260 20,
261 10); // 20 in, 10 out
262 }
263 }
264
265 void
267 {
268 testcase("raw");
269
270 {
271 Quality const q(0x5d048191fb9130daull); // 126836389.7680090
272 Amounts const value(
273 amount(349469768), // 349.469768 XRP
274 raw(2755280000000000ull, -15)); // 2.75528
275 STAmount const limit(raw(4131113916555555, -16)); // .4131113916555555
276 Amounts const result(q.ceilOut(value, limit));
277 BEAST_EXPECT(result.in != beast::kZero);
278 }
279 }
280
281 void
283 {
284 testcase("ceil out clamp native");
285
286 STAmount const in{XRPAmount{static_cast<std::int64_t>(STAmount::kMaxNativeN)}};
287 Amounts const value(in, raw(9'999'999'999'999'995ull, -15));
288 STAmount const limit(raw(9'999'999'999'999'994ull, -15));
289 Quality const q(value);
290
291 auto const rules = [](bool const fix) {
292 // Rules keeps a reference to the presets set, so use static
293 // storage here rather than a local temporary.
294 static std::unordered_set<UInt256, beast::Uhash<>> const kNoFeatures;
295 static std::unordered_set<UInt256, beast::Uhash<>> const kFixFeatures{fixCleanup3_5_0};
296 return Rules{fix ? kFixFeatures : kNoFeatures};
297 };
298
299 auto const ceilOuts = std::array{
300 std::function<Amounts()>{[&] { return q.ceilOut(value, limit); }},
301 std::function<Amounts()>{[&] { return q.ceilOutStrict(value, limit, false); }},
302 std::function<Amounts()>{[&] { return q.ceilOutStrict(value, limit, true); }},
303 };
304
305 for (auto const& ceilOut : ceilOuts)
306 {
307 {
308 CurrentTransactionRulesGuard const rg(rules(false));
309 bool threw = false;
310 try
311 {
312 (void)ceilOut();
313 }
314 catch (std::runtime_error const&)
315 {
316 threw = true;
317 }
318 BEAST_EXPECT(threw);
319 }
320
321 {
322 CurrentTransactionRulesGuard const rg(rules(true));
323 Amounts const result = ceilOut();
324 BEAST_EXPECT(result.in == in);
325 BEAST_EXPECT(result.out == limit);
326 }
327
328 // Outside a transaction (e.g. pathfinding) the fix applies.
329 {
330 Amounts const result = ceilOut();
331 BEAST_EXPECT(result.in == in);
332 BEAST_EXPECT(result.out == limit);
333 }
334 }
335 }
336
337 void
339 {
340 testcase("round");
341
342 Quality const q(0x59148191fb913522ull); // 57719.63525051682
343 BEAST_EXPECT(q.round(3).rate().getText() == "57800");
344 BEAST_EXPECT(q.round(4).rate().getText() == "57720");
345 BEAST_EXPECT(q.round(5).rate().getText() == "57720");
346 BEAST_EXPECT(q.round(6).rate().getText() == "57719.7");
347 BEAST_EXPECT(q.round(7).rate().getText() == "57719.64");
348 BEAST_EXPECT(q.round(8).rate().getText() == "57719.636");
349 BEAST_EXPECT(q.round(9).rate().getText() == "57719.6353");
350 BEAST_EXPECT(q.round(10).rate().getText() == "57719.63526");
351 BEAST_EXPECT(q.round(11).rate().getText() == "57719.635251");
352 BEAST_EXPECT(q.round(12).rate().getText() == "57719.6352506");
353 BEAST_EXPECT(q.round(13).rate().getText() == "57719.63525052");
354 BEAST_EXPECT(q.round(14).rate().getText() == "57719.635250517");
355 BEAST_EXPECT(q.round(15).rate().getText() == "57719.6352505169");
356 BEAST_EXPECT(q.round(16).rate().getText() == "57719.63525051682");
357 }
358
359 void
361 {
362 testcase("comparisons");
363
364 STAmount const amount1(noIssue(), 231);
365 STAmount const amount2(noIssue(), 462);
366 STAmount const amount3(noIssue(), 924);
367
368 Quality const q11(Amounts(amount1, amount1));
369 Quality const q12(Amounts(amount1, amount2));
370 Quality const q13(Amounts(amount1, amount3));
371 Quality const q21(Amounts(amount2, amount1));
372 Quality const q31(Amounts(amount3, amount1));
373
374 BEAST_EXPECT(q11 == q11);
375 BEAST_EXPECT(q11 < q12);
376 BEAST_EXPECT(q12 < q13);
377 BEAST_EXPECT(q31 < q21);
378 BEAST_EXPECT(q21 < q11);
379 BEAST_EXPECT(q11 >= q11);
380 BEAST_EXPECT(q12 >= q11);
381 BEAST_EXPECT(q13 >= q12);
382 BEAST_EXPECT(q21 >= q31);
383 BEAST_EXPECT(q11 >= q21);
384 BEAST_EXPECT(q12 > q11);
385 BEAST_EXPECT(q13 > q12);
386 BEAST_EXPECT(q21 > q31);
387 BEAST_EXPECT(q11 > q21);
388 BEAST_EXPECT(q11 <= q11);
389 BEAST_EXPECT(q11 <= q12);
390 BEAST_EXPECT(q12 <= q13);
391 BEAST_EXPECT(q31 <= q21);
392 BEAST_EXPECT(q21 <= q11);
393 BEAST_EXPECT(q31 != q21);
394 }
395
396 void
398 {
399 testcase("composition");
400
401 STAmount const amount1(noIssue(), 231);
402 STAmount const amount2(noIssue(), 462);
403 STAmount const amount3(noIssue(), 924);
404
405 Quality const q11(Amounts(amount1, amount1));
406 Quality const q12(Amounts(amount1, amount2));
407 Quality const q13(Amounts(amount1, amount3));
408 Quality const q21(Amounts(amount2, amount1));
409 Quality const q31(Amounts(amount3, amount1));
410
411 BEAST_EXPECT(composedQuality(q12, q21) == q11);
412
413 Quality const q1331(composedQuality(q13, q31));
414 Quality const q3113(composedQuality(q31, q13));
415
416 BEAST_EXPECT(q1331 == q3113);
417 BEAST_EXPECT(q1331 == q11);
418 }
419
420 void
422 {
423 testcase("operations");
424
425 Quality const q11(Amounts(STAmount(noIssue(), 731), STAmount(noIssue(), 731)));
426
427 Quality qa(q11);
428 Quality qb(q11);
429
430 BEAST_EXPECT(qa == qb);
431 BEAST_EXPECT(++qa != q11);
432 BEAST_EXPECT(qa != qb);
433 BEAST_EXPECT(--qb != q11);
434 BEAST_EXPECT(qa != qb);
435 BEAST_EXPECT(qb < qa);
436 BEAST_EXPECT(qb++ < qa);
437 BEAST_EXPECT(qb++ < qa);
438 BEAST_EXPECT(qb++ == qa);
439 BEAST_EXPECT(qa < qb);
440 }
441 void
442 run() override
443 {
447 testCeilIn();
448 testCeilOut();
449 testRaw();
451 testRound();
452 }
453};
454
456
457} // namespace xrpl
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
RAII class to set and restore the current transaction rules.
Definition Rules.h:113
A currency issued by an account.
Definition Issue.h:18
static STAmount amount(Integer integer)
static STAmount raw(std::uint64_t mantissa, int exponent)
static STAmount amount(Integer integer)
void ceilOut(Quality const &q, In1 in, Out1 out, Int limit, In2 inExpected, Out2 outExpected)
void run() override
Runs the suite.
void ceilIn(Quality const &q, In1 in, Out1 out, Int limit, In2 inExpected, Out2 outExpected)
static Amounts amounts(In in, Out out)
Represents the logical ratio of output currency to input currency.
Definition Quality.h:90
STAmount rate() const
Returns the quality as STAmount.
Definition Quality.h:162
Quality round(int tickSize) const
Returns the quality rounded up to the specified number of decimal digits.
Definition Quality.cpp:171
Amounts ceilOut(Amounts const &amount, STAmount const &limit) const
Returns the scaled amount with out capped.
Definition Quality.cpp:139
Amounts ceilIn(Amounts const &amount, STAmount const &limit) const
Returns the scaled amount with in capped.
Definition Quality.cpp:77
Amounts ceilOutStrict(Amounts const &amount, STAmount const &limit, bool roundUp) const
Definition Quality.cpp:145
Rules controlling protocol behavior.
Definition Rules.h:40
std::string getText() const override
static constexpr std::uint64_t kMaxNativeN
Definition STAmount.h:72
T is_integral_v
T is_signed_v
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
TAmounts< STAmount, STAmount > Amounts
Definition Quality.h:69
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
Definition Issue.h:118
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)
Quality composedQuality(Quality const &lhs, Quality const &rhs)
Calculate the quality of a two-hop path given the two hops.
Definition Quality.cpp:151