xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
server
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
14
namespace
xrpl
{
15
16
bool
17
LoadFeeTrack::raiseLocalFee
()
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
27
localTxnLoadFee_
=
std::max
(
localTxnLoadFee_
,
remoteTxnLoadFee_
);
28
29
// Increase slowly
30
localTxnLoadFee_
+= (
localTxnLoadFee_
/
kLftFeeIncFraction
);
31
32
localTxnLoadFee_
=
std::min
(
localTxnLoadFee_
,
kLftFeeMax
);
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
41
bool
42
LoadFeeTrack::lowerLocalFee
()
43
{
44
std::scoped_lock
const
sl(
lock_
);
45
std::uint32_t
const
origFee =
localTxnLoadFee_
;
46
raiseCount_
= 0;
47
48
// Reduce slowly
49
localTxnLoadFee_
-= (
localTxnLoadFee_
/
kLftFeeDecFraction
);
50
51
localTxnLoadFee_
=
std::max
(
localTxnLoadFee_
,
kLftNormalFee
);
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
63
XRPAmount
64
scaleFeeLoad
(
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
algorithm
xrpl::LoadFeeTrack
Manages the current fee schedule.
Definition
LoadFeeTrack.h:27
xrpl::LoadFeeTrack::kLftNormalFee
static constexpr std::uint32_t kLftNormalFee
Definition
LoadFeeTrack.h:117
xrpl::LoadFeeTrack::raiseLocalFee
bool raiseLocalFee()
Definition
LoadFeeTrack.cpp:17
xrpl::LoadFeeTrack::kLftFeeDecFraction
static constexpr std::uint32_t kLftFeeDecFraction
Definition
LoadFeeTrack.h:119
xrpl::LoadFeeTrack::remoteTxnLoadFee_
std::uint32_t remoteTxnLoadFee_
Definition
LoadFeeTrack.h:126
xrpl::LoadFeeTrack::lowerLocalFee
bool lowerLocalFee()
Definition
LoadFeeTrack.cpp:42
xrpl::LoadFeeTrack::getLoadBase
static std::uint32_t getLoadBase()
Definition
LoadFeeTrack.h:66
xrpl::LoadFeeTrack::kLftFeeMax
static constexpr std::uint32_t kLftFeeMax
Definition
LoadFeeTrack.h:120
xrpl::LoadFeeTrack::lock_
std::mutex lock_
Definition
LoadFeeTrack.h:123
xrpl::LoadFeeTrack::localTxnLoadFee_
std::uint32_t localTxnLoadFee_
Definition
LoadFeeTrack.h:125
xrpl::LoadFeeTrack::raiseCount_
std::uint32_t raiseCount_
Definition
LoadFeeTrack.h:128
xrpl::LoadFeeTrack::kLftFeeIncFraction
static constexpr std::uint32_t kLftFeeIncFraction
Definition
LoadFeeTrack.h:118
xrpl::LoadFeeTrack::j_
beast::Journal const j_
Definition
LoadFeeTrack.h:122
xrpl::LoadFeeTrack::getScalingFactors
std::pair< std::uint32_t, std::uint32_t > getScalingFactors() const
Definition
LoadFeeTrack.h:79
xrpl::XRPAmount
Definition
XRPAmount.h:28
cstdint
std::uint32_t
std::max
T max(T... args)
std::min
T min(T... args)
mutex
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::mulDiv
std::optional< std::uint64_t > mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div)
Return value*mul/div accurately.
Definition
libxrpl/basics/mulDiv.cpp:11
xrpl::safeCast
constexpr Dest safeCast(Src s) noexcept
Definition
safe_cast.h:21
xrpl::scaleFeeLoad
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
Definition
LoadFeeTrack.cpp:64
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
std::scoped_lock
stdexcept
xrpl::Fees
Reflects the fee settings for a particular ledger.
Definition
protocol/Fees.h:20
Generated by
1.17.0