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/contract.h>
22 #include <ripple/basics/FeeUnits.h>
23 #include <ripple/basics/Log.h>
24 #include <ripple/basics/safe_cast.h>
25 #include <ripple/core/Config.h>
26 #include <ripple/ledger/ReadView.h>
27 #include <ripple/protocol/jss.h>
28 #include <ripple/protocol/STAmount.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 
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 " <<
60  origFee << " to " << localTxnLoadFee_;
61  return true;
62 }
63 
64 bool
66 {
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 " <<
81  origFee << " to " << localTxnLoadFee_;
82  return true;
83 }
84 
85 //------------------------------------------------------------------------------
86 
87 // Scale using load as well as base rate
89 scaleFeeLoad(FeeUnit64 fee, LoadFeeTrack const& feeTrack,
90  Fees const& fees, bool bUnlimited)
91 {
92  if (fee == 0)
93  return XRPAmount{0};
94 
95  // Normally, types with different units wouldn't be mathematically
96  // compatible. This function is an exception.
97  auto lowestTerms = [](auto& a, auto& b)
98  {
99  auto value = [](auto val)
100  {
101  if constexpr(std::is_arithmetic_v<decltype(val)>)
102  return val;
103  else
104  return val.value();
105  };
106 
107  if (auto const g = std::gcd(value(a), value(b)))
108  {
109  a = value(a) / g;
110  b = value(b) / g;
111  }
112  };
113 
114  // Collect the fee rates
115  auto [feeFactor, uRemFee] = feeTrack.getScalingFactors();
116 
117  // Let privileged users pay the normal fee until
118  // the local load exceeds four times the remote.
119  if (bUnlimited && (feeFactor > uRemFee) && (feeFactor < (4 * uRemFee)))
120  feeFactor = uRemFee;
121 
122  XRPAmount baseFee{fees.base};
123  // Compute:
124  // fee = fee * baseFee * feeFactor / (fees.units * lftNormalFee);
125  // without overflow, and as accurately as possible
126 
127  // The denominator of the fraction we're trying to compute.
128  // fees.units and lftNormalFee are both 32 bit,
129  // so the multiplication can't overflow.
130  auto den = FeeUnit64{ fees.units }
131  * safe_cast<std::uint64_t>(feeTrack.getLoadBase());
132  // Reduce fee * baseFee * feeFactor / (fees.units * lftNormalFee)
133  // to lowest terms.
134  lowestTerms(fee, den);
135  lowestTerms(baseFee, den);
136  lowestTerms(feeFactor, den);
137 
138  // fee and baseFee are 64 bit, feeFactor is 32 bit
139  // Order fee and baseFee largest first
140  // Normally, these types wouldn't be comparable or swappable.
141  // This function is an exception.
142  if (fee.value() < baseFee.value())
143  {
144  auto tmp = fee.value();
145  fee = baseFee.value();
146  baseFee = tmp;
147  }
148  // double check
149  assert(fee.value() >= baseFee.value());
150 
151  // If baseFee * feeFactor overflows, the final result will overflow
152  XRPAmount const baseFeeOverflow{
154  if (baseFee > baseFeeOverflow)
155  {
156  Throw<std::overflow_error>("scaleFeeLoad");
157  }
158  baseFee *= feeFactor;
159 
160  auto const result = mulDiv(fee, baseFee, den);
161  if (!result.first)
162  Throw<std::overflow_error> ("scaleFeeLoad");
163  return result.second;
164 }
165 
166 } // ripple
ripple::LoadFeeTrack::lftFeeMax
static constexpr std::uint32_t lftFeeMax
Definition: LoadFeeTrack.h:130
ripple::XRPAmount::value
constexpr value_type value() const
Returns the underlying value.
Definition: XRPAmount.h:245
ripple::LoadFeeTrack::lftFeeDecFraction
static constexpr std::uint32_t lftFeeDecFraction
Definition: LoadFeeTrack.h:129
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:82
std::is_arithmetic_v
T is_arithmetic_v
ripple::Fees
Reflects the fee settings for a particular ledger.
Definition: ReadView.h:47
ripple::LoadFeeTrack::lock_
std::mutex lock_
Definition: LoadFeeTrack.h:133
ripple::LoadFeeTrack::getScalingFactors
std::pair< std::uint32_t, std::uint32_t > getScalingFactors() const
Definition: LoadFeeTrack.h:94
ripple::LoadFeeTrack::lftFeeIncFraction
static constexpr std::uint32_t lftFeeIncFraction
Definition: LoadFeeTrack.h:128
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:72
ripple::LoadFeeTrack::j_
const beast::Journal j_
Definition: LoadFeeTrack.h:132
ripple::LoadFeeTrack::remoteTxnLoadFee_
std::uint32_t remoteTxnLoadFee_
Definition: LoadFeeTrack.h:136
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:333
ripple::Fees::units
FeeUnit32 units
Definition: ReadView.h:50
beast::Journal::debug
Stream debug() const
Definition: Journal.h:292
ripple::mulDiv
std::pair< bool, Dest > mulDiv(Source1 value, Dest mul, Source2 div)
Definition: FeeUnits.h:487
ripple::LoadFeeTrack::raiseCount_
std::uint32_t raiseCount_
Definition: LoadFeeTrack.h:138
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:127
type_traits
ripple::LoadFeeTrack::localTxnLoadFee_
std::uint32_t localTxnLoadFee_
Definition: LoadFeeTrack.h:135
ripple::Fees::base
XRPAmount base
Definition: ReadView.h:49
ripple::XRPAmount
Definition: XRPAmount.h:46