rippled
LoadFeeTrack.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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 <ripple/app/misc/LoadFeeTrack.h>
21 #include <ripple/basics/FeeUnits.h>
22 #include <ripple/basics/Log.h>
23 #include <ripple/basics/contract.h>
24 #include <ripple/basics/safe_cast.h>
25 #include <ripple/core/Config.h>
26 #include <ripple/ledger/ReadView.h>
27 #include <ripple/protocol/STAmount.h>
28 #include <ripple/protocol/jss.h>
29 
30 #include <cstdint>
31 #include <numeric>
32 #include <type_traits>
33 
34 namespace ripple {
35 
36 bool
38 {
40 
41  if (++raiseCount_ < 2)
42  return false;
43 
44  std::uint32_t const origFee = localTxnLoadFee_;
45 
46  // make sure this fee takes effect
49 
50  // Increase slowly
52 
55 
56  if (origFee == localTxnLoadFee_)
57  return false;
58 
59  JLOG(j_.debug()) << "Local load fee raised from " << origFee << " to "
61  return true;
62 }
63 
64 bool
66 {
68  std::uint32_t const origFee = localTxnLoadFee_;
69  raiseCount_ = 0;
70 
71  // Reduce slowly
73 
76 
77  if (origFee == localTxnLoadFee_)
78  return false;
79 
80  JLOG(j_.debug()) << "Local load fee lowered from " << origFee << " to "
82  return true;
83 }
84 
85 //------------------------------------------------------------------------------
86 
87 // Scale using load as well as base rate
90  FeeUnit64 fee,
91  LoadFeeTrack const& feeTrack,
92  Fees const& fees,
93  bool bUnlimited)
94 {
95  if (fee == 0)
96  return XRPAmount{0};
97 
98  // Normally, types with different units wouldn't be mathematically
99  // compatible. This function is an exception.
100  auto lowestTerms = [](auto& a, auto& b) {
101  auto value = [](auto val) {
102  if constexpr (std::is_arithmetic_v<decltype(val)>)
103  return val;
104  else
105  return val.value();
106  };
107 
108  if (auto const g = std::gcd(value(a), value(b)))
109  {
110  a = value(a) / g;
111  b = value(b) / g;
112  }
113  };
114 
115  // Collect the fee rates
116  auto [feeFactor, uRemFee] = feeTrack.getScalingFactors();
117 
118  // Let privileged users pay the normal fee until
119  // the local load exceeds four times the remote.
120  if (bUnlimited && (feeFactor > uRemFee) && (feeFactor < (4 * uRemFee)))
121  feeFactor = uRemFee;
122 
123  XRPAmount baseFee{fees.base};
124  // Compute:
125  // fee = fee * baseFee * feeFactor / (fees.units * lftNormalFee);
126  // without overflow, and as accurately as possible
127 
128  // The denominator of the fraction we're trying to compute.
129  // fees.units and lftNormalFee are both 32 bit,
130  // so the multiplication can't overflow.
131  auto den = FeeUnit64{fees.units} *
132  safe_cast<std::uint64_t>(feeTrack.getLoadBase());
133  // Reduce fee * baseFee * feeFactor / (fees.units * lftNormalFee)
134  // to lowest terms.
135  lowestTerms(fee, den);
136  lowestTerms(baseFee, den);
137  lowestTerms(feeFactor, den);
138 
139  // fee and baseFee are 64 bit, feeFactor is 32 bit
140  // Order fee and baseFee largest first
141  // Normally, these types wouldn't be comparable or swappable.
142  // This function is an exception.
143  if (fee.value() < baseFee.value())
144  {
145  auto tmp = fee.value();
146  fee = baseFee.value();
147  baseFee = tmp;
148  }
149  // double check
150  assert(fee.value() >= baseFee.value());
151 
152  // If baseFee * feeFactor overflows, the final result will overflow
153  XRPAmount const baseFeeOverflow{
155  if (baseFee > baseFeeOverflow)
156  {
157  Throw<std::overflow_error>("scaleFeeLoad");
158  }
159  baseFee *= feeFactor;
160 
161  auto const result = mulDiv(fee, baseFee, den);
162  if (!result.first)
163  Throw<std::overflow_error>("scaleFeeLoad");
164  return result.second;
165 }
166 
167 } // namespace ripple
ripple::LoadFeeTrack::lftFeeMax
static constexpr std::uint32_t lftFeeMax
Definition: LoadFeeTrack.h:144
ripple::XRPAmount::value
constexpr value_type value() const
Returns the underlying value.
Definition: XRPAmount.h:230
ripple::LoadFeeTrack::lftFeeDecFraction
static constexpr std::uint32_t lftFeeDecFraction
Definition: LoadFeeTrack.h:142
ripple::scaleFeeLoad
XRPAmount scaleFeeLoad(FeeUnit64 fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
Definition: LoadFeeTrack.cpp:89
std::lock_guard
STL class.
ripple::LoadFeeTrack::getLoadBase
std::uint32_t getLoadBase() const
Definition: LoadFeeTrack.h:87
std::is_arithmetic_v
T is_arithmetic_v
ripple::Fees
Reflects the fee settings for a particular ledger.
Definition: ReadView.h:48
ripple::LoadFeeTrack::lock_
std::mutex lock_
Definition: LoadFeeTrack.h:147
ripple::LoadFeeTrack::getScalingFactors
std::pair< std::uint32_t, std::uint32_t > getScalingFactors() const
Definition: LoadFeeTrack.h:101
ripple::LoadFeeTrack::lftFeeIncFraction
static constexpr std::uint32_t lftFeeIncFraction
Definition: LoadFeeTrack.h:140
ripple::LoadFeeTrack
Manages the current fee schedule.
Definition: LoadFeeTrack.h:43
cstdint
std::uint32_t
std::gcd
T gcd(T... args)
ripple::feeunit::TaggedFee
Definition: FeeUnits.h:70
ripple::LoadFeeTrack::j_
const beast::Journal j_
Definition: LoadFeeTrack.h:146
ripple::LoadFeeTrack::remoteTxnLoadFee_
std::uint32_t remoteTxnLoadFee_
Definition: LoadFeeTrack.h:150
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::LoadFeeTrack::raiseLocalFee
bool raiseLocalFee()
Definition: LoadFeeTrack.cpp:37
ripple::feeunit::TaggedFee::value
constexpr value_type value() const
Returns the underlying value.
Definition: FeeUnits.h:313
beast::Journal::debug
Stream debug() const
Definition: Journal.h:315
ripple::mulDiv
std::pair< bool, Dest > mulDiv(Source1 value, Dest mul, Source2 div)
Definition: FeeUnits.h:473
ripple::LoadFeeTrack::raiseCount_
std::uint32_t raiseCount_
Definition: LoadFeeTrack.h:153
numeric
std::numeric_limits::max
T max(T... args)
ripple::LoadFeeTrack::lowerLocalFee
bool lowerLocalFee()
Definition: LoadFeeTrack.cpp:65
ripple::LoadFeeTrack::lftNormalFee
static constexpr std::uint32_t lftNormalFee
Definition: LoadFeeTrack.h:138
type_traits
ripple::LoadFeeTrack::localTxnLoadFee_
std::uint32_t localTxnLoadFee_
Definition: LoadFeeTrack.h:149
ripple::XRPAmount
Definition: XRPAmount.h:46