xrpld
Loading...
Searching...
No Matches
TrustSet.cpp
1#include <xrpl/tx/transactors/token/TrustSet.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/core/ServiceRegistry.h>
7#include <xrpl/ledger/ReadView.h>
8#include <xrpl/ledger/helpers/AccountRootHelpers.h>
9#include <xrpl/ledger/helpers/RippleStateHelpers.h>
10#include <xrpl/ledger/helpers/SponsorHelpers.h>
11#include <xrpl/protocol/AMMCore.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/Feature.h>
14#include <xrpl/protocol/Indexes.h>
15#include <xrpl/protocol/LedgerFormats.h>
16#include <xrpl/protocol/Permissions.h>
17#include <xrpl/protocol/Quality.h>
18#include <xrpl/protocol/SField.h>
19#include <xrpl/protocol/STAmount.h>
20#include <xrpl/protocol/STLedgerEntry.h>
21#include <xrpl/protocol/STTx.h>
22#include <xrpl/protocol/TER.h>
23#include <xrpl/protocol/TxFlags.h>
24#include <xrpl/protocol/UintTypes.h>
25#include <xrpl/protocol/XRPAmount.h>
26#include <xrpl/tx/Transactor.h>
27
28#include <cstdint>
29#include <unordered_set>
30
31namespace {
32
33uint32_t
34computeFreezeFlags(
35 uint32_t uFlags,
36 bool bHigh,
37 bool bNoFreeze,
38 bool bSetFreeze,
39 bool bClearFreeze,
40 bool bSetDeepFreeze,
41 bool bClearDeepFreeze)
42{
43 if (bSetFreeze && !bClearFreeze && !bNoFreeze)
44 {
45 uFlags |= (bHigh ? xrpl::lsfHighFreeze : xrpl::lsfLowFreeze);
46 }
47 else if (bClearFreeze && !bSetFreeze)
48 {
49 uFlags &= ~(bHigh ? xrpl::lsfHighFreeze : xrpl::lsfLowFreeze);
50 }
51 if (bSetDeepFreeze && !bClearDeepFreeze && !bNoFreeze)
52 {
53 uFlags |= (bHigh ? xrpl::lsfHighDeepFreeze : xrpl::lsfLowDeepFreeze);
54 }
55 else if (bClearDeepFreeze && !bSetDeepFreeze)
56 {
57 uFlags &= ~(bHigh ? xrpl::lsfHighDeepFreeze : xrpl::lsfLowDeepFreeze);
58 }
59
60 return uFlags;
61}
62
63} // namespace
64
65namespace xrpl {
66
67std::uint32_t
69{
70 return tfTrustSetMask;
71}
72
75{
76 auto& tx = ctx.tx;
77 auto& j = ctx.j;
78
79 if (!ctx.rules.enabled(featureDeepFreeze))
80 {
81 // Even though the deep freeze flags are included in the
82 // `tfTrustSetMask`, they are not valid if the amendment is not enabled.
83 if ((tx.getFlags() & (tfSetDeepFreeze | tfClearDeepFreeze)) != 0u)
84 {
85 return temINVALID_FLAG;
86 }
87 }
88
89 STAmount const saLimitAmount(tx.getFieldAmount(sfLimitAmount));
90
91 if (!isLegalNet(saLimitAmount))
92 return temBAD_AMOUNT;
93
94 if (saLimitAmount.native())
95 {
96 JLOG(j.trace()) << "Malformed transaction: specifies native limit "
97 << saLimitAmount.getFullText();
98 return temBAD_LIMIT;
99 }
100
101 if (badCurrency() == saLimitAmount.get<Issue>().currency)
102 {
103 JLOG(j.trace()) << "Malformed transaction: specifies XRP as IOU";
104 return temBAD_CURRENCY;
105 }
106
107 if (saLimitAmount < beast::kZero)
108 {
109 JLOG(j.trace()) << "Malformed transaction: Negative credit limit.";
110 return temBAD_LIMIT;
111 }
112
113 // Check if destination makes sense.
114 auto const& issuer = saLimitAmount.getIssuer();
115
116 if (!issuer || issuer == noAccount())
117 {
118 JLOG(j.trace()) << "Malformed transaction: no destination account.";
119 return temDST_NEEDED;
120 }
121
122 return tesSUCCESS;
123}
124
125NotTEC
127 ReadView const& view,
128 STTx const& tx,
129 std::unordered_set<GranularPermissionType> const& heldGranularPermissions)
130{
131 auto const saLimitAmount = tx.getFieldAmount(sfLimitAmount);
132 auto const sleRippleState = view.read(
134 tx[sfAccount], saLimitAmount.getIssuer(), saLimitAmount.get<Issue>().currency));
135
136 // granular permissions are not allowed to create a trustline
137 if (!sleRippleState)
139
140 // updating LimitAmount is not allowed with granular permissions,
141 // unless there's a new granular permission for this in the future.
142 auto const curLimit = tx[sfAccount] > saLimitAmount.getIssuer()
143 ? sleRippleState->getFieldAmount(sfHighLimit)
144 : sleRippleState->getFieldAmount(sfLowLimit);
145
146 STAmount saLimitAllow = saLimitAmount;
147 saLimitAllow.get<Issue>().account = tx[sfAccount];
148
149 if (curLimit != saLimitAllow)
151
152 return tesSUCCESS;
153}
154
155TER
157{
158 auto const id = ctx.tx[sfAccount];
159
160 auto const sle = ctx.view.read(keylet::account(id));
161 if (!sle)
162 return terNO_ACCOUNT;
163
164 bool const bSetAuth = ctx.tx.isFlag(tfSetfAuth);
165
166 if (bSetAuth && !sle->isFlag(lsfRequireAuth))
167 {
168 JLOG(ctx.j.trace()) << "Retry: Auth not required.";
169 return tefNO_AUTH_REQUIRED;
170 }
171
172 auto const saLimitAmount = ctx.tx[sfLimitAmount];
173
174 auto const currency = saLimitAmount.get<Issue>().currency;
175 auto const uDstAccountID = saLimitAmount.getIssuer();
176
177 if (id == uDstAccountID)
178 return temDST_IS_SRC;
179
180 // This might be nullptr
181 auto const sleDst = ctx.view.read(keylet::account(uDstAccountID));
182 if (sleDst == nullptr)
183 return tecNO_DST;
184
185 // If the destination has opted to disallow incoming trustlines
186 // then honour that flag
187 if (sleDst->isFlag(lsfDisallowIncomingTrustline) &&
188 !ctx.view.exists(keylet::trustLine(id, uDstAccountID, currency)))
189 {
190 return tecNO_PERMISSION;
191 }
192
193 // In general, trust lines to pseudo accounts are not permitted, unless
194 // enabled in the code section below, for specific cases. This block is not
195 // amendment-gated because sleDst will not have a pseudo-account designator
196 // field populated, unless the appropriate amendment was already enabled.
197 if (sleDst && isPseudoAccount(sleDst))
198 {
199 // If destination is AMM and the trustline doesn't exist then only allow
200 // TrustSet if the asset is AMM LP token and AMM is not in empty state.
201 if (sleDst->isFieldPresent(sfAMMID))
202 {
203 if (ctx.view.exists(keylet::trustLine(id, uDstAccountID, currency)))
204 {
205 // pass
206 }
207 else if (auto const ammSle = ctx.view.read({ltAMM, sleDst->getFieldH256(sfAMMID)}))
208 {
209 auto const lpTokens = ammSle->getFieldAmount(sfLPTokenBalance);
210 if (lpTokens == beast::kZero)
211 {
212 return tecAMM_EMPTY;
213 }
214 if (lpTokens.get<Issue>().currency != saLimitAmount.get<Issue>().currency)
215 {
216 return tecNO_PERMISSION;
217 }
218 }
219 else
220 {
221 return tecINTERNAL; // LCOV_EXCL_LINE
222 }
223 }
224 else if (sleDst->isFieldPresent(sfVaultID) || sleDst->isFieldPresent(sfLoanBrokerID))
225 {
226 if (!ctx.view.exists(keylet::trustLine(id, uDstAccountID, currency)))
227 return tecNO_PERMISSION;
228 // else pass
229 }
230 else
231 {
232 return tecPSEUDO_ACCOUNT;
233 }
234 }
235
236 // Checking all freeze/deep freeze flag invariants.
237 if (ctx.view.rules().enabled(featureDeepFreeze))
238 {
239 bool const bNoFreeze = sle->isFlag(lsfNoFreeze);
240 bool const bSetFreeze = ctx.tx.isFlag(tfSetFreeze);
241 bool const bSetDeepFreeze = ctx.tx.isFlag(tfSetDeepFreeze);
242
243 if (bNoFreeze && (bSetFreeze || bSetDeepFreeze))
244 {
245 // Cannot freeze the trust line if NoFreeze is set
246 return tecNO_PERMISSION;
247 }
248
249 bool const bClearFreeze = ctx.tx.isFlag(tfClearFreeze);
250 bool const bClearDeepFreeze = ctx.tx.isFlag(tfClearDeepFreeze);
251 if ((bSetFreeze || bSetDeepFreeze) && (bClearFreeze || bClearDeepFreeze))
252 {
253 // Freezing and unfreezing in the same transaction should be
254 // illegal
255 return tecNO_PERMISSION;
256 }
257
258 bool const bHigh = id > uDstAccountID;
259 // Fetching current state of trust line
260 auto const sleRippleState = ctx.view.read(keylet::trustLine(id, uDstAccountID, currency));
261 std::uint32_t uFlags = sleRippleState ? sleRippleState->getFieldU32(sfFlags) : 0u;
262 // Computing expected trust line state
263 uFlags = computeFreezeFlags(
264 uFlags, bHigh, bNoFreeze, bSetFreeze, bClearFreeze, bSetDeepFreeze, bClearDeepFreeze);
265
266 auto const frozen = uFlags & (bHigh ? lsfHighFreeze : lsfLowFreeze);
267 auto const deepFrozen = uFlags & (bHigh ? lsfHighDeepFreeze : lsfLowDeepFreeze);
268
269 // Trying to set deep freeze on not already frozen trust line must
270 // fail. This also checks that clearing normal freeze while deep
271 // frozen must not work
272 if ((deepFrozen != 0u) && (frozen == 0u))
273 {
274 return tecNO_PERMISSION;
275 }
276 }
277
278 return tesSUCCESS;
279}
280
281TER
283{
284 TER terResult = tesSUCCESS;
285
286 STAmount const saLimitAmount(ctx_.tx.getFieldAmount(sfLimitAmount));
287 bool const bQualityIn(ctx_.tx.isFieldPresent(sfQualityIn));
288 bool const bQualityOut(ctx_.tx.isFieldPresent(sfQualityOut));
289
290 Currency const currency(saLimitAmount.get<Issue>().currency);
291 AccountID const uDstAccountID(saLimitAmount.getIssuer());
292
293 // true, if current is high account.
294 bool const bHigh = accountID_ > uDstAccountID;
295
296 auto const sle = view().peek(keylet::account(accountID_));
297 if (!sle)
298 return tefINTERNAL; // LCOV_EXCL_LINE
299
300 // The reserve that is required to create the line. Note
301 // that although the reserve increases with every item
302 // an account owns, in the case of trust lines we only
303 // *enforce* a reserve if the user owns more than two
304 // items.
305 //
306 // We do this because being able to exchange currencies,
307 // which needs trust lines, is a powerful XRPL feature.
308 // So we want to make it easy for a gateway to fund the
309 // accounts of its users without fear of being tricked.
310 //
311 // Without this logic, a gateway that wanted to have a
312 // new user use its services, would have to give that
313 // user enough XRP to cover not only the account reserve
314 // but the incremental reserve for the trust line as
315 // well. A person with no intention of using the gateway
316 // could use the extra XRP for their own purposes.
317
318 auto const sponsorExp = getTxReserveSponsor(ctx_.getApplyViewContext());
319 if (!sponsorExp)
320 return sponsorExp.error(); // LCOV_EXCL_LINE
321 auto const sponsorSle = *sponsorExp;
322
323 auto const getSponsor = [&sponsorSle, this](AccountID const& account) {
324 return (sponsorSle && account == accountID_) ? sponsorSle : SLE::pointer();
325 };
326
327 // The "free-tier" shortcut (ownerCount < 2) only applies when there is no sponsor.
328 // With any sponsor on the tx, the sponsor must cover the reserve (via balance or
329 // prefunded budget), so the reserve check always runs.
330 bool const freeTrustLine = !sponsorSle && (ownerCount(sle, j_) < 2);
331 std::uint32_t const uOwnerCount = ownerCount(sle, j_);
332 XRPAmount const reserveCreate(
333 (uOwnerCount < 2) ? XRPAmount(beast::kZero)
334 : accountReserve(view(), sle, j_, {.ownerCountDelta = 1}));
335
336 std::uint32_t const uQualityIn(bQualityIn ? ctx_.tx.getFieldU32(sfQualityIn) : 0);
337 std::uint32_t uQualityOut(bQualityOut ? ctx_.tx.getFieldU32(sfQualityOut) : 0);
338
339 if (bQualityOut && QUALITY_ONE == uQualityOut)
340 uQualityOut = 0;
341
342 bool const bSetAuth = ctx_.tx.isFlag(tfSetfAuth);
343 bool const bSetNoRipple = ctx_.tx.isFlag(tfSetNoRipple);
344 bool const bClearNoRipple = ctx_.tx.isFlag(tfClearNoRipple);
345 bool const bSetFreeze = ctx_.tx.isFlag(tfSetFreeze);
346 bool const bClearFreeze = ctx_.tx.isFlag(tfClearFreeze);
347 bool const bSetDeepFreeze = ctx_.tx.isFlag(tfSetDeepFreeze);
348 bool const bClearDeepFreeze = ctx_.tx.isFlag(tfClearDeepFreeze);
349
350 auto const viewJ = ctx_.registry.get().getJournal("View");
351
352 SLE::pointer const sleDst = view().peek(keylet::account(uDstAccountID));
353
354 if (!sleDst)
355 {
356 JLOG(j_.trace()) << "Delay transaction: Destination account does not exist.";
357 return tecNO_DST;
358 }
359
360 STAmount saLimitAllow = saLimitAmount;
361 saLimitAllow.get<Issue>().account = accountID_;
362
363 SLE::pointer const sleRippleState =
364 view().peek(keylet::trustLine(accountID_, uDstAccountID, currency));
365
366 if (sleRippleState)
367 {
368 STAmount saLowBalance;
369 STAmount saLowLimit;
370 STAmount saHighBalance;
371 STAmount saHighLimit;
372 std::uint32_t uLowQualityIn = 0;
373 std::uint32_t uLowQualityOut = 0;
374 std::uint32_t uHighQualityIn = 0;
375 std::uint32_t uHighQualityOut = 0;
376 auto const& uLowAccountID = !bHigh ? accountID_ : uDstAccountID;
377 auto const& uHighAccountID = bHigh ? accountID_ : uDstAccountID;
378 SLE::Ref sleLowAccount = !bHigh ? sle : sleDst;
379 SLE::Ref sleHighAccount = bHigh ? sle : sleDst;
380
381 //
382 // Balances
383 //
384
385 saLowBalance = sleRippleState->getFieldAmount(sfBalance);
386 saHighBalance = -saLowBalance;
387
388 //
389 // Limits
390 //
391
392 sleRippleState->setFieldAmount(!bHigh ? sfLowLimit : sfHighLimit, saLimitAllow);
393
394 saLowLimit = !bHigh ? saLimitAllow : sleRippleState->getFieldAmount(sfLowLimit);
395 saHighLimit = bHigh ? saLimitAllow : sleRippleState->getFieldAmount(sfHighLimit);
396
397 //
398 // Quality in
399 //
400
401 if (!bQualityIn)
402 {
403 // Not setting. Just get it.
404
405 uLowQualityIn = sleRippleState->getFieldU32(sfLowQualityIn);
406 uHighQualityIn = sleRippleState->getFieldU32(sfHighQualityIn);
407 }
408 else if (uQualityIn != 0u)
409 {
410 // Setting.
411
412 sleRippleState->setFieldU32(!bHigh ? sfLowQualityIn : sfHighQualityIn, uQualityIn);
413
414 uLowQualityIn = !bHigh ? uQualityIn : sleRippleState->getFieldU32(sfLowQualityIn);
415 uHighQualityIn = bHigh ? uQualityIn : sleRippleState->getFieldU32(sfHighQualityIn);
416 }
417 else
418 {
419 // Clearing.
420
421 sleRippleState->makeFieldAbsent(!bHigh ? sfLowQualityIn : sfHighQualityIn);
422
423 uLowQualityIn = !bHigh ? 0 : sleRippleState->getFieldU32(sfLowQualityIn);
424 uHighQualityIn = bHigh ? 0 : sleRippleState->getFieldU32(sfHighQualityIn);
425 }
426
427 if (QUALITY_ONE == uLowQualityIn)
428 uLowQualityIn = 0;
429
430 if (QUALITY_ONE == uHighQualityIn)
431 uHighQualityIn = 0;
432
433 //
434 // Quality out
435 //
436
437 if (!bQualityOut)
438 {
439 // Not setting. Just get it.
440
441 uLowQualityOut = sleRippleState->getFieldU32(sfLowQualityOut);
442 uHighQualityOut = sleRippleState->getFieldU32(sfHighQualityOut);
443 }
444 else if (uQualityOut != 0u)
445 {
446 // Setting.
447
448 sleRippleState->setFieldU32(!bHigh ? sfLowQualityOut : sfHighQualityOut, uQualityOut);
449
450 uLowQualityOut = !bHigh ? uQualityOut : sleRippleState->getFieldU32(sfLowQualityOut);
451 uHighQualityOut = bHigh ? uQualityOut : sleRippleState->getFieldU32(sfHighQualityOut);
452 }
453 else
454 {
455 // Clearing.
456
457 sleRippleState->makeFieldAbsent(!bHigh ? sfLowQualityOut : sfHighQualityOut);
458
459 uLowQualityOut = !bHigh ? 0 : sleRippleState->getFieldU32(sfLowQualityOut);
460 uHighQualityOut = bHigh ? 0 : sleRippleState->getFieldU32(sfHighQualityOut);
461 }
462
463 std::uint32_t const uFlagsIn(sleRippleState->getFieldU32(sfFlags));
464 std::uint32_t uFlagsOut(uFlagsIn);
465
466 if (bSetNoRipple && !bClearNoRipple)
467 {
468 if ((bHigh ? saHighBalance : saLowBalance) >= beast::kZero)
469 {
470 uFlagsOut |= (bHigh ? lsfHighNoRipple : lsfLowNoRipple);
471 }
472 else
473 {
474 // Cannot set noRipple on a negative balance.
475 return tecNO_PERMISSION;
476 }
477 }
478 else if (bClearNoRipple && !bSetNoRipple)
479 {
480 uFlagsOut &= ~(bHigh ? lsfHighNoRipple : lsfLowNoRipple);
481 }
482
483 // Have to use lsfNoFreeze to maintain pre-deep freeze behavior
484 bool const bNoFreeze = sle->isFlag(lsfNoFreeze);
485 uFlagsOut = computeFreezeFlags(
486 uFlagsOut,
487 bHigh,
488 bNoFreeze,
489 bSetFreeze,
490 bClearFreeze,
491 bSetDeepFreeze,
492 bClearDeepFreeze);
493
494 if (QUALITY_ONE == uLowQualityOut)
495 uLowQualityOut = 0;
496
497 if (QUALITY_ONE == uHighQualityOut)
498 uHighQualityOut = 0;
499
500 bool const bLowDefRipple = sleLowAccount->isFlag(lsfDefaultRipple);
501 bool const bHighDefRipple = sleHighAccount->isFlag(lsfDefaultRipple);
502
503 bool const bLowReserveSet = (uLowQualityIn != 0u) || (uLowQualityOut != 0u) ||
504 ((uFlagsOut & lsfLowNoRipple) == 0) != bLowDefRipple ||
505 ((uFlagsOut & lsfLowFreeze) != 0u) || saLowLimit || saLowBalance > beast::kZero;
506 bool const bLowReserveClear = !bLowReserveSet;
507
508 bool const bHighReserveSet = (uHighQualityIn != 0u) || (uHighQualityOut != 0u) ||
509 ((uFlagsOut & lsfHighNoRipple) == 0) != bHighDefRipple ||
510 ((uFlagsOut & lsfHighFreeze) != 0u) || saHighLimit || saHighBalance > beast::kZero;
511 bool const bHighReserveClear = !bHighReserveSet;
512
513 bool const bDefault = bLowReserveClear && bHighReserveClear;
514
515 bool const bLowReserved = sleRippleState->isFlag(lsfLowReserve);
516 bool const bHighReserved = sleRippleState->isFlag(lsfHighReserve);
517
518 bool bReserveIncrease = false;
519
520 auto const currentHighSponsor =
521 getLedgerEntryReserveSponsor(view(), sleRippleState, sfHighSponsor);
522 auto const currentLowSponsor =
523 getLedgerEntryReserveSponsor(view(), sleRippleState, sfLowSponsor);
524
525 if (bSetAuth)
526 {
527 uFlagsOut |= (bHigh ? lsfHighAuth : lsfLowAuth);
528 }
529
530 if (bLowReserveSet && !bLowReserved)
531 {
532 SLE::pointer const lowSponsor = getSponsor(uLowAccountID);
533
534 if (view().rules().enabled(featureSponsor))
535 {
536 if (auto const ret = checkReserve(
537 ctx_.getApplyViewContext(),
538 sleLowAccount,
540 lowSponsor,
541 {.ownerCountDelta = 1},
542 j_,
544 lowSponsor && !isTesSuccess(ret))
545 {
546 return ret;
547 }
548 }
549
550 // Set reserve for low account.
551 increaseOwnerCount(view(), sleLowAccount, lowSponsor, 1, viewJ);
552 uFlagsOut |= lsfLowReserve;
553
554 addSponsorToLedgerEntry(sleRippleState, lowSponsor, sfLowSponsor);
555
556 if (!bHigh)
557 bReserveIncrease = true;
558 }
559
560 if (bLowReserveClear && bLowReserved)
561 {
562 // Clear reserve for low account.
563 decreaseOwnerCount(view(), sleLowAccount, currentLowSponsor, 1, viewJ);
564 uFlagsOut &= ~lsfLowReserve;
565
566 removeSponsorFromLedgerEntry(sleRippleState, sfLowSponsor);
567 }
568
569 if (bHighReserveSet && !bHighReserved)
570 {
571 SLE::pointer const highSponsor = getSponsor(uHighAccountID);
572
573 // should be checked PreFunded Sponsor before increaseOwnerCount()
574 // For PreFunded sponsors, we need to check if there are sufficient reserves before
575 // calling increaseOwnerCount().
576 if (view().rules().enabled(featureSponsor))
577 {
578 if (auto const ret = checkReserve(
579 ctx_.getApplyViewContext(),
580 sleHighAccount,
582 highSponsor,
583 {.ownerCountDelta = 1},
584 j_,
586 highSponsor && !isTesSuccess(ret))
587 {
588 return ret;
589 }
590 }
591
592 // Set reserve for high account.
593 increaseOwnerCount(view(), sleHighAccount, highSponsor, 1, viewJ);
594 uFlagsOut |= lsfHighReserve;
595
596 addSponsorToLedgerEntry(sleRippleState, highSponsor, sfHighSponsor);
597
598 if (bHigh)
599 bReserveIncrease = true;
600 }
601
602 if (bHighReserveClear && bHighReserved)
603 {
604 // Clear reserve for high account.
605 decreaseOwnerCount(view(), sleHighAccount, currentHighSponsor, 1, viewJ);
606 uFlagsOut &= ~lsfHighReserve;
607
608 removeSponsorFromLedgerEntry(sleRippleState, sfHighSponsor);
609 }
610
611 if (uFlagsIn != uFlagsOut)
612 sleRippleState->setFieldU32(sfFlags, uFlagsOut);
613
614 if (view().rules().enabled(featureSponsor))
615 {
616 if (bDefault || badCurrency() == currency)
617 {
618 // Delete.
619
620 terResult =
621 trustDelete(view(), sleRippleState, uLowAccountID, uHighAccountID, viewJ);
622 }
623 // Reserve is not scaled by load
624 else if (
625 auto const ret = checkReserve(
626 ctx_.getApplyViewContext(),
627 sle,
629 sponsorSle,
630 // NOLINTNEXTLINE(readability-trailing-comma) fix-it drops comma after `{}`
631 {},
632 j_,
634 !freeTrustLine && bReserveIncrease && !isTesSuccess(ret))
635 {
636 JLOG(j_.trace()) << "Delay transaction: Insufficent reserve to "
637 "add trust line.";
638
639 // Another transaction could provide XRP to the account and then
640 // this transaction would succeed.
641 terResult = ret;
642 }
643 else
644 {
645 view().update(sleRippleState);
646
647 JLOG(j_.trace()) << "Modify ripple line";
648 }
649 }
650 else
651 {
652 if (bDefault || badCurrency() == currency)
653 {
654 // Delete.
655
656 terResult =
657 trustDelete(view(), sleRippleState, uLowAccountID, uHighAccountID, viewJ);
658 }
659 // Reserve is not scaled by load.
660 else if (bReserveIncrease && preFeeBalance_ < reserveCreate)
661 {
662 JLOG(j_.trace()) << "Delay transaction: Insufficent reserve to "
663 "add trust line.";
664
665 // Another transaction could provide XRP to the account and then
666 // this transaction would succeed.
667 terResult = tecINSUF_RESERVE_LINE;
668 }
669 else
670 {
671 view().update(sleRippleState);
672
673 JLOG(j_.trace()) << "Modify ripple line";
674 }
675 }
676 }
677 // Line does not exist.
678 else if (
679 !saLimitAmount && // Setting default limit.
680 (!bQualityIn || (uQualityIn == 0u)) && // Not setting quality in or
681 // setting default quality in.
682 (!bQualityOut || (uQualityOut == 0u)) && // Not setting quality out or
683 // setting default quality out.
684 (!bSetAuth))
685 {
686 JLOG(j_.trace()) << "Redundant: Setting non-existent ripple line to defaults.";
688 }
689 // reserve is not scaled by load
690 else if (!view().rules().enabled(featureSponsor) && preFeeBalance_ < reserveCreate)
691 {
692 JLOG(j_.trace()) << "Delay transaction: Line does not exist. "
693 "Insufficent reserve to create line.";
694
695 // Another transaction could create the account and then this
696 // transaction would succeed.
697 terResult = tecNO_LINE_INSUF_RESERVE;
698 }
699 else if (
700 auto const ret = checkReserve(
701 ctx_.getApplyViewContext(),
702 sle,
704 sponsorSle,
705 {.ownerCountDelta = 1},
706 j_,
708 view().rules().enabled(featureSponsor) && !freeTrustLine &&
709 !isTesSuccess(ret)) // Reserve is not scaled by load.
710 {
711 JLOG(j_.trace()) << "Delay transaction: Line does not exist. "
712 "Insufficent reserve to create line.";
713
714 // Another transaction could create the account and then this
715 // transaction would succeed.
716 terResult = ret;
717 }
718 else
719 {
720 // Zero balance in currency.
721 STAmount const saBalance(Issue{currency, noAccount()});
722
723 auto const k = keylet::trustLine(accountID_, uDstAccountID, currency);
724
725 JLOG(j_.trace()) << "doTrustSet: Creating ripple line: " << to_string(k.key);
726
727 // Create a new ripple line.
728 terResult = trustCreate(
729 view(),
730 bHigh,
732 uDstAccountID,
733 k.key,
734 sle,
735 bSetAuth,
736 bSetNoRipple && !bClearNoRipple,
737 bSetFreeze && !bClearFreeze,
738 bSetDeepFreeze,
739 saBalance,
740 saLimitAllow, // Limit for who is being charged.
741 uQualityIn,
742 uQualityOut,
743 sponsorSle,
744 viewJ);
745 }
746
747 return terResult;
748}
749
750void
752{
753 // No transaction-specific invariants yet (future work).
754}
755
756bool
758{
759 // No transaction-specific invariants yet (future work).
760 return true;
761}
762
763} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Stream trace() const
Severity stream access functions.
Definition Journal.h:338
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void update(SLE::Ref sle)=0
Indicate changes to a peeked SLE.
A currency issued by an account.
Definition Issue.h:18
Currency currency
Definition Issue.h:20
AccountID const & getIssuer() const
Definition Issue.h:30
A view into a ledger.
Definition ReadView.h:41
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
constexpr TIss const & get() const
std::string getFullText() const override
bool native() const noexcept
Definition STAmount.h:471
AccountID const & getIssuer() const
Definition STAmount.h:516
std::shared_ptr< STLedgerEntry > pointer
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::shared_ptr< STLedgerEntry > const & Ref
bool isFlag(std::uint32_t) const
Definition STObject.cpp:511
STAmount const & getFieldAmount(SField const &field) const
Definition STObject.cpp:657
beast::Journal const j_
Definition Transactor.h:164
ApplyView & view()
Definition Transactor.h:184
AccountID const accountID_
Definition Transactor.h:166
XRPAmount preFeeBalance_
Definition Transactor.h:167
ApplyContext & ctx_
Definition Transactor.h:162
static TER preclaim(PreclaimContext const &ctx)
Definition TrustSet.cpp:156
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
Definition TrustSet.cpp:68
TER doApply() override
Definition TrustSet.cpp:282
static NotTEC checkGranularSemantics(ReadView const &view, STTx const &tx, std::unordered_set< GranularPermissionType > const &heldGranularPermissions)
Definition TrustSet.cpp:126
static NotTEC preflight(PreflightContext const &ctx)
Definition TrustSet.cpp:74
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 TrustSet.cpp:757
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
Definition TrustSet.cpp:751
constexpr Zero kZero
Definition Zero.h:30
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:287
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void removeSponsorFromLedgerEntry(SLE::Ref sle, SF_ACCOUNT const &field=sfSponsor)
Remove the reserve sponsor field from a ledger entry.
@ terNO_DELEGATE_PERMISSION
Definition TER.h:232
@ terNO_ACCOUNT
Definition TER.h:219
std::uint32_t ownerCount(SLE::ConstRef sle, beast::Journal j, std::int32_t ownerCountAdj=0)
Return number of the objects which reserve is covered by the account(sle) (so called "ownercount").
@ tefNO_AUTH_REQUIRED
Definition TER.h:168
@ tefINTERNAL
Definition TER.h:167
bool isLegalNet(STAmount const &value)
Definition STAmount.h:616
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
TER checkReserve(ApplyViewContext ctx, SLE::ConstRef accSle, XRPAmount accBalance, SLE::ConstRef sponsorSle, Adjustment adj, beast::Journal j, TER insufReserveCode=tecINSUFFICIENT_RESERVE)
Check if an account has sufficient reserve.
TER trustCreate(ApplyView &view, bool const bSrcHigh, AccountID const &uSrcAccountID, AccountID const &uDstAccountID, UInt256 const &uIndex, SLE::Ref sleAccount, bool const bAuth, bool const bNoRipple, bool const bFreeze, bool bDeepFreeze, STAmount const &saBalance, STAmount const &saLimit, std::uint32_t uQualityIn, std::uint32_t uQualityOut, SLE::Ref sponsorSle, beast::Journal j)
Create a trust line.
void increaseOwnerCount(ApplyView &view, SLE::Ref accountSle, SLE::Ref sponsorSle, std::uint32_t count, beast::Journal j)
Increase owner-count fields when the caller supplies the sponsor.
void addSponsorToLedgerEntry(SLE::Ref sle, SLE::ConstRef sponsorSle, SF_ACCOUNT const &field=sfSponsor)
Stamp a reserve sponsor onto a ledger entry using an explicit sponsor SLE.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
std::expected< SLE::pointer, TER > getTxReserveSponsor(ApplyViewContext ctx)
Return a mutable SLE for the transaction's reserve sponsor account.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:617
void decreaseOwnerCount(ApplyView &view, SLE::Ref accountSle, SLE::Ref sponsorSle, std::uint32_t count, beast::Journal j)
Decrease owner-count fields when the caller supplies the sponsor.
bool isPseudoAccount(SLE::const_pointer sleAcct)
Returns true if and only if sleAcct is a pseudo-account of any kind (i.e.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
AccountID const & noAccount()
A placeholder for empty accounts.
@ temBAD_CURRENCY
Definition TER.h:78
@ temINVALID_FLAG
Definition TER.h:99
@ temDST_IS_SRC
Definition TER.h:96
@ temDST_NEEDED
Definition TER.h:97
@ temBAD_LIMIT
Definition TER.h:82
@ temBAD_AMOUNT
Definition TER.h:77
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
SLE::pointer getLedgerEntryReserveSponsor(ApplyView &view, SLE::ConstRef sle, SF_ACCOUNT const &field=sfSponsor)
Return a mutable SLE for the reserve sponsor recorded on a ledger entry.
TERSubset< CanCvtToTER > TER
Definition TER.h:657
XRPAmount accountReserve(ReadView const &view, SLE::ConstRef sle, beast::Journal j, Adjustment adj={})
Returns the account reserve, in drops.
@ tecPSEUDO_ACCOUNT
Definition TER.h:373
@ tecAMM_EMPTY
Definition TER.h:343
@ tecNO_LINE_INSUF_RESERVE
Definition TER.h:303
@ tecINSUF_RESERVE_LINE
Definition TER.h:299
@ tecINTERNAL
Definition TER.h:321
@ tecNO_LINE_REDUNDANT
Definition TER.h:304
@ tecNO_PERMISSION
Definition TER.h:316
@ tecNO_DST
Definition TER.h:301
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
TER trustDelete(ApplyView &view, SLE::Ref sleRippleState, AccountID const &uLowAccountID, AccountID const &uHighAccountID, beast::Journal j)
@ tesSUCCESS
Definition TER.h:253
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