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/ledger/ReadView.h>
22
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
28#include <cstdint>
29
30namespace ripple {
31
32bool
34{
36
37 if (++raiseCount_ < 2)
38 return false;
39
40 std::uint32_t const origFee = localTxnLoadFee_;
41
42 // make sure this fee takes effect
45
46 // Increase slowly
48
51
52 if (origFee == localTxnLoadFee_)
53 return false;
54
55 JLOG(j_.debug()) << "Local load fee raised from " << origFee << " to "
57 return true;
58}
59
60bool
62{
64 std::uint32_t const origFee = localTxnLoadFee_;
65 raiseCount_ = 0;
66
67 // Reduce slowly
69
72
73 if (origFee == localTxnLoadFee_)
74 return false;
75
76 JLOG(j_.debug()) << "Local load fee lowered from " << origFee << " to "
78 return true;
79}
80
81//------------------------------------------------------------------------------
82
83// Scale using load as well as base rate
86 XRPAmount fee,
87 LoadFeeTrack const& feeTrack,
88 Fees const& fees,
89 bool bUnlimited)
90{
91 if (fee == 0)
92 return fee;
93
94 // Collect the fee rates
95 auto [feeFactor, uRemFee] = feeTrack.getScalingFactors();
96
97 // Let privileged users pay the normal fee until
98 // the local load exceeds four times the remote.
99 if (bUnlimited && (feeFactor > uRemFee) && (feeFactor < (4 * uRemFee)))
100 feeFactor = uRemFee;
101
102 // Compute:
103 // fee = fee * feeFactor / (lftNormalFee);
104 // without overflow, and as accurately as possible
105
106 auto const result = mulDiv(
107 fee, feeFactor, safe_cast<std::uint64_t>(feeTrack.getLoadBase()));
108 if (!result)
109 Throw<std::overflow_error>("scaleFeeLoad");
110 return *result;
111}
112
113} // 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.