rippled
Loading...
Searching...
No Matches
QualityFunction.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/Number.h>
21#include <xrpl/basics/contract.h>
22#include <xrpl/beast/utility/Zero.h>
23#include <xrpl/protocol/Quality.h>
24#include <xrpl/protocol/QualityFunction.h>
25
26#include <optional>
27#include <stdexcept>
28
29namespace ripple {
30
32 Quality const& quality,
34 : m_(0), b_(0), quality_(quality)
35{
36 if (quality.rate() <= beast::zero)
37 Throw<std::runtime_error>("QualityFunction quality rate is 0.");
38 b_ = 1 / quality.rate();
39}
40
41void
43{
44 m_ += b_ * qf.m_;
45 b_ *= qf.b_;
46 if (m_ != 0)
48}
49
51QualityFunction::outFromAvgQ(Quality const& quality)
52{
53 if (m_ != 0 && quality.rate() != beast::zero)
54 {
56 auto const out = (1 / quality.rate() - b_) / m_;
57 if (out <= 0)
58 return std::nullopt;
59 return out;
60 }
61 return std::nullopt;
62}
63
64} // namespace ripple
static rounding_mode setround(rounding_mode mode)
Definition Number.cpp:53
Average quality of a path as a function of out: q(out) = m * out + b, where m = -1 / poolGets,...
QualityFunction(Quality const &quality, CLOBLikeTag)
void combine(QualityFunction const &qf)
Combines QF with the next step QF.
std::optional< Number > outFromAvgQ(Quality const &quality)
Find output to produce the requested average quality.
std::optional< Quality > const & quality() const
std::optional< Quality > quality_
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25