rippled
Loading...
Searching...
No Matches
FeeVoteImpl.cpp
1#include <xrpld/app/ledger/Ledger.h>
2#include <xrpld/app/misc/FeeVote.h>
3
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/protocol/STValidation.h>
6#include <xrpl/protocol/st.h>
7
8namespace xrpl {
9
10namespace detail {
11
13{
14private:
16 value_type const current_; // The current setting
17 value_type const target_; // The setting we want
19
20public:
22 {
23 // Add our vote
25 }
26
27 void
29 {
30 ++voteMap_[vote];
31 }
32
33 void
35 {
37 }
38
40 current() const
41 {
42 return current_;
43 }
44
46 getVotes() const;
47};
48
49auto
50VotableValue::getVotes() const -> std::pair<value_type, bool>
51{
52 value_type ourVote = current_;
53 int weight = 0;
54 for (auto const& [key, val] : voteMap_)
55 {
56 // Take most voted value between current and target, inclusive
57 if ((key <= std::max(target_, current_)) && (key >= std::min(target_, current_)) &&
58 (val > weight))
59 {
60 ourVote = key;
61 weight = val;
62 }
63 }
64
65 return {ourVote, ourVote != current_};
66}
67
68} // namespace detail
69
70//------------------------------------------------------------------------------
71
72class FeeVoteImpl : public FeeVote
73{
74private:
77
78public:
79 FeeVoteImpl(FeeSetup const& setup, beast::Journal journal);
80
81 void
82 doValidation(Fees const& lastFees, Rules const& rules, STValidation& val) override;
83
84 void
86 std::shared_ptr<ReadView const> const& lastClosedLedger,
87 std::vector<std::shared_ptr<STValidation>> const& parentValidations,
88 std::shared_ptr<SHAMap> const& initialPosition) override;
89};
90
91//--------------------------------------------------------------------------
92
94 : target_(setup), journal_(journal)
95{
96}
97
98void
99FeeVoteImpl::doValidation(Fees const& lastFees, Rules const& rules, STValidation& v)
100{
101 // Values should always be in a valid range (because the voting process
102 // will ignore out-of-range values) but if we detect such a case, we do
103 // not send a value.
104 if (rules.enabled(featureXRPFees))
105 {
106 auto vote =
107 [&v, this](auto const current, XRPAmount target, char const* name, auto const& sfield) {
108 if (current != target)
109 {
110 JLOG(journal_.info()) << "Voting for " << name << " of " << target;
111
112 v[sfield] = target;
113 }
114 };
115 vote(lastFees.base, target_.reference_fee, "base fee", sfBaseFeeDrops);
116 vote(lastFees.reserve, target_.account_reserve, "base reserve", sfReserveBaseDrops);
117 vote(
118 lastFees.increment,
120 "reserve increment",
121 sfReserveIncrementDrops);
122 }
123 else
124 {
125 auto to32 = [](XRPAmount target) { return target.dropsAs<std::uint32_t>(); };
126 auto to64 = [](XRPAmount target) { return target.dropsAs<std::uint64_t>(); };
127 auto vote = [&v, this](
128 auto const current,
129 XRPAmount target,
130 auto const& convertCallback,
131 char const* name,
132 auto const& sfield) {
133 if (current != target)
134 {
135 JLOG(journal_.info()) << "Voting for " << name << " of " << target;
136
137 if (auto const f = convertCallback(target))
138 v[sfield] = *f;
139 }
140 };
141
142 vote(lastFees.base, target_.reference_fee, to64, "base fee", sfBaseFee);
143 vote(lastFees.reserve, target_.account_reserve, to32, "base reserve", sfReserveBase);
144 vote(
145 lastFees.increment,
147 to32,
148 "reserve increment",
149 sfReserveIncrement);
150 }
151}
152
153void
155 std::shared_ptr<ReadView const> const& lastClosedLedger,
157 std::shared_ptr<SHAMap> const& initialPosition)
158{
159 // LCL must be flag ledger
160 XRPL_ASSERT(
161 lastClosedLedger && isFlagLedger(lastClosedLedger->seq()),
162 "xrpl::FeeVoteImpl::doVoting : has a flag ledger");
163
164 detail::VotableValue baseFeeVote(lastClosedLedger->fees().base, target_.reference_fee);
165
166 detail::VotableValue baseReserveVote(lastClosedLedger->fees().reserve, target_.account_reserve);
167
168 detail::VotableValue incReserveVote(lastClosedLedger->fees().increment, target_.owner_reserve);
169
170 auto const& rules = lastClosedLedger->rules();
171 if (rules.enabled(featureXRPFees))
172 {
173 auto doVote = [](std::shared_ptr<STValidation> const& val,
175 SF_AMOUNT const& xrpField) {
176 if (auto const field = ~val->at(~xrpField); field && field->native())
177 {
178 auto const vote = field->xrp();
179 if (isLegalAmountSigned(vote))
180 value.addVote(vote);
181 else
182 value.noVote();
183 }
184 else
185 {
186 value.noVote();
187 }
188 };
189
190 for (auto const& val : set)
191 {
192 if (!val->isTrusted())
193 continue;
194 doVote(val, baseFeeVote, sfBaseFeeDrops);
195 doVote(val, baseReserveVote, sfReserveBaseDrops);
196 doVote(val, incReserveVote, sfReserveIncrementDrops);
197 }
198 }
199 else
200 {
201 auto doVote = [](std::shared_ptr<STValidation> const& val,
203 auto const& valueField) {
204 if (auto const field = val->at(~valueField))
205 {
206 using XRPType = XRPAmount::value_type;
207 auto const vote = *field;
208 if (vote <= std::numeric_limits<XRPType>::max() &&
209 isLegalAmountSigned(XRPAmount{unsafe_cast<XRPType>(vote)}))
210 value.addVote(XRPAmount{unsafe_cast<XRPType>(vote)});
211 else
212 // Invalid amounts will be treated as if they're
213 // not provided. Don't throw because this value is
214 // provided by an external entity.
215 value.noVote();
216 }
217 else
218 {
219 value.noVote();
220 }
221 };
222
223 for (auto const& val : set)
224 {
225 if (!val->isTrusted())
226 continue;
227 doVote(val, baseFeeVote, sfBaseFee);
228 doVote(val, baseReserveVote, sfReserveBase);
229 doVote(val, incReserveVote, sfReserveIncrement);
230 }
231 }
232
233 // choose our positions
234 // TODO: Use structured binding once LLVM 16 is the minimum supported
235 // version. See also: https://github.com/llvm/llvm-project/issues/48582
236 // https://github.com/llvm/llvm-project/commit/127bf44385424891eb04cff8e52d3f157fc2cb7c
237 auto const baseFee = baseFeeVote.getVotes();
238 auto const baseReserve = baseReserveVote.getVotes();
239 auto const incReserve = incReserveVote.getVotes();
240
241 auto const seq = lastClosedLedger->header().seq + 1;
242
243 // add transactions to our position
244 if (baseFee.second || baseReserve.second || incReserve.second)
245 {
246 JLOG(journal_.warn()) << "We are voting for a fee change: " << baseFee.first << "/"
247 << baseReserve.first << "/" << incReserve.first;
248
249 STTx feeTx(ttFEE, [=, &rules](auto& obj) {
250 obj[sfAccount] = AccountID();
251 obj[sfLedgerSequence] = seq;
252 if (rules.enabled(featureXRPFees))
253 {
254 obj[sfBaseFeeDrops] = baseFee.first;
255 obj[sfReserveBaseDrops] = baseReserve.first;
256 obj[sfReserveIncrementDrops] = incReserve.first;
257 }
258 else
259 {
260 // Without the featureXRPFees amendment, these fields are
261 // required.
262 obj[sfBaseFee] = baseFee.first.dropsAs<std::uint64_t>(baseFeeVote.current());
263 obj[sfReserveBase] =
264 baseReserve.first.dropsAs<std::uint32_t>(baseReserveVote.current());
265 obj[sfReserveIncrement] =
266 incReserve.first.dropsAs<std::uint32_t>(incReserveVote.current());
267 obj[sfReferenceFeeUnits] = Config::FEE_UNITS_DEPRECATED;
268 }
269 });
270
271 uint256 txID = feeTx.getTransactionID();
272
273 JLOG(journal_.warn()) << "Vote: " << txID;
274
275 Serializer s;
276 feeTx.add(s);
277
278 if (!initialPosition->addGiveItem(
280 {
281 JLOG(journal_.warn()) << "Ledger already had fee change";
282 }
283 }
284}
285
286//------------------------------------------------------------------------------
287
290{
291 return std::make_unique<FeeVoteImpl>(setup, journal);
292}
293
294} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Stream info() const
Definition Journal.h:307
Stream warn() const
Definition Journal.h:313
static constexpr std::uint32_t FEE_UNITS_DEPRECATED
Definition Config.h:142
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:11
Rules controlling protocol behavior.
Definition Rules.h:18
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:120
Slice slice() const noexcept
Definition Serializer.h:44
std::int64_t value_type
Definition XRPAmount.h:26
value_type const current_
std::map< value_type, int > voteMap_
std::pair< value_type, bool > getVotes() const
value_type current() const
VotableValue(value_type current, value_type target)
void addVote(value_type vote)
T is_same_v
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
std::unique_ptr< FeeVote > make_FeeVote(FeeSetup const &setup, beast::Journal journal)
Create an instance of the FeeVote logic.
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
boost::intrusive_ptr< SHAMapItem > make_shamapitem(uint256 const &tag, Slice data)
Definition SHAMapItem.h:139
base_uint< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
@ current
This was a new validation and was added.
bool isLegalAmountSigned(XRPAmount const &amount)
Returns true if the absolute value of the amount does not exceed the initial XRP in existence.
Fee schedule for startup / standalone, and to vote for.
Definition Config.h:48
XRPAmount reference_fee
The cost of a reference transaction in drops.
Definition Config.h:50
XRPAmount account_reserve
The account reserve requirement in drops.
Definition Config.h:53
XRPAmount owner_reserve
The per-owned item reserve requirement in drops.
Definition Config.h:56
Reflects the fee settings for a particular ledger.
XRPAmount reserve
XRPAmount increment
XRPAmount base
A field with a type known at compile time.
Definition SField.h:301