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  XRPAmount fee,
91  LoadFeeTrack const& feeTrack,
92  Fees const& fees,
93  bool bUnlimited)
94 {
95  if (fee == 0)
96  return fee;
97 
98  // Collect the fee rates
99  auto [feeFactor, uRemFee] = feeTrack.getScalingFactors();
100 
101  // Let privileged users pay the normal fee until
102  // the local load exceeds four times the remote.
103  if (bUnlimited && (feeFactor > uRemFee) && (feeFactor < (4 * uRemFee)))
104  feeFactor = uRemFee;
105 
106  // Compute:
107  // fee = fee * feeFactor / (lftNormalFee);
108  // without overflow, and as accurately as possible
109 
110  auto const result = mulDiv(
111  fee, feeFactor, safe_cast<std::uint64_t>(feeTrack.getLoadBase()));
112  if (!result.first)
113  Throw<std::overflow_error>("scaleFeeLoad");
114  return result.second;
115 }
116 
117 } // namespace ripple
ripple::LoadFeeTrack::lftFeeMax
static constexpr std::uint32_t lftFeeMax
Definition: LoadFeeTrack.h:147
ripple::LoadFeeTrack::lftFeeDecFraction
static constexpr std::uint32_t lftFeeDecFraction
Definition: LoadFeeTrack.h:145
std::lock_guard
STL class.
ripple::LoadFeeTrack::getLoadBase
std::uint32_t getLoadBase() const
Definition: LoadFeeTrack.h:89
ripple::Fees
Reflects the fee settings for a particular ledger.
Definition: ReadView.h:49
ripple::LoadFeeTrack::lock_
std::mutex lock_
Definition: LoadFeeTrack.h:150
ripple::LoadFeeTrack::getScalingFactors
std::pair< std::uint32_t, std::uint32_t > getScalingFactors() const
Definition: LoadFeeTrack.h:103
ripple::LoadFeeTrack::lftFeeIncFraction
static constexpr std::uint32_t lftFeeIncFraction
Definition: LoadFeeTrack.h:143
ripple::LoadFeeTrack
Manages the current fee schedule.
Definition: LoadFeeTrack.h:44
cstdint
std::uint32_t
ripple::LoadFeeTrack::j_
const beast::Journal j_
Definition: LoadFeeTrack.h:149
ripple::LoadFeeTrack::remoteTxnLoadFee_
std::uint32_t remoteTxnLoadFee_
Definition: LoadFeeTrack.h:153
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
beast::Journal::debug
Stream debug() const
Definition: Journal.h:315
ripple::scaleFeeLoad
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
Definition: LoadFeeTrack.cpp:89
ripple::mulDiv
std::pair< bool, Dest > mulDiv(Source1 value, Dest mul, Source2 div)
Definition: FeeUnits.h:468
ripple::LoadFeeTrack::raiseCount_
std::uint32_t raiseCount_
Definition: LoadFeeTrack.h:156
numeric
ripple::LoadFeeTrack::lowerLocalFee
bool lowerLocalFee()
Definition: LoadFeeTrack.cpp:65
ripple::LoadFeeTrack::lftNormalFee
static constexpr std::uint32_t lftNormalFee
Definition: LoadFeeTrack.h:141
type_traits
ripple::LoadFeeTrack::localTxnLoadFee_
std::uint32_t localTxnLoadFee_
Definition: LoadFeeTrack.h:152
ripple::XRPAmount
Definition: XRPAmount.h:46