rippled
Loading...
Searching...
No Matches
Change.cpp
1#include <xrpld/app/ledger/Ledger.h>
2#include <xrpld/app/main/Application.h>
3#include <xrpld/app/misc/AmendmentTable.h>
4#include <xrpld/app/misc/NetworkOPs.h>
5#include <xrpld/app/tx/detail/Change.h>
6
7#include <xrpl/basics/Log.h>
8#include <xrpl/ledger/Sandbox.h>
9#include <xrpl/protocol/Feature.h>
10#include <xrpl/protocol/Indexes.h>
11#include <xrpl/protocol/TxFlags.h>
12
13#include <string_view>
14
15namespace ripple {
16
17template <>
19Transactor::invokePreflight<Change>(PreflightContext const& ctx)
20{
21 // 0 means "Allow any flags"
22 if (auto const ret = preflight0(ctx, 0))
23 return ret;
24
25 auto account = ctx.tx.getAccountID(sfAccount);
26 if (account != beast::zero)
27 {
28 JLOG(ctx.j.warn()) << "Change: Bad source id";
29 return temBAD_SRC_ACCOUNT;
30 }
31
32 // No point in going any further if the transaction fee is malformed.
33 auto const fee = ctx.tx.getFieldAmount(sfFee);
34 if (!fee.native() || fee != beast::zero)
35 {
36 JLOG(ctx.j.warn()) << "Change: invalid fee";
37 return temBAD_FEE;
38 }
39
40 if (!ctx.tx.getSigningPubKey().empty() || !ctx.tx.getSignature().empty() ||
41 ctx.tx.isFieldPresent(sfSigners))
42 {
43 JLOG(ctx.j.warn()) << "Change: Bad signature";
44 return temBAD_SIGNATURE;
45 }
46
47 if (ctx.tx.getFieldU32(sfSequence) != 0 ||
48 ctx.tx.isFieldPresent(sfPreviousTxnID))
49 {
50 JLOG(ctx.j.warn()) << "Change: Bad sequence";
51 return temBAD_SEQUENCE;
52 }
53
54 if (ctx.tx.getTxnType() == ttUNL_MODIFY &&
55 !ctx.rules.enabled(featureNegativeUNL))
56 {
57 JLOG(ctx.j.warn()) << "Change: NegativeUNL not enabled";
58 return temDISABLED;
59 }
60
61 return tesSUCCESS;
62}
63
64TER
66{
67 // If tapOPEN_LEDGER is resurrected into ApplyFlags,
68 // this block can be moved to preflight.
69 if (ctx.view.open())
70 {
71 JLOG(ctx.j.warn()) << "Change transaction against open ledger";
72 return temINVALID;
73 }
74
75 switch (ctx.tx.getTxnType())
76 {
77 case ttFEE:
78 if (ctx.view.rules().enabled(featureXRPFees))
79 {
80 // The ttFEE transaction format defines these fields as
81 // optional, but once the XRPFees feature is enabled, they are
82 // required.
83 if (!ctx.tx.isFieldPresent(sfBaseFeeDrops) ||
84 !ctx.tx.isFieldPresent(sfReserveBaseDrops) ||
85 !ctx.tx.isFieldPresent(sfReserveIncrementDrops))
86 return temMALFORMED;
87 // The ttFEE transaction format defines these fields as
88 // optional, but once the XRPFees feature is enabled, they are
89 // forbidden.
90 if (ctx.tx.isFieldPresent(sfBaseFee) ||
91 ctx.tx.isFieldPresent(sfReferenceFeeUnits) ||
92 ctx.tx.isFieldPresent(sfReserveBase) ||
93 ctx.tx.isFieldPresent(sfReserveIncrement))
94 return temMALFORMED;
95 }
96 else
97 {
98 // The ttFEE transaction format formerly defined these fields
99 // as required. When the XRPFees feature was implemented, they
100 // were changed to be optional. Until the feature has been
101 // enabled, they are required.
102 if (!ctx.tx.isFieldPresent(sfBaseFee) ||
103 !ctx.tx.isFieldPresent(sfReferenceFeeUnits) ||
104 !ctx.tx.isFieldPresent(sfReserveBase) ||
105 !ctx.tx.isFieldPresent(sfReserveIncrement))
106 return temMALFORMED;
107 // The ttFEE transaction format defines these fields as
108 // optional, but without the XRPFees feature, they are
109 // forbidden.
110 if (ctx.tx.isFieldPresent(sfBaseFeeDrops) ||
111 ctx.tx.isFieldPresent(sfReserveBaseDrops) ||
112 ctx.tx.isFieldPresent(sfReserveIncrementDrops))
113 return temDISABLED;
114 }
115 return tesSUCCESS;
116 case ttAMENDMENT:
117 case ttUNL_MODIFY:
118 return tesSUCCESS;
119 default:
120 return temUNKNOWN;
121 }
122}
123
124TER
126{
127 switch (ctx_.tx.getTxnType())
128 {
129 case ttAMENDMENT:
130 return applyAmendment();
131 case ttFEE:
132 return applyFee();
133 case ttUNL_MODIFY:
134 return applyUNLModify();
135 // LCOV_EXCL_START
136 default:
137 UNREACHABLE("ripple::Change::doApply : invalid transaction type");
138 return tefFAILURE;
139 // LCOV_EXCL_STOP
140 }
141}
142
143void
145{
146 XRPL_ASSERT(
147 account_ == beast::zero, "ripple::Change::preCompute : zero account");
148}
149
150TER
152{
153 uint256 amendment(ctx_.tx.getFieldH256(sfAmendment));
154
155 auto const k = keylet::amendments();
156
157 SLE::pointer amendmentObject = view().peek(k);
158
159 if (!amendmentObject)
160 {
161 amendmentObject = std::make_shared<SLE>(k);
162 view().insert(amendmentObject);
163 }
164
165 STVector256 amendments = amendmentObject->getFieldV256(sfAmendments);
166
167 if (std::find(amendments.begin(), amendments.end(), amendment) !=
168 amendments.end())
169 return tefALREADY;
170
171 auto flags = ctx_.tx.getFlags();
172
173 bool const gotMajority = (flags & tfGotMajority) != 0;
174 bool const lostMajority = (flags & tfLostMajority) != 0;
175
176 if (gotMajority && lostMajority)
177 return temINVALID_FLAG;
178
179 STArray newMajorities(sfMajorities);
180
181 bool found = false;
182 if (amendmentObject->isFieldPresent(sfMajorities))
183 {
184 STArray const& oldMajorities =
185 amendmentObject->getFieldArray(sfMajorities);
186 for (auto const& majority : oldMajorities)
187 {
188 if (majority.getFieldH256(sfAmendment) == amendment)
189 {
190 if (gotMajority)
191 return tefALREADY;
192 found = true;
193 }
194 else
195 {
196 // pass through
197 newMajorities.push_back(majority);
198 }
199 }
200 }
201
202 if (!found && lostMajority)
203 return tefALREADY;
204
205 if (gotMajority)
206 {
207 // This amendment now has a majority
208 newMajorities.push_back(STObject::makeInnerObject(sfMajority));
209 auto& entry = newMajorities.back();
210 entry[sfAmendment] = amendment;
211 entry[sfCloseTime] =
213
214 if (!ctx_.app.getAmendmentTable().isSupported(amendment))
215 {
216 JLOG(j_.warn()) << "Unsupported amendment " << amendment
217 << " received a majority.";
218 }
219 }
220 else if (!lostMajority)
221 {
222 // No flags, enable amendment
223 amendments.push_back(amendment);
224 amendmentObject->setFieldV256(sfAmendments, amendments);
225
226 ctx_.app.getAmendmentTable().enable(amendment);
227
228 if (!ctx_.app.getAmendmentTable().isSupported(amendment))
229 {
230 JLOG(j_.error()) << "Unsupported amendment " << amendment
231 << " activated: server blocked.";
233 }
234 }
235
236 if (newMajorities.empty())
237 amendmentObject->makeFieldAbsent(sfMajorities);
238 else
239 amendmentObject->setFieldArray(sfMajorities, newMajorities);
240
241 view().update(amendmentObject);
242
243 return tesSUCCESS;
244}
245
246TER
248{
249 auto const k = keylet::fees();
250
251 SLE::pointer feeObject = view().peek(k);
252
253 if (!feeObject)
254 {
255 feeObject = std::make_shared<SLE>(k);
256 view().insert(feeObject);
257 }
258 auto set = [](SLE::pointer& feeObject, STTx const& tx, auto const& field) {
259 feeObject->at(field) = tx[field];
260 };
261 if (view().rules().enabled(featureXRPFees))
262 {
263 set(feeObject, ctx_.tx, sfBaseFeeDrops);
264 set(feeObject, ctx_.tx, sfReserveBaseDrops);
265 set(feeObject, ctx_.tx, sfReserveIncrementDrops);
266 // Ensure the old fields are removed
267 feeObject->makeFieldAbsent(sfBaseFee);
268 feeObject->makeFieldAbsent(sfReferenceFeeUnits);
269 feeObject->makeFieldAbsent(sfReserveBase);
270 feeObject->makeFieldAbsent(sfReserveIncrement);
271 }
272 else
273 {
274 set(feeObject, ctx_.tx, sfBaseFee);
275 set(feeObject, ctx_.tx, sfReferenceFeeUnits);
276 set(feeObject, ctx_.tx, sfReserveBase);
277 set(feeObject, ctx_.tx, sfReserveIncrement);
278 }
279
280 view().update(feeObject);
281
282 JLOG(j_.warn()) << "Fees have been changed";
283 return tesSUCCESS;
284}
285
286TER
288{
289 if (!isFlagLedger(view().seq()))
290 {
291 JLOG(j_.warn()) << "N-UNL: applyUNLModify, not a flag ledger, seq="
292 << view().seq();
293 return tefFAILURE;
294 }
295
296 if (!ctx_.tx.isFieldPresent(sfUNLModifyDisabling) ||
297 ctx_.tx.getFieldU8(sfUNLModifyDisabling) > 1 ||
298 !ctx_.tx.isFieldPresent(sfLedgerSequence) ||
299 !ctx_.tx.isFieldPresent(sfUNLModifyValidator))
300 {
301 JLOG(j_.warn()) << "N-UNL: applyUNLModify, wrong Tx format.";
302 return tefFAILURE;
303 }
304
305 bool const disabling = ctx_.tx.getFieldU8(sfUNLModifyDisabling);
306 auto const seq = ctx_.tx.getFieldU32(sfLedgerSequence);
307 if (seq != view().seq())
308 {
309 JLOG(j_.warn()) << "N-UNL: applyUNLModify, wrong ledger seq=" << seq;
310 return tefFAILURE;
311 }
312
313 Blob const validator = ctx_.tx.getFieldVL(sfUNLModifyValidator);
314 if (!publicKeyType(makeSlice(validator)))
315 {
316 JLOG(j_.warn()) << "N-UNL: applyUNLModify, bad validator key";
317 return tefFAILURE;
318 }
319
320 JLOG(j_.info()) << "N-UNL: applyUNLModify, "
321 << (disabling ? "ToDisable" : "ToReEnable")
322 << " seq=" << seq
323 << " validator data:" << strHex(validator);
324
325 auto const k = keylet::negativeUNL();
326 SLE::pointer negUnlObject = view().peek(k);
327 if (!negUnlObject)
328 {
329 negUnlObject = std::make_shared<SLE>(k);
330 view().insert(negUnlObject);
331 }
332
333 bool const found = [&] {
334 if (negUnlObject->isFieldPresent(sfDisabledValidators))
335 {
336 auto const& negUnl =
337 negUnlObject->getFieldArray(sfDisabledValidators);
338 for (auto const& v : negUnl)
339 {
340 if (v.isFieldPresent(sfPublicKey) &&
341 v.getFieldVL(sfPublicKey) == validator)
342 return true;
343 }
344 }
345 return false;
346 }();
347
348 if (disabling)
349 {
350 // cannot have more than one toDisable
351 if (negUnlObject->isFieldPresent(sfValidatorToDisable))
352 {
353 JLOG(j_.warn()) << "N-UNL: applyUNLModify, already has ToDisable";
354 return tefFAILURE;
355 }
356
357 // cannot be the same as toReEnable
358 if (negUnlObject->isFieldPresent(sfValidatorToReEnable))
359 {
360 if (negUnlObject->getFieldVL(sfValidatorToReEnable) == validator)
361 {
362 JLOG(j_.warn())
363 << "N-UNL: applyUNLModify, ToDisable is same as ToReEnable";
364 return tefFAILURE;
365 }
366 }
367
368 // cannot be in negative UNL already
369 if (found)
370 {
371 JLOG(j_.warn())
372 << "N-UNL: applyUNLModify, ToDisable already in negative UNL";
373 return tefFAILURE;
374 }
375
376 negUnlObject->setFieldVL(sfValidatorToDisable, validator);
377 }
378 else
379 {
380 // cannot have more than one toReEnable
381 if (negUnlObject->isFieldPresent(sfValidatorToReEnable))
382 {
383 JLOG(j_.warn()) << "N-UNL: applyUNLModify, already has ToReEnable";
384 return tefFAILURE;
385 }
386
387 // cannot be the same as toDisable
388 if (negUnlObject->isFieldPresent(sfValidatorToDisable))
389 {
390 if (negUnlObject->getFieldVL(sfValidatorToDisable) == validator)
391 {
392 JLOG(j_.warn())
393 << "N-UNL: applyUNLModify, ToReEnable is same as ToDisable";
394 return tefFAILURE;
395 }
396 }
397
398 // must be in negative UNL
399 if (!found)
400 {
401 JLOG(j_.warn())
402 << "N-UNL: applyUNLModify, ToReEnable is not in negative UNL";
403 return tefFAILURE;
404 }
405
406 negUnlObject->setFieldVL(sfValidatorToReEnable, validator);
407 }
408
409 view().update(negUnlObject);
410 return tesSUCCESS;
411}
412
413} // namespace ripple
Stream error() const
Definition Journal.h:327
Stream info() const
Definition Journal.h:315
Stream warn() const
Definition Journal.h:321
virtual bool isSupported(uint256 const &amendment) const =0
virtual bool enable(uint256 const &amendment)=0
virtual NetworkOPs & getOPs()=0
virtual AmendmentTable & getAmendmentTable()=0
Application & app
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
TER applyUNLModify()
Definition Change.cpp:287
void preCompute() override
Definition Change.cpp:144
static TER preclaim(PreclaimContext const &ctx)
Definition Change.cpp:65
TER applyAmendment()
Definition Change.cpp:151
TER doApply() override
Definition Change.cpp:125
virtual void setAmendmentBlocked()=0
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition ReadView.h:92
virtual bool open() const =0
Returns true if this reflects an open ledger.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition ReadView.h:99
virtual Rules const & rules() const =0
Returns the tx processing rules.
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:111
bool empty() const
Definition STArray.h:235
void push_back(STObject const &object)
Definition STArray.h:193
STObject & back()
Definition STArray.h:174
unsigned char getFieldU8(SField const &field) const
Definition STObject.cpp:584
Blob getFieldVL(SField const &field) const
Definition STObject.cpp:644
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:638
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:596
STAmount const & getFieldAmount(SField const &field) const
Definition STObject.cpp:652
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:465
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:76
std::uint32_t getFlags() const
Definition STObject.cpp:518
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:626
static Blob getSignature(STObject const &sigObject)
Definition STTx.cpp:184
Blob getSigningPubKey() const
Definition STTx.h:224
TxType getTxnType() const
Definition STTx.h:218
AccountID const account_
Definition Transactor.h:128
ApplyView & view()
Definition Transactor.h:144
beast::Journal const j_
Definition Transactor.h:126
ApplyContext & ctx_
Definition Transactor.h:124
T empty(T... args)
T find(T... args)
T is_same_v
Keylet const & negativeUNL() noexcept
The (fixed) index of the object containing the ledger negativeUNL.
Definition Indexes.cpp:211
Keylet const & amendments() noexcept
The index of the amendment table.
Definition Indexes.cpp:195
Keylet const & fees() noexcept
The (fixed) index of the object containing the ledger fees.
Definition Indexes.cpp:203
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
constexpr std::uint32_t tfGotMajority
Definition TxFlags.h:109
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,...
@ tefFAILURE
Definition TER.h:147
@ tefALREADY
Definition TER.h:148
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
bool isFlagLedger(LedgerIndex seq)
Returns true if the given ledgerIndex is a flag ledgerIndex.
Definition Ledger.cpp:942
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:11
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition Slice.h:225
@ tesSUCCESS
Definition TER.h:226
constexpr std::uint32_t tfLostMajority
Definition TxFlags.h:110
NotTEC preflight0(PreflightContext const &ctx, std::uint32_t flagMask)
Performs early sanity checks on the txid.
@ temBAD_SRC_ACCOUNT
Definition TER.h:87
@ temUNKNOWN
Definition TER.h:105
@ temBAD_FEE
Definition TER.h:73
@ temBAD_SEQUENCE
Definition TER.h:85
@ temMALFORMED
Definition TER.h:68
@ temINVALID
Definition TER.h:91
@ temINVALID_FLAG
Definition TER.h:92
@ temDISABLED
Definition TER.h:95
@ temBAD_SIGNATURE
Definition TER.h:86
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
beast::Journal const j
Definition Transactor.h:69
State information when preflighting a tx.
Definition Transactor.h:16
beast::Journal const j
Definition Transactor.h:23
T time_since_epoch(T... args)