xrpld
Loading...
Searching...
No Matches
LoadFeeTrack.cpp
1#include <xrpl/server/LoadFeeTrack.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/basics/safe_cast.h>
6#include <xrpl/protocol/Units.h>
7#include <xrpl/protocol/XRPAmount.h>
8
9#include <algorithm>
10#include <cstdint>
11#include <mutex>
12#include <stdexcept>
13
14namespace xrpl {
15
16bool
18{
19 std::scoped_lock const sl(lock_);
20
21 if (++raiseCount_ < 2)
22 return false;
23
24 std::uint32_t const origFee = localTxnLoadFee_;
25
26 // make sure this fee takes effect
28
29 // Increase slowly
31
33
34 if (origFee == localTxnLoadFee_)
35 return false;
36
37 JLOG(j_.debug()) << "Local load fee raised from " << origFee << " to " << localTxnLoadFee_;
38 return true;
39}
40
41bool
43{
44 std::scoped_lock const sl(lock_);
45 std::uint32_t const origFee = localTxnLoadFee_;
46 raiseCount_ = 0;
47
48 // Reduce slowly
50
52
53 if (origFee == localTxnLoadFee_)
54 return false;
55
56 JLOG(j_.debug()) << "Local load fee lowered from " << origFee << " to " << localTxnLoadFee_;
57 return true;
58}
59
60//------------------------------------------------------------------------------
61
62// Scale using load as well as base rate
64scaleFeeLoad(XRPAmount fee, LoadFeeTrack const& feeTrack, Fees const& fees, bool bUnlimited)
65{
66 if (fee == 0)
67 return fee;
68
69 // Collect the fee rates
70 auto [feeFactor, uRemFee] = feeTrack.getScalingFactors();
71
72 // Let privileged users pay the normal fee until
73 // the local load exceeds four times the remote.
74 if (bUnlimited && (feeFactor > uRemFee) && (feeFactor < (4 * uRemFee)))
75 feeFactor = uRemFee;
76
77 // Compute:
78 // fee = fee * feeFactor / (lftNormalFee);
79 // without overflow, and as accurately as possible
80
81 auto const result = mulDiv(fee, feeFactor, safeCast<std::uint64_t>(feeTrack.getLoadBase()));
82 if (!result)
83 Throw<std::overflow_error>("scaleFeeLoad");
84 return *result;
85}
86
87} // namespace xrpl
Manages the current fee schedule.
static constexpr std::uint32_t kLftNormalFee
static constexpr std::uint32_t kLftFeeDecFraction
std::uint32_t remoteTxnLoadFee_
static std::uint32_t getLoadBase()
static constexpr std::uint32_t kLftFeeMax
std::uint32_t localTxnLoadFee_
std::uint32_t raiseCount_
static constexpr std::uint32_t kLftFeeIncFraction
beast::Journal const j_
std::pair< std::uint32_t, std::uint32_t > getScalingFactors() const
T max(T... args)
T min(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safeCast(Src s) noexcept
Definition safe_cast.h:21
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
Reflects the fee settings for a particular ledger.