rippled
Loading...
Searching...
No Matches
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 <xrpld/app/misc/LoadFeeTrack.h>
21#include <xrpld/core/Config.h>
22#include <xrpld/ledger/ReadView.h>
23#include <xrpl/basics/Log.h>
24#include <xrpl/basics/contract.h>
25#include <xrpl/basics/safe_cast.h>
26#include <xrpl/protocol/FeeUnits.h>
27#include <xrpl/protocol/STAmount.h>
28#include <xrpl/protocol/jss.h>
29
30#include <cstdint>
31#include <numeric>
32#include <type_traits>
33
34namespace ripple {
35
36bool
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
64bool
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)
113 Throw<std::overflow_error>("scaleFeeLoad");
114 return *result;
115}
116
117} // namespace ripple
Stream debug() const
Definition: Journal.h:317
Manages the current fee schedule.
Definition: LoadFeeTrack.h:46
static std::uint32_t constexpr lftFeeIncFraction
Definition: LoadFeeTrack.h:144
beast::Journal const j_
Definition: LoadFeeTrack.h:150
static std::uint32_t constexpr lftNormalFee
Definition: LoadFeeTrack.h:142
std::uint32_t raiseCount_
Definition: LoadFeeTrack.h:157
std::uint32_t localTxnLoadFee_
Definition: LoadFeeTrack.h:153
std::uint32_t remoteTxnLoadFee_
Definition: LoadFeeTrack.h:154
std::uint32_t getLoadBase() const
Definition: LoadFeeTrack.h:90
std::pair< std::uint32_t, std::uint32_t > getScalingFactors() const
Definition: LoadFeeTrack.h:104
static std::uint32_t constexpr lftFeeDecFraction
Definition: LoadFeeTrack.h:146
static std::uint32_t constexpr lftFeeMax
Definition: LoadFeeTrack.h:148
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
Definition: mulDiv.cpp:27
Reflects the fee settings for a particular ledger.
Definition: protocol/Fees.h:33