xrpld
Loading...
Searching...
No Matches
Quality.cpp
1#include <xrpl/protocol/Quality.h>
2
3#include <xrpl/beast/utility/Zero.h> // IWYU pragma: keep
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/protocol/Asset.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/Rules.h>
8#include <xrpl/protocol/STAmount.h>
9
10#include <cstdint>
11#include <limits> // IWYU pragma: keep
12#include <optional>
13#include <stdexcept>
14
15namespace xrpl {
16
18{
19}
20
21Quality::Quality(Amounts const& amount) : value_(getRate(amount.out, amount.in))
22{
23}
24
27{
28 XRPL_ASSERT(value_ > 0, "xrpl::Quality::operator++() : minimum value");
29 --value_;
30 return *this;
31}
32
35{
36 Quality prev(*this);
37 ++*this;
38 return prev;
39}
40
43{
44 XRPL_ASSERT(
46 "xrpl::Quality::operator--() : maximum value");
47 ++value_;
48 return *this;
49}
50
53{
54 Quality prev(*this);
55 --*this;
56 return prev;
57}
58
59template <STAmount (*DivRoundFunc)(STAmount const&, STAmount const&, Asset const&, bool)>
60static Amounts
61ceilInImpl(Amounts const& amount, STAmount const& limit, bool roundUp, Quality const& quality)
62{
63 if (amount.in > limit)
64 {
65 Amounts result(limit, DivRoundFunc(limit, quality.rate(), amount.out.asset(), roundUp));
66 // Clamp out
67 if (result.out > amount.out)
68 result.out = amount.out;
69 XRPL_ASSERT(result.in == limit, "xrpl::ceilInImpl : result matches limit");
70 return result;
71 }
72 XRPL_ASSERT(amount.in <= limit, "xrpl::ceilInImpl : result inside limit");
73 return amount;
74}
75
77Quality::ceilIn(Amounts const& amount, STAmount const& limit) const
78{
79 return ceilInImpl<divRound>(amount, limit, /* roundUp */ true, *this);
80}
81
83Quality::ceilInStrict(Amounts const& amount, STAmount const& limit, bool roundUp) const
84{
85 return ceilInImpl<divRoundStrict>(amount, limit, roundUp, *this);
86}
87
88// Returns std::nullopt if a native result can't be represented, instead of
89// throwing. Pathfinding runs without transaction
90// rules, so default to the fixed behavior there to match transactions once the
91// amendment is enabled.
92template <STAmount (*MulRoundFunc)(STAmount const&, STAmount const&, Asset const&, bool)>
94tryMulRound(STAmount const& v1, STAmount const& v2, Asset const& asset, bool roundUp)
95{
96 try
97 {
98 return MulRoundFunc(v1, v2, asset, roundUp);
99 }
100 catch (std::overflow_error const&)
101 {
102 // std::overflow_error is a std::runtime_error, so it must be caught
103 // first to keep the handler below from swallowing it. It means the
104 // computation itself overflowed, not that the result is out of range.
105 // Callers handle it, e.g. BookStep removes the offer.
106 throw;
107 }
108 catch (std::runtime_error const&)
109 {
110 if (!asset.native() || !isFeatureEnabled(fixCleanup3_5_0, /*resultIfNoRules*/ true))
111 throw;
112 return std::nullopt;
113 }
114}
115
116template <STAmount (*MulRoundFunc)(STAmount const&, STAmount const&, Asset const&, bool)>
117static Amounts
118ceilOutImpl(Amounts const& amount, STAmount const& limit, bool roundUp, Quality const& quality)
119{
120 if (amount.out > limit)
121 {
122 // An unrepresentable XRP product is larger than amount.in, so the
123 // clamp below would return amount.in anyway.
124 Amounts result(
125 tryMulRound<MulRoundFunc>(limit, quality.rate(), amount.in.asset(), roundUp)
126 .value_or(amount.in),
127 limit);
128 // Clamp in
129 if (result.in > amount.in)
130 result.in = amount.in;
131 XRPL_ASSERT(result.out == limit, "xrpl::ceilOutImpl : result matches limit");
132 return result;
133 }
134 XRPL_ASSERT(amount.out <= limit, "xrpl::ceilOutImpl : result inside limit");
135 return amount;
136}
137
139Quality::ceilOut(Amounts const& amount, STAmount const& limit) const
140{
141 return ceilOutImpl<mulRound>(amount, limit, /* roundUp */ true, *this);
142}
143
145Quality::ceilOutStrict(Amounts const& amount, STAmount const& limit, bool roundUp) const
146{
147 return ceilOutImpl<mulRoundStrict>(amount, limit, roundUp, *this);
148}
149
151composedQuality(Quality const& lhs, Quality const& rhs)
152{
153 STAmount const lhsRate(lhs.rate());
154 XRPL_ASSERT(lhsRate != beast::kZero, "xrpl::composedQuality : nonzero left input");
155
156 STAmount const rhsRate(rhs.rate());
157 XRPL_ASSERT(rhsRate != beast::kZero, "xrpl::composedQuality : nonzero right input");
158
159 STAmount const rate(mulRound(lhsRate, rhsRate, lhsRate.asset(), true));
160
161 std::uint64_t const storedExponent(rate.exponent() + 100);
162 std::uint64_t const storedMantissa(rate.mantissa());
163
164 XRPL_ASSERT(
165 (storedExponent > 0) && (storedExponent <= 255), "xrpl::composedQuality : valid exponent");
166
167 return Quality((storedExponent << (64 - 8)) | storedMantissa);
168}
169
170Quality
171Quality::round(int digits) const
172{
173 // Modulus for mantissa
174 static std::uint64_t const kMod[17] = {
175 /* 0 */ 10000000000000000,
176 /* 1 */ 1000000000000000,
177 /* 2 */ 100000000000000,
178 /* 3 */ 10000000000000,
179 /* 4 */ 1000000000000,
180 /* 5 */ 100000000000,
181 /* 6 */ 10000000000,
182 /* 7 */ 1000000000,
183 /* 8 */ 100000000,
184 /* 9 */ 10000000,
185 /* 10 */ 1000000,
186 /* 11 */ 100000,
187 /* 12 */ 10000,
188 /* 13 */ 1000,
189 /* 14 */ 100,
190 /* 15 */ 10,
191 /* 16 */ 1,
192 };
193
194 auto const exponent = value_ >> (64 - 8);
195 auto mantissa = value_ & 0x00ffffffffffffffULL;
196 mantissa += kMod[digits] - 1;
197 mantissa -= (mantissa % kMod[digits]);
198
199 return Quality{(exponent << (64 - 8)) | mantissa};
200}
201
202} // namespace xrpl
constexpr bool native() const
Definition Asset.h:125
Represents the logical ratio of output currency to input currency.
Definition Quality.h:90
Quality & operator++()
Advances to the next higher quality level.
Definition Quality.cpp:26
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
Amounts ceilInStrict(Amounts const &amount, STAmount const &limit, bool roundUp) const
Definition Quality.cpp:83
Quality & operator--()
Advances to the next lower quality level.
Definition Quality.cpp:42
Quality()=default
value_type value_
Definition Quality.h:104
Asset const & asset() const
Definition STAmount.h:496
T max(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
TAmounts< STAmount, STAmount > Amounts
Definition Quality.h:69
bool isFeatureEnabled(UInt256 const &feature, bool resultIfNoRules)
Check whether a feature is enabled in the current ledger rules.
Definition Rules.cpp:199
static Amounts ceilOutImpl(Amounts const &amount, STAmount const &limit, bool roundUp, Quality const &quality)
Definition Quality.cpp:118
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
static std::optional< STAmount > tryMulRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
Definition Quality.cpp:94
STAmount mulRound(STAmount const &v1, STAmount const &v2, Asset const &asset, bool roundUp)
static Amounts ceilInImpl(Amounts const &amount, STAmount const &limit, bool roundUp, Quality const &quality)
Definition Quality.cpp:61
Quality composedQuality(Quality const &lhs, Quality const &rhs)
Calculate the quality of a two-hop path given the two hops.
Definition Quality.cpp:151