xrpld
Loading...
Searching...
No Matches
QualityFunction.cpp
1#include <xrpl/protocol/QualityFunction.h>
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/protocol/Quality.h>
7
8#include <optional>
9#include <stdexcept>
10
11namespace xrpl {
12
14 : m_(0), b_(0), quality_(quality)
15{
16 if (quality.rate() <= beast::kZero)
17 Throw<std::runtime_error>("QualityFunction quality rate is 0.");
18 b_ = 1 / quality.rate();
19}
20
21void
23{
24 m_ += b_ * qf.m_;
25 b_ *= qf.b_;
26 if (m_ != 0)
27 quality_ = std::nullopt;
28}
29
32{
33 if (m_ != 0 && quality.rate() != beast::kZero)
34 {
36 auto const out = (1 / quality.rate() - b_) / m_;
37 if (out <= 0)
38 return std::nullopt;
39 return out;
40 }
41 // The sole caller (StrandFlow::limitOut) only invokes this on a non-const
42 // quality function, so m_ != 0 here, and a real payment/offer never yields
43 // a zero-rate limit quality (it would divide by zero above). This fallback
44 // is therefore unreachable in practice.
45 return std::nullopt; // LCOV_EXCL_LINE
46}
47
48bool
50{
51 // satisfiesAvgQ is only reached from StrandFlow::limitOut *after*
52 // outFromAvgQ returned a value, which requires a non-zero rate. So a
53 // zero-rate quality never reaches here; this guard is defensive.
54 if (quality.rate() == beast::kZero)
55 return false; // LCOV_EXCL_LINE
56 return m_ * out + b_ >= 1 / quality.rate();
57}
58
59} // namespace xrpl
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
static RoundingMode setround(RoundingMode inMode)
std::optional< Quality > const & quality() const
QualityFunction(Quality const &quality, CLOBLikeTag)
void combine(QualityFunction const &qf)
Combines QF with the next step QF.
std::optional< Quality > quality_
std::optional< Number > outFromAvgQ(Quality const &quality)
Find output to produce the requested average quality.
bool satisfiesAvgQ(Quality const &quality, Number const &out) const
Return whether out produces at least the requested average quality.
Represents the logical ratio of output currency to input currency.
Definition Quality.h:90
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
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52