xrpld
Loading...
Searching...
No Matches
Change.cpp
1#include <xrpl/tx/transactors/system/Change.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Slice.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/strHex.h>
7#include <xrpl/beast/utility/Zero.h>
8#include <xrpl/beast/utility/instrumentation.h>
9#include <xrpl/core/ServiceRegistry.h>
10#include <xrpl/ledger/AmendmentTable.h>
11#include <xrpl/ledger/entries/AmendmentsEntry.h>
12#include <xrpl/ledger/entries/FeeSettingsEntry.h>
13#include <xrpl/ledger/entries/NegativeUNLEntry.h>
14#include <xrpl/protocol/Feature.h>
15#include <xrpl/protocol/Fees.h>
16#include <xrpl/protocol/Protocol.h>
17#include <xrpl/protocol/PublicKey.h>
18#include <xrpl/protocol/SField.h>
19#include <xrpl/protocol/STArray.h>
20#include <xrpl/protocol/STLedgerEntry.h>
21#include <xrpl/protocol/STTx.h>
22#include <xrpl/protocol/STVector256.h>
23#include <xrpl/protocol/TER.h>
24#include <xrpl/protocol/TxFlags.h>
25#include <xrpl/protocol/TxFormats.h>
26#include <xrpl/protocol/XRPAmount.h>
27#include <xrpl/server/NetworkOPs.h>
28#include <xrpl/tx/Transactor.h>
29
30#include <algorithm>
31
32namespace xrpl {
33
34template <>
37{
38 // 0 means "Allow any flags"
39 // The check for tfEnableAmendmentMask is gated by LendingProtocol because
40 // that feature introduced this parameter, and it's not worth adding another
41 // amendment just for this.
42 if (auto const ret =
43 preflight0(ctx, ctx.rules.enabled(featureLendingProtocol) ? tfEnableAmendmentMask : 0))
44 return ret;
45
46 auto const account = ctx.tx.getAccountID(sfAccount);
47 if (account != beast::kZero)
48 {
49 JLOG(ctx.j.warn()) << "Change: Bad source id";
50 return temBAD_SRC_ACCOUNT;
51 }
52
53 // No point in going any further if the transaction fee is malformed.
54 auto const fee = ctx.tx.getFieldAmount(sfFee);
55 if (!fee.native() || fee != beast::kZero)
56 {
57 JLOG(ctx.j.warn()) << "Change: invalid fee";
58 return temBAD_FEE;
59 }
60
61 if (!ctx.tx.getSigningPubKey().empty() || !ctx.tx.getSignature().empty() ||
62 ctx.tx.isFieldPresent(sfSigners))
63 {
64 JLOG(ctx.j.warn()) << "Change: Bad signature";
65 return temBAD_SIGNATURE;
66 }
67
68 if (ctx.tx.getFieldU32(sfSequence) != 0 || ctx.tx.isFieldPresent(sfPreviousTxnID))
69 {
70 JLOG(ctx.j.warn()) << "Change: Bad sequence";
71 return temBAD_SEQUENCE;
72 }
73
74 return tesSUCCESS;
75}
76
77TER
79{
80 // If tapOPEN_LEDGER is resurrected into ApplyFlags,
81 // this block can be moved to preflight.
82 if (ctx.view.open())
83 {
84 JLOG(ctx.j.warn()) << "Change transaction against open ledger";
85 return temINVALID;
86 }
87
88 switch (ctx.tx.getTxnType())
89 {
90 case ttFEE:
91 // The XRPFees feature has been retired, these fields are required now.
92 if (!ctx.tx.isFieldPresent(sfBaseFeeDrops) ||
93 !ctx.tx.isFieldPresent(sfReserveBaseDrops) ||
94 !ctx.tx.isFieldPresent(sfReserveIncrementDrops))
95 return temMALFORMED;
96 // The XRPFees feature has been retired, these fields are forbidden now.
97 if (ctx.tx.isFieldPresent(sfBaseFee) || ctx.tx.isFieldPresent(sfReferenceFeeUnits) ||
98 ctx.tx.isFieldPresent(sfReserveBase) || ctx.tx.isFieldPresent(sfReserveIncrement))
99 return temMALFORMED;
100 if (ctx.view.rules().enabled(featureSmartEscrow))
101 {
102 if (!ctx.tx.isFieldPresent(sfGasLimit) ||
103 !ctx.tx.isFieldPresent(sfBytecodeSizeLimit) ||
104 !ctx.tx.isFieldPresent(sfGasPrice))
105 return temMALFORMED;
106 if (ctx.tx[sfGasLimit] > kMaxGasLimit ||
107 ctx.tx[sfBytecodeSizeLimit] > kMaxBytecodeSizeLimit ||
108 ctx.tx[sfGasPrice] < kMinGasPrice)
109 return temBAD_FEE;
110 }
111 else
112 {
113 if (ctx.tx.isFieldPresent(sfGasLimit) ||
114 ctx.tx.isFieldPresent(sfBytecodeSizeLimit) || ctx.tx.isFieldPresent(sfGasPrice))
115 return temDISABLED;
116 }
117 return tesSUCCESS;
118 case ttAMENDMENT:
119 case ttUNL_MODIFY:
120 return tesSUCCESS;
121 default:
122 return temUNKNOWN;
123 }
124}
125
126TER
128{
129 switch (ctx_.tx.getTxnType())
130 {
131 case ttAMENDMENT:
132 return applyAmendment();
133 case ttFEE:
134 return applyFee();
135 case ttUNL_MODIFY:
136 return applyUNLModify();
137 // LCOV_EXCL_START
138 default:
139 UNREACHABLE("xrpl::Change::doApply : invalid transaction type");
140 return tefFAILURE;
141 // LCOV_EXCL_STOP
142 }
143}
144
145void
147{
148 XRPL_ASSERT(accountID_ == beast::kZero, "xrpl::Change::preCompute : zero account");
149}
150
151void
153{
154 FeeSettingsEntryW feeObject(view());
155
156 if (!feeObject)
157 {
158 feeObject.newSLE();
159 feeObject.insert();
160 }
161
162 // Compile-time constants, never `FeeSetup`: every node applies this
163 // pseudo-transaction, so reading local config here would diverge.
164 feeObject->at(sfGasLimit) = kDefaultGasLimit;
165 feeObject->at(sfBytecodeSizeLimit) = kDefaultBytecodeSizeLimit;
166 feeObject->at(sfGasPrice) = kDefaultGasPrice;
167
168 feeObject.update();
169
170 JLOG(j_.info()) << "Feature Extension fees initialized on SmartEscrow activation";
171}
172
173TER
175{
176 UInt256 const amendment(ctx_.tx.getFieldH256(sfAmendment));
177
178 AmendmentsEntryW amendmentObject(view(), j_);
179
180 if (!amendmentObject)
181 {
182 amendmentObject.newSLE();
183 amendmentObject.insert();
184 }
185
186 STVector256 amendments = amendmentObject->getFieldV256(sfAmendments);
187
188 if (std::ranges::find(amendments, amendment) != amendments.end())
189 return tefALREADY;
190
191 bool const gotMajority = ctx_.tx.isFlag(tfGotMajority);
192 bool const lostMajority = ctx_.tx.isFlag(tfLostMajority);
193
194 if (gotMajority && lostMajority)
195 return temINVALID_FLAG;
196
197 STArray newMajorities(sfMajorities);
198
199 bool found = false;
200 if (amendmentObject->isFieldPresent(sfMajorities))
201 {
202 STArray const& oldMajorities = amendmentObject->getFieldArray(sfMajorities);
203 for (auto const& majority : oldMajorities)
204 {
205 if (majority.getFieldH256(sfAmendment) == amendment)
206 {
207 if (gotMajority)
208 return tefALREADY;
209 found = true;
210 }
211 else
212 {
213 // pass through
214 newMajorities.pushBack(majority);
215 }
216 }
217 }
218
219 if (!found && lostMajority)
220 return tefALREADY;
221
222 if (gotMajority)
223 {
224 // This amendment now has a majority
225 newMajorities.pushBack(STObject::makeInnerObject(sfMajority));
226 auto& entry = newMajorities.back();
227 entry[sfAmendment] = amendment;
228 entry[sfCloseTime] = view().parentCloseTime().time_since_epoch().count();
229
230 if (!ctx_.registry.get().getAmendmentTable().isSupported(amendment))
231 {
232 JLOG(j_.warn()) << "Unsupported amendment " << amendment << " received a majority.";
233 }
234 }
235 else if (!lostMajority)
236 {
237 // No flags, enable amendment
238 amendments.pushBack(amendment);
239 amendmentObject->setFieldV256(sfAmendments, amendments);
240
241 ctx_.registry.get().getAmendmentTable().enable(amendment);
242
243 if (!ctx_.registry.get().getAmendmentTable().isSupported(amendment))
244 {
245 JLOG(j_.error()) << "Unsupported amendment " << amendment
246 << " activated: server blocked.";
247 ctx_.registry.get().getOPs().setAmendmentBlocked();
248 }
249
250 if (amendment == featureSmartEscrow)
252 }
253
254 if (newMajorities.empty())
255 {
256 amendmentObject->makeFieldAbsent(sfMajorities);
257 }
258 else
259 {
260 amendmentObject->setFieldArray(sfMajorities, newMajorities);
261 }
262
263 amendmentObject.update();
264
265 return tesSUCCESS;
266}
267
268TER
270{
271 FeeSettingsEntryW feeObject(view());
272
273 if (!feeObject)
274 {
275 feeObject.newSLE();
276 feeObject.insert();
277 }
278 auto const set = [](FeeSettingsEntryW& feeObject, STTx const& tx, auto const&... fields) {
279 ((feeObject->at(fields) = tx[fields]), ...);
280 };
281 set(feeObject, ctx_.tx, sfBaseFeeDrops, sfReserveBaseDrops, sfReserveIncrementDrops);
282 // Ensure the old fields are removed
283 feeObject->makeFieldAbsent(sfBaseFee);
284 feeObject->makeFieldAbsent(sfReferenceFeeUnits);
285 feeObject->makeFieldAbsent(sfReserveBase);
286 feeObject->makeFieldAbsent(sfReserveIncrement);
287 if (view().rules().enabled(featureSmartEscrow))
288 {
289 set(feeObject, ctx_.tx, sfGasLimit, sfBytecodeSizeLimit, sfGasPrice);
290 }
291
292 feeObject.update();
293
294 JLOG(j_.warn()) << "Fees have been changed";
295 return tesSUCCESS;
296}
297
298TER
300{
301 if (!isFlagLedger(view().seq()))
302 {
303 JLOG(j_.warn()) << "N-UNL: applyUNLModify, not a flag ledger, seq=" << view().seq();
304 return tefFAILURE;
305 }
306
307 if (!ctx_.tx.isFieldPresent(sfUNLModifyDisabling) ||
308 ctx_.tx.getFieldU8(sfUNLModifyDisabling) > 1 || !ctx_.tx.isFieldPresent(sfLedgerSequence) ||
309 !ctx_.tx.isFieldPresent(sfUNLModifyValidator))
310 {
311 JLOG(j_.warn()) << "N-UNL: applyUNLModify, wrong Tx format.";
312 return tefFAILURE;
313 }
314
315 bool const disabling = ctx_.tx.getFieldU8(sfUNLModifyDisabling) != 0u;
316 auto const seq = ctx_.tx.getFieldU32(sfLedgerSequence);
317 if (seq != view().seq())
318 {
319 JLOG(j_.warn()) << "N-UNL: applyUNLModify, wrong ledger seq=" << seq;
320 return tefFAILURE;
321 }
322
323 Blob const validator = ctx_.tx.getFieldVL(sfUNLModifyValidator);
324 if (!publicKeyType(makeSlice(validator)))
325 {
326 JLOG(j_.warn()) << "N-UNL: applyUNLModify, bad validator key";
327 return tefFAILURE;
328 }
329
330 JLOG(j_.info()) << "N-UNL: applyUNLModify, " << (disabling ? "ToDisable" : "ToReEnable")
331 << " seq=" << seq << " validator data:" << strHex(validator);
332
333 NegativeUNLEntryW negUnlObject(view(), j_);
334 if (!negUnlObject)
335 {
336 negUnlObject.newSLE();
337 negUnlObject.insert();
338 }
339
340 bool const found = [&] {
341 if (negUnlObject->isFieldPresent(sfDisabledValidators))
342 {
343 auto const& negUnl = negUnlObject->getFieldArray(sfDisabledValidators);
344 for (auto const& v : negUnl)
345 {
346 if (v.isFieldPresent(sfPublicKey) && v.getFieldVL(sfPublicKey) == validator)
347 return true;
348 }
349 }
350 return false;
351 }();
352
353 if (disabling)
354 {
355 // cannot have more than one toDisable
356 if (negUnlObject->isFieldPresent(sfValidatorToDisable))
357 {
358 JLOG(j_.warn()) << "N-UNL: applyUNLModify, already has ToDisable";
359 return tefFAILURE;
360 }
361
362 // cannot be the same as toReEnable
363 if (negUnlObject->isFieldPresent(sfValidatorToReEnable))
364 {
365 if (negUnlObject->getFieldVL(sfValidatorToReEnable) == validator)
366 {
367 JLOG(j_.warn()) << "N-UNL: applyUNLModify, ToDisable is same as ToReEnable";
368 return tefFAILURE;
369 }
370 }
371
372 // cannot be in negative UNL already
373 if (found)
374 {
375 JLOG(j_.warn()) << "N-UNL: applyUNLModify, ToDisable already in negative UNL";
376 return tefFAILURE;
377 }
378
379 negUnlObject->setFieldVL(sfValidatorToDisable, validator);
380 }
381 else
382 {
383 // cannot have more than one toReEnable
384 if (negUnlObject->isFieldPresent(sfValidatorToReEnable))
385 {
386 JLOG(j_.warn()) << "N-UNL: applyUNLModify, already has ToReEnable";
387 return tefFAILURE;
388 }
389
390 // cannot be the same as toDisable
391 if (negUnlObject->isFieldPresent(sfValidatorToDisable))
392 {
393 if (negUnlObject->getFieldVL(sfValidatorToDisable) == validator)
394 {
395 JLOG(j_.warn()) << "N-UNL: applyUNLModify, ToReEnable is same as ToDisable";
396 return tefFAILURE;
397 }
398 }
399
400 // must be in negative UNL
401 if (!found)
402 {
403 JLOG(j_.warn()) << "N-UNL: applyUNLModify, ToReEnable is not in negative UNL";
404 return tefFAILURE;
405 }
406
407 negUnlObject->setFieldVL(sfValidatorToReEnable, validator);
408 }
409
410 negUnlObject.update();
411 return tesSUCCESS;
412}
413
414void
416{
417 // No transaction-specific invariants yet (future work).
418}
419
420bool
422{
423 // No transaction-specific invariants yet (future work).
424 return true;
425}
426
427} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream warn() const
Definition Journal.h:356
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
Definition Change.cpp:421
static TER preclaim(PreclaimContext const &ctx)
Definition Change.cpp:78
TER applyUNLModify()
Definition Change.cpp:299
TER doApply() override
Definition Change.cpp:127
void initializeVMFees()
Write the default Feature Extension fee settings into FeeSettings on featureSmartEscrow activation.
Definition Change.cpp:152
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
Definition Change.cpp:415
TER applyAmendment()
Definition Change.cpp:174
void preCompute() override
Definition Change.cpp:146
TER applyFee()
Definition Change.cpp:269
A view into a ledger.
Definition ReadView.h:41
virtual Rules const & rules() const =0
Returns the tx processing rules.
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition ReadView.h:106
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:115
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
bool empty() const
Definition STArray.h:254
void pushBack(STObject const &object)
Definition STArray.h:212
STObject & back()
Definition STArray.h:193
std::shared_ptr< STLedgerEntry const > const & ConstRef
T::value_type at(TypedField< T > const &f) const
Get the value of a field.
Definition STObject.h:1077
Blob getFieldVL(SField const &field) const
Definition STObject.cpp:649
void setFieldV256(SField const &field, STVector256 const &v)
Definition STObject.cpp:779
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:601
void setFieldVL(SField const &field, Blob const &)
Definition STObject.cpp:791
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:688
void setFieldArray(SField const &field, STArray const &v)
Definition STObject.cpp:833
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:79
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:643
STVector256 const & getFieldV256(SField const &field) const
Definition STObject.cpp:671
void makeFieldAbsent(SField const &field)
Definition STObject.cpp:550
STAmount const & getFieldAmount(SField const &field) const
Definition STObject.cpp:657
static Blob getSignature(STObject const &sigObject)
Definition STTx.cpp:185
TxType getTxnType() const
Definition STTx.h:250
Blob getSigningPubKey() const
Definition STTx.h:256
static NotTEC invokePreflight(PreflightContext const &ctx)
Definition Transactor.h:664
beast::Journal const j_
Definition Transactor.h:164
ApplyView & view()
Definition Transactor.h:184
AccountID const accountID_
Definition Transactor.h:166
ApplyContext & ctx_
Definition Transactor.h:162
T empty(T... args)
T find(T... args)
constexpr Zero kZero
Definition Zero.h:30
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,...
constexpr std::uint32_t kDefaultGasLimit
bool isFlagLedger(LedgerIndex seq)
Returns true if the given ledgerIndex is a flag ledgerIndex.
Definition Protocol.cpp:11
constexpr std::uint32_t kMinGasPrice
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
@ tefALREADY
Definition TER.h:161
@ tefFAILURE
Definition TER.h:160
NegativeUNLEntry< ApplyView > NegativeUNLEntryW
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
BaseUInt< 256 > UInt256
Definition base_uint.h:581
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
constexpr std::uint32_t kDefaultGasPrice
constexpr std::uint32_t kDefaultBytecodeSizeLimit
FeeSettingsEntry< ApplyView > FeeSettingsEntryW
@ temBAD_FEE
Definition TER.h:80
@ temINVALID
Definition TER.h:98
@ temBAD_SEQUENCE
Definition TER.h:92
@ temINVALID_FLAG
Definition TER.h:99
@ temBAD_SRC_ACCOUNT
Definition TER.h:94
@ temMALFORMED
Definition TER.h:75
@ temDISABLED
Definition TER.h:102
@ temUNKNOWN
Definition TER.h:112
@ temBAD_SIGNATURE
Definition TER.h:93
TERSubset< CanCvtToTER > TER
Definition TER.h:657
NotTEC preflight0(PreflightContext const &ctx, std::uint32_t flagMask)
Performs early sanity checks on the txid and flags.
constexpr std::uint32_t kMaxGasLimit
Hard protocol bounds on the Feature Extension fee settings.
constexpr std::uint32_t kMaxBytecodeSizeLimit
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
@ tesSUCCESS
Definition TER.h:253
AmendmentsEntry< ApplyView > AmendmentsEntryW
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:92
ReadView const & view
Definition Transactor.h:95
beast::Journal const j
Definition Transactor.h:100
State information when preflighting a tx.
Definition Transactor.h:39
beast::Journal const j
Definition Transactor.h:46
T time_since_epoch(T... args)