rippled
Loading...
Searching...
No Matches
LoadFeeTrack.cpp
1#include <xrpl/basics/Log.h>
2#include <xrpl/basics/contract.h>
3#include <xrpl/basics/safe_cast.h>
4#include <xrpl/protocol/Units.h>
5#include <xrpl/server/LoadFeeTrack.h>
6
7#include <algorithm>
8#include <cstdint>
9
10namespace xrpl {
11
12bool
14{
15 std::lock_guard const sl(lock_);
16
17 if (++raiseCount_ < 2)
18 return false;
19
20 std::uint32_t const origFee = localTxnLoadFee_;
21
22 // make sure this fee takes effect
24
25 // Increase slowly
27
29
30 if (origFee == localTxnLoadFee_)
31 return false;
32
33 JLOG(j_.debug()) << "Local load fee raised from " << origFee << " to " << localTxnLoadFee_;
34 return true;
35}
36
37bool
39{
40 std::lock_guard const sl(lock_);
41 std::uint32_t const origFee = localTxnLoadFee_;
42 raiseCount_ = 0;
43
44 // Reduce slowly
46
48
49 if (origFee == localTxnLoadFee_)
50 return false;
51
52 JLOG(j_.debug()) << "Local load fee lowered from " << origFee << " to " << localTxnLoadFee_;
53 return true;
54}
55
56//------------------------------------------------------------------------------
57
58// Scale using load as well as base rate
60scaleFeeLoad(XRPAmount fee, LoadFeeTrack const& feeTrack, Fees const& fees, bool bUnlimited)
61{
62 if (fee == 0)
63 return fee;
64
65 // Collect the fee rates
66 auto [feeFactor, uRemFee] = feeTrack.getScalingFactors();
67
68 // Let privileged users pay the normal fee until
69 // the local load exceeds four times the remote.
70 if (bUnlimited && (feeFactor > uRemFee) && (feeFactor < (4 * uRemFee)))
71 feeFactor = uRemFee;
72
73 // Compute:
74 // fee = fee * feeFactor / (lftNormalFee);
75 // without overflow, and as accurately as possible
76
77 auto const result = mulDiv(fee, feeFactor, safe_cast<std::uint64_t>(feeTrack.getLoadBase()));
78 if (!result)
79 Throw<std::overflow_error>("scaleFeeLoad");
80 return *result;
81}
82
83} // namespace xrpl
Stream debug() const
Definition Journal.h:301
Manages the current fee schedule.
static std::uint32_t constexpr lftFeeDecFraction
static std::uint32_t constexpr lftFeeIncFraction
std::uint32_t remoteTxnLoadFee_
static std::uint32_t constexpr lftNormalFee
std::uint32_t getLoadBase() const
std::uint32_t localTxnLoadFee_
std::uint32_t raiseCount_
beast::Journal const j_
std::pair< std::uint32_t, std::uint32_t > getScalingFactors() const
static std::uint32_t constexpr lftFeeMax
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.
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
Reflects the fee settings for a particular ledger.