xrpld
Loading...
Searching...
No Matches
FeeVoteImpl.cpp
1#include <xrpld/app/misc/FeeVote.h>
2#include <xrpld/core/Config.h>
3
4#include <xrpl/basics/Log.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/beast/utility/Journal.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/protocol/Feature.h>
10#include <xrpl/protocol/Fees.h>
11#include <xrpl/protocol/Protocol.h> // IWYU pragma: keep
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/STAccount.h> // IWYU pragma: keep
14#include <xrpl/protocol/STTx.h>
15#include <xrpl/protocol/STValidation.h>
16#include <xrpl/protocol/Serializer.h>
17#include <xrpl/protocol/SystemParameters.h>
18#include <xrpl/protocol/TxFormats.h>
19#include <xrpl/protocol/XRPAmount.h>
20#include <xrpl/shamap/SHAMap.h>
21#include <xrpl/shamap/SHAMapItem.h>
22#include <xrpl/shamap/SHAMapTreeNode.h>
23
24#include <algorithm>
25#include <cstdint>
26#include <limits>
27#include <map>
28#include <memory>
29#include <utility>
30#include <vector>
31
32namespace xrpl {
33
34namespace detail {
35
36template <typename ValueType>
38{
39private:
40 using value_type = ValueType;
41 value_type const current_; // The current setting
42 value_type const target_; // The setting we want
44
45public:
47 {
48 // Add our vote
50 }
51
52 void
54 {
55 ++voteMap_[vote];
56 }
57
58 void
60 {
62 }
63
64 [[nodiscard]] value_type
65 current() const
66 {
67 return current_;
68 }
69
70 [[nodiscard]] std::pair<value_type, bool>
71 getVotes() const;
72};
73
74template <typename ValueType>
75auto
77{
78 value_type ourVote = current_;
79 int weight = 0;
80 for (auto const& [key, val] : voteMap_)
81 {
82 // Take most voted value between current and target, inclusive
83 if ((key <= std::max(target_, current_)) && (key >= std::min(target_, current_)) &&
84 (val > weight))
85 {
86 ourVote = key;
87 weight = val;
88 }
89 }
90
91 return {ourVote, ourVote != current_};
92}
93
94} // namespace detail
95
96//------------------------------------------------------------------------------
97
98class FeeVoteImpl : public FeeVote
99{
100private:
103
104public:
105 FeeVoteImpl(FeeSetup const& setup, beast::Journal journal);
106
107 void
108 doValidation(Fees const& lastFees, Rules const& rules, STValidation& val) override;
109
110 void
111 doVoting(
112 std::shared_ptr<ReadView const> const& lastClosedLedger,
113 std::vector<std::shared_ptr<STValidation>> const& parentValidations,
114 std::shared_ptr<SHAMap> const& initialPosition) override;
115};
116
117//--------------------------------------------------------------------------
118
120 : target_(setup), journal_(journal)
121{
122}
123
124void
125FeeVoteImpl::doValidation(Fees const& lastFees, Rules const& rules, STValidation& v)
126{
127 // Values should always be in a valid range (because the voting process
128 // will ignore out-of-range values) but if we detect such a case, we do
129 // not send a value.
130 auto const vote = [&v, this](
131 auto const current, auto target, char const* name, auto const& sfield) {
132 if (current != target)
133 {
134 JLOG(journal_.info()) << "Voting for " << name << " of " << target;
135
136 v[sfield] = target;
137 }
138 };
139 vote(lastFees.base, target_.referenceFee, "base fee", sfBaseFeeDrops);
140 vote(lastFees.reserve, target_.accountReserve, "base reserve", sfReserveBaseDrops);
141 vote(lastFees.increment, target_.ownerReserve, "reserve increment", sfReserveIncrementDrops);
142 if (rules.enabled(featureSmartEscrow))
143 {
144 if (target_.gasLimit <= kMaxGasLimit)
145 {
146 vote(lastFees.gasLimit, target_.gasLimit, "gas limit", sfGasLimit);
147 }
148 if (target_.bytecodeSizeLimit <= kMaxBytecodeSizeLimit)
149 {
150 vote(
151 lastFees.bytecodeSizeLimit,
152 target_.bytecodeSizeLimit,
153 "bytecode size limit",
154 sfBytecodeSizeLimit);
155 }
156 if (target_.gasPrice >= kMinGasPrice)
157 {
158 vote(lastFees.gasPrice, target_.gasPrice, "gas price", sfGasPrice);
159 }
160 }
161}
162
163void
165 std::shared_ptr<ReadView const> const& lastClosedLedger,
167 std::shared_ptr<SHAMap> const& initialPosition)
168{
169 // LCL must be flag ledger
170 XRPL_ASSERT(
171 lastClosedLedger && isFlagLedger(lastClosedLedger->seq()),
172 "xrpl::FeeVoteImpl::doVoting : has a flag ledger");
173
174 detail::VotableValue baseFeeVote(lastClosedLedger->fees().base, target_.referenceFee);
175
176 detail::VotableValue baseReserveVote(lastClosedLedger->fees().reserve, target_.accountReserve);
177
178 detail::VotableValue incReserveVote(lastClosedLedger->fees().increment, target_.ownerReserve);
179
180 auto const validOrCurrent =
181 [](std::uint32_t target, std::uint32_t min, std::uint32_t max, std::uint32_t current) {
182 return (target >= min && target <= max) ? target : current;
183 };
184
185 detail::VotableValue gasLimitVote(
186 lastClosedLedger->fees().gasLimit,
187 validOrCurrent(target_.gasLimit, 0, kMaxGasLimit, lastClosedLedger->fees().gasLimit));
188
189 detail::VotableValue bytecodeSizeLimitVote(
190 lastClosedLedger->fees().bytecodeSizeLimit,
191 validOrCurrent(
192 target_.bytecodeSizeLimit,
193 0,
195 lastClosedLedger->fees().bytecodeSizeLimit));
196
197 detail::VotableValue gasPriceVote(
198 lastClosedLedger->fees().gasPrice,
199 validOrCurrent(
200 target_.gasPrice,
203 lastClosedLedger->fees().gasPrice));
204
205 auto const doVote = [](std::shared_ptr<STValidation> const& val,
207 SF_AMOUNT const& xrpField) {
208 if (auto const field = ~val->at(~xrpField); field && field->native())
209 {
210 auto const vote = field->xrp();
211 if (isLegalAmountSigned(vote))
212 {
213 value.addVote(vote);
214 }
215 else
216 {
217 value.noVote();
218 }
219 }
220 else
221 {
222 value.noVote();
223 }
224 };
225
226 for (auto const& val : set)
227 {
228 if (!val->isTrusted())
229 continue;
230 doVote(val, baseFeeVote, sfBaseFeeDrops);
231 doVote(val, baseReserveVote, sfReserveBaseDrops);
232 doVote(val, incReserveVote, sfReserveIncrementDrops);
233 }
234
235 auto const& rules = lastClosedLedger->rules();
236 if (rules.enabled(featureSmartEscrow))
237 {
238 auto const doExtensionVote = [](std::shared_ptr<STValidation> const& val,
240 SF_UINT32 const& sfield,
241 std::uint32_t minValue,
242 std::uint32_t maxValue) {
243 if (auto const field = ~val->at(~sfield);
244 field && *field >= minValue && *field <= maxValue)
245 {
246 value.addVote(*field);
247 }
248 else
249 {
250 value.noVote();
251 }
252 };
253
254 for (auto const& val : set)
255 {
256 if (!val->isTrusted())
257 continue;
258 doExtensionVote(val, gasLimitVote, sfGasLimit, 0, kMaxGasLimit);
259 doExtensionVote(
260 val, bytecodeSizeLimitVote, sfBytecodeSizeLimit, 0, kMaxBytecodeSizeLimit);
261 doExtensionVote(
262 val,
263 gasPriceVote,
264 sfGasPrice,
267 }
268 }
269
270 // choose our positions
271 auto const [baseFee, baseFeeChanged] = baseFeeVote.getVotes();
272 auto const [baseReserve, baseReserveChanged] = baseReserveVote.getVotes();
273 auto const [incReserve, incReserveChanged] = incReserveVote.getVotes();
274 auto const [gasLimit, gasLimitChanged] = gasLimitVote.getVotes();
275 auto const [bytecodeSizeLimit, bytecodeSizeLimitChanged] = bytecodeSizeLimitVote.getVotes();
276 auto const [gasPrice, gasPriceChanged] = gasPriceVote.getVotes();
277
278 auto const seq = lastClosedLedger->header().seq + 1;
279
280 // The gas votes only count once the amendment is on. Before it is, the
281 // ledger's gas settings may differ from the config targets, and counting
282 // that as a change would emit a SetFee on every flag ledger that carries
283 // no gas fields and changes nothing.
284 bool const smartEscrowFeeChanged = rules.enabled(featureSmartEscrow) &&
285 (gasLimitChanged || bytecodeSizeLimitChanged || gasPriceChanged);
286
287 // add transactions to our position
288 if (baseFeeChanged || baseReserveChanged || incReserveChanged || smartEscrowFeeChanged)
289 {
290 JLOG(journal_.warn()) << "We are voting for a fee change: " << baseFee << "/" << baseReserve
291 << "/" << incReserve;
292
293 STTx const feeTx(ttFEE, [=, &rules](auto& obj) {
294 obj[sfAccount] = AccountID();
295 obj[sfLedgerSequence] = seq;
296 obj[sfBaseFeeDrops] = baseFee;
297 obj[sfReserveBaseDrops] = baseReserve;
298 obj[sfReserveIncrementDrops] = incReserve;
299 if (rules.enabled(featureSmartEscrow))
300 {
301 obj[sfGasLimit] = gasLimit;
302 obj[sfBytecodeSizeLimit] = bytecodeSizeLimit;
303 obj[sfGasPrice] = gasPrice;
304 }
305 });
306
307 UInt256 const txID = feeTx.getTransactionID();
308
309 JLOG(journal_.warn()) << "Vote: " << txID;
310
311 Serializer s;
312 feeTx.add(s);
313
314 if (!initialPosition->addGiveItem(
316 {
317 JLOG(journal_.warn()) << "Ledger already had fee change";
318 }
319 }
320}
321
322//------------------------------------------------------------------------------
323
325makeFeeVote(FeeSetup const& setup, beast::Journal journal)
326{
327 return std::make_unique<FeeVoteImpl>(setup, journal);
328}
329
330} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
void doValidation(Fees const &lastFees, Rules const &rules, STValidation &val) override
Add local fee preference to validation.
FeeVoteImpl(FeeSetup const &setup, beast::Journal journal)
void doVoting(std::shared_ptr< ReadView const > const &lastClosedLedger, std::vector< std::shared_ptr< STValidation > > const &parentValidations, std::shared_ptr< SHAMap > const &initialPosition) override
Cast our local vote on the fee.
beast::Journal const journal_
Manager to process fee votes.
Definition FeeVote.h:19
Rules controlling protocol behavior.
Definition Rules.h:40
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
Slice slice() const noexcept
Definition Serializer.h:141
std::map< value_type, int > voteMap_
value_type current() const
void addVote(value_type vote)
VotableValue(value_type current, value_type target)
value_type const current_
std::pair< value_type, bool > getVotes() const
T make_unique(T... args)
T max(T... args)
T min(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
bool isFlagLedger(LedgerIndex seq)
Returns true if the given ledgerIndex is a flag ledgerIndex.
Definition Protocol.cpp:11
constexpr std::uint32_t kMinGasPrice
std::unique_ptr< FeeVote > makeFeeVote(FeeSetup const &setup, beast::Journal journal)
Create an instance of the FeeVote logic.
TypedField< STInteger< std::uint32_t > > SF_UINT32
Definition SField.h:343
TypedField< STAmount > SF_AMOUNT
Definition SField.h:357
BaseUInt< 256 > UInt256
Definition base_uint.h:581
boost::intrusive_ptr< SHAMapItem > makeShamapitem(UInt256 const &tag, Slice data)
Definition SHAMapItem.h:148
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
bool isLegalAmountSigned(XRPAmount const &amount)
Returns true if the absolute value of the amount does not exceed the initial XRP in existence.
constexpr std::uint32_t kMaxGasLimit
Hard protocol bounds on the Feature Extension fee settings.
constexpr std::uint32_t kMaxBytecodeSizeLimit
Fee schedule for startup / standalone, and to vote for.
Reflects the fee settings for a particular ledger.
std::uint32_t gasLimit
Gas limit for Feature Extensions (instructions).
XRPAmount reserve
Minimum XRP an account must hold to exist on the ledger.
std::uint32_t bytecodeSizeLimit
Bytecode size limit for Feature Extensions (bytes).
XRPAmount increment
Additional XRP reserve required per owned ledger object.
std::uint32_t gasPrice
Price of WASM gas (micro-drops).
XRPAmount base
Cost of a reference transaction in drops.