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
22#include <xrpl/basics/Log.h>
23#include <xrpl/basics/contract.h>
24#include <xrpl/basics/safe_cast.h>
25#include <xrpl/protocol/Units.h>
26
27#include <cstdint>
28
29namespace ripple {
30
31bool
33{
35
36 if (++raiseCount_ < 2)
37 return false;
38
39 std::uint32_t const origFee = localTxnLoadFee_;
40
41 // make sure this fee takes effect
44
45 // Increase slowly
47
50
51 if (origFee == localTxnLoadFee_)
52 return false;
53
54 JLOG(j_.debug()) << "Local load fee raised from " << origFee << " to "
56 return true;
57}
58
59bool
61{
63 std::uint32_t const origFee = localTxnLoadFee_;
64 raiseCount_ = 0;
65
66 // Reduce slowly
68
71
72 if (origFee == localTxnLoadFee_)
73 return false;
74
75 JLOG(j_.debug()) << "Local load fee lowered from " << origFee << " to "
77 return true;
78}
79
80//------------------------------------------------------------------------------
81
82// Scale using load as well as base rate
85 XRPAmount fee,
86 LoadFeeTrack const& feeTrack,
87 Fees const& fees,
88 bool bUnlimited)
89{
90 if (fee == 0)
91 return fee;
92
93 // Collect the fee rates
94 auto [feeFactor, uRemFee] = feeTrack.getScalingFactors();
95
96 // Let privileged users pay the normal fee until
97 // the local load exceeds four times the remote.
98 if (bUnlimited && (feeFactor > uRemFee) && (feeFactor < (4 * uRemFee)))
99 feeFactor = uRemFee;
100
101 // Compute:
102 // fee = fee * feeFactor / (lftNormalFee);
103 // without overflow, and as accurately as possible
104
105 auto const result = mulDiv(
106 fee, feeFactor, safe_cast<std::uint64_t>(feeTrack.getLoadBase()));
107 if (!result)
108 Throw<std::overflow_error>("scaleFeeLoad");
109 return *result;
110}
111
112} // namespace ripple
Stream debug() const
Definition Journal.h:328
Manages the current fee schedule.
static std::uint32_t constexpr lftFeeIncFraction
beast::Journal const j_
static std::uint32_t constexpr lftNormalFee
std::uint32_t raiseCount_
std::uint32_t localTxnLoadFee_
std::uint32_t remoteTxnLoadFee_
std::uint32_t getLoadBase() const
std::pair< std::uint32_t, std::uint32_t > getScalingFactors() const
static std::uint32_t constexpr lftFeeDecFraction
static std::uint32_t constexpr lftFeeMax
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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.
Reflects the fee settings for a particular ledger.