xrpld
Loading...
Searching...
No Matches
InvariantCheck.cpp
1#include <xrpl/tx/invariants/InvariantCheck.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/ledger/helpers/AccountRootHelpers.h>
10#include <xrpl/ledger/helpers/TokenHelpers.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/Asset.h>
13#include <xrpl/protocol/Feature.h>
14#include <xrpl/protocol/Indexes.h>
15#include <xrpl/protocol/Issue.h>
16#include <xrpl/protocol/Keylet.h>
17#include <xrpl/protocol/LedgerFormats.h>
18#include <xrpl/protocol/MPTIssue.h>
19#include <xrpl/protocol/Protocol.h>
20#include <xrpl/protocol/Rules.h>
21#include <xrpl/protocol/SField.h>
22#include <xrpl/protocol/STAmount.h>
23#include <xrpl/protocol/STLedgerEntry.h>
24#include <xrpl/protocol/STNumber.h> // IWYU pragma: keep
25#include <xrpl/protocol/STTx.h>
26#include <xrpl/protocol/STVector256.h>
27#include <xrpl/protocol/SystemParameters.h>
28#include <xrpl/protocol/TER.h>
29#include <xrpl/protocol/TxFormats.h>
30#include <xrpl/protocol/TxSettings.h>
31#include <xrpl/protocol/UintTypes.h>
32#include <xrpl/protocol/XRPAmount.h>
33#include <xrpl/tx/invariants/InvariantCheckPrivilege.h>
34
35#include <algorithm>
36#include <cstdint>
37#include <functional>
38#include <memory>
39#include <optional>
40#include <sstream>
41#include <string>
42#include <vector>
43
44namespace xrpl {
45
46#pragma push_macro("UNWRAP")
47#undef UNWRAP
48#pragma push_macro("TRANSACTION")
49#undef TRANSACTION
50
51#define UNWRAP(...) __VA_ARGS__
52#define TRANSACTION(tag, value, name, settings, ...) \
53 case tag: { \
54 return ((TxSettings UNWRAP settings).privileges & priv) != Privilege::NoPriv; \
55 }
56
57bool
58hasPrivilege(STTx const& tx, Privilege priv)
59{
60 switch (tx.getTxnType())
61 {
62#include <xrpl/protocol/detail/transactions.macro>
63
64 // Deprecated types
65 default:
66 return false;
67 }
68};
69
70#undef TRANSACTION
71#pragma pop_macro("TRANSACTION")
72#undef UNWRAP
73#pragma pop_macro("UNWRAP")
74
75// Returns the human-readable name of a ledger entry's type, falling back to
76// the numeric type if the format is somehow unknown.
77static std::string
79{
80 auto const item = LedgerFormats::getInstance().findByType(sle.getType());
81
82 if (item == nullptr)
83 {
84 // LCOV_EXCL_START
85 UNREACHABLE("xrpl::ledgerEntryTypeName : ledger entry has no known ledger format");
86 return std::to_string(sle.getType());
87 // LCOV_EXCL_STOP
88 }
89 return item->getName();
90}
91
92void
97
98bool
100 STTx const& tx,
101 TER const,
102 XRPAmount const fee,
103 ReadView const&,
104 beast::Journal const& j)
105{
106 // We should never charge a negative fee
107 if (fee.drops() < 0)
108 {
109 JLOG(j.fatal()) << "Invariant failed: fee paid was negative: " << fee.drops();
110 return false;
111 }
112
113 // We should never charge a fee that's greater than or equal to the
114 // entire XRP supply.
115 if (fee >= kInitialXrp)
116 {
117 JLOG(j.fatal()) << "Invariant failed: fee paid exceeds system limit: " << fee.drops();
118 return false;
119 }
120
121 // We should never charge more for a transaction than the transaction
122 // authorizes. It's possible to charge less in some circumstances.
123 if (fee > tx.getFieldAmount(sfFee).xrp())
124 {
125 JLOG(j.fatal()) << "Invariant failed: fee paid is " << fee.drops()
126 << " exceeds fee specified in transaction.";
127 return false;
128 }
129
130 return true;
131}
132
133//------------------------------------------------------------------------------
134
135void
137{
138 /* We go through all modified ledger entries, looking only at account roots,
139 * escrow payments, and payment channels. We remove from the total any
140 * previous XRP values and add to the total any new XRP values. The net
141 * balance of a payment channel is computed from two fields (amount and
142 * balance) and deletions are ignored for paychan and escrow because the
143 * amount fields have not been adjusted for those in the case of deletion.
144 */
145 if (before)
146 {
147 switch (before->getType())
148 {
149 case ltACCOUNT_ROOT:
150 drops_ -= (*before)[sfBalance].xrp().drops();
151 break;
152 case ltPAYCHAN:
153 drops_ -= ((*before)[sfAmount] - (*before)[sfBalance]).xrp().drops();
154 break;
155 case ltESCROW:
156 if (isXRP((*before)[sfAmount]))
157 drops_ -= (*before)[sfAmount].xrp().drops();
158 break;
159 case ltSPONSORSHIP:
160 if (before->isFieldPresent(sfFeeAmount))
161 {
162 XRPL_ASSERT(
163 isXRP((*before)[sfFeeAmount]),
164 "XRPNotCreated::visitEntry : Sponsorship.FeeAmount is XRP");
165 drops_ -= (*before)[sfFeeAmount].xrp().drops();
166 }
167 break;
168 default:
169 break;
170 }
171 }
172
173 if (!after)
174 {
175 // LCOV_EXCL_START
176 UNREACHABLE("xrpl::XRPNotCreated::visitEntry : after can't be null");
177 return;
178 // LCOV_EXCL_STOP
179 }
180 switch (after->getType())
181 {
182 case ltACCOUNT_ROOT:
183 drops_ += (*after)[sfBalance].xrp().drops();
184 break;
185 case ltPAYCHAN:
186 if (!isDelete)
187 drops_ += ((*after)[sfAmount] - (*after)[sfBalance]).xrp().drops();
188 break;
189 case ltESCROW:
190 if (!isDelete && isXRP((*after)[sfAmount]))
191 drops_ += (*after)[sfAmount].xrp().drops();
192 break;
193 case ltSPONSORSHIP:
194 if (!isDelete && after->isFieldPresent(sfFeeAmount))
195 {
196 XRPL_ASSERT(
197 isXRP((*after)[sfFeeAmount]),
198 "XRPNotCreated::visitEntry : Sponsorship.FeeAmount is XRP");
199 drops_ += (*after)[sfFeeAmount].xrp().drops();
200 }
201 break;
202 default:
203 break;
204 }
205}
206
207bool
209 STTx const& tx,
210 TER const,
211 XRPAmount const fee,
212 ReadView const&,
213 beast::Journal const& j) const
214{
215 // drops_ is a full-width running total, so a value that would have wrapped a
216 // 64-bit accumulator is caught here.
217
218 // The net change should never be positive, as this would mean that the
219 // transaction created XRP out of thin air. That's not possible.
220 if (drops_ > 0)
221 {
222 JLOG(j.fatal()) << "Invariant failed: XRP net change was positive: " << drops_;
223 return false;
224 }
225
226 // The negative of the net change should be equal to actual fee charged.
227 if (-drops_ != fee.drops())
228 {
229 JLOG(j.fatal()) << "Invariant failed: XRP net change of " << drops_ << " doesn't match fee "
230 << fee.drops();
231 return false;
232 }
233
234 return true;
235}
236
237//------------------------------------------------------------------------------
238
239void
241{
242 auto const isBad = [](STAmount const& balance) {
243 if (!balance.native())
244 return true;
245
246 auto const drops = balance.xrp();
247
248 // Can't have more than the number of drops instantiated
249 // in the genesis ledger.
250 if (drops > kInitialXrp)
251 return true;
252
253 // Can't have a negative balance (0 is OK)
254 if (drops < XRPAmount{0})
255 return true;
256
257 return false;
258 };
259
260 if (before && before->getType() == ltACCOUNT_ROOT)
261 bad_ |= isBad((*before)[sfBalance]);
262
263 if (after && after->getType() == ltACCOUNT_ROOT)
264 bad_ |= isBad((*after)[sfBalance]);
265}
266
267bool
269 STTx const&,
270 TER const,
271 XRPAmount const,
272 ReadView const&,
273 beast::Journal const& j) const
274{
275 if (bad_)
276 {
277 JLOG(j.fatal()) << "Invariant failed: incorrect account XRP balance";
278 return false;
279 }
280
281 return true;
282}
283
284//------------------------------------------------------------------------------
285
286void
288{
289 auto const isBad = [](STAmount const& pays, STAmount const& gets) {
290 // An offer should never be negative
291 if (pays < beast::kZero)
292 return true;
293
294 if (gets < beast::kZero)
295 return true;
296
297 // Can't have an XRP to XRP offer:
298 return pays.native() && gets.native();
299 };
300
301 if (before && before->getType() == ltOFFER)
302 bad_ |= isBad((*before)[sfTakerPays], (*before)[sfTakerGets]);
303
304 if (after && after->getType() == ltOFFER)
305 bad_ |= isBad((*after)[sfTakerPays], (*after)[sfTakerGets]);
306}
307
308bool
310 STTx const&,
311 TER const,
312 XRPAmount const,
313 ReadView const&,
314 beast::Journal const& j) const
315{
316 if (bad_)
317 {
318 JLOG(j.fatal()) << "Invariant failed: offer with a bad amount";
319 return false;
320 }
321
322 return true;
323}
324
325//------------------------------------------------------------------------------
326
327void
329{
330 auto const isBad = [](STAmount const& amount) {
331 // XRP case
332 if (amount.native())
333 {
334 if (amount.xrp() <= XRPAmount{0})
335 return true;
336
337 if (amount.xrp() >= kInitialXrp)
338 return true;
339 }
340 else
341 {
342 return amount.asset().visit(
343 [&](Issue const& issue) {
344 // IOU case
345 if (amount <= beast::kZero)
346 return true;
347
348 if (badCurrency() == issue.currency)
349 return true;
350
351 return false;
352 }
353
354 // MPT case
355 ,
356 [&](MPTIssue const&) {
357 if (amount <= beast::kZero)
358 return true;
359
360 if (amount.mpt() > MPTAmount{kMaxMpTokenAmount})
361 return true; // LCOV_EXCL_LINE
362
363 return false;
364 });
365 }
366 return false;
367 };
368
369 if (before && before->getType() == ltESCROW)
370 bad_ |= isBad((*before)[sfAmount]);
371
372 if (after && after->getType() == ltESCROW)
373 bad_ |= isBad((*after)[sfAmount]);
374
375 auto const checkAmount = [this](std::int64_t amount) {
376 if (amount > kMaxMpTokenAmount || amount < 0)
377 bad_ |= true;
378 };
379
380 bool const overwriteFixEnabled = isFeatureEnabled(fixCleanup3_1_3, true);
381
382 if (after && after->getType() == ltMPTOKEN_ISSUANCE)
383 {
384 auto const outstanding = (*after)[sfOutstandingAmount];
385 checkAmount(outstanding);
386 if (auto const locked = (*after)[~sfLockedAmount])
387 {
388 checkAmount(*locked);
389 bool const isBad = outstanding < *locked;
390 if (overwriteFixEnabled)
391 {
392 bad_ |= isBad;
393 }
394 else
395 {
396 bad_ = isBad;
397 }
398 }
399 }
400
401 if (after && after->getType() == ltMPTOKEN)
402 {
403 auto const mptAmount = (*after)[sfMPTAmount];
404 checkAmount(mptAmount);
405 if (auto const locked = (*after)[~sfLockedAmount])
406 {
407 checkAmount(*locked);
408 }
409 }
410}
411
412bool
414 STTx const& txn,
415 TER const,
416 XRPAmount const,
417 ReadView const&,
418 beast::Journal const& j) const
419{
420 if (bad_)
421 {
422 JLOG(j.fatal()) << "Invariant failed: escrow specifies invalid amount";
423 return false;
424 }
425
426 return true;
427}
428
429//------------------------------------------------------------------------------
430
431void
433{
434 if (isDelete && before && before->getType() == ltACCOUNT_ROOT)
436}
437
438bool
440 STTx const& tx,
441 TER const result,
442 XRPAmount const,
443 ReadView const&,
444 beast::Journal const& j) const
445{
446 // AMM account root can be deleted as the result of AMM withdraw/delete
447 // transaction when the total AMM LP Tokens balance goes to 0.
448 // A successful AccountDelete or AMMDelete MUST delete exactly
449 // one account root.
451 {
452 if (accountsDeleted_ == 1)
453 return true;
454
455 if (accountsDeleted_ == 0)
456 {
457 JLOG(j.fatal()) << "Invariant failed: account deletion "
458 "succeeded without deleting an account";
459 }
460 else
461 {
462 JLOG(j.fatal()) << "Invariant failed: account deletion "
463 "succeeded but deleted multiple accounts!";
464 }
465 return false;
466 }
467
468 // A successful AMMWithdraw/AMMClawback MAY delete one account root
469 // when the total AMM LP Tokens balance goes to 0. Not every AMM withdraw
470 // deletes the AMM account, accountsDeleted_ is set if it is deleted.
472 return true;
473
474 if (accountsDeleted_ == 0)
475 return true;
476
477 JLOG(j.fatal()) << "Invariant failed: an account root was deleted";
478 return false;
479}
480
481//------------------------------------------------------------------------------
482
483void
485{
486 if (isDelete && before && before->getType() == ltACCOUNT_ROOT)
487 accountsDeleted_.emplace_back(before, after);
488}
489
490bool
492 STTx const& tx,
493 TER const result,
494 XRPAmount const,
495 ReadView const& view,
496 beast::Journal const& j)
497{
498 // Always check for objects in the ledger, but to prevent differing
499 // transaction processing results, however unlikely, only fail if the
500 // feature is enabled. Enabled, or not, though, a fatal-level message will
501 // be logged
502 [[maybe_unused]] bool const enforce = view.rules().enabled(fixCleanup3_2_0) ||
503 view.rules().enabled(featureSponsor) || view.rules().enabled(featureSingleAssetVault) ||
504 view.rules().enabled(featureLendingProtocol);
505
506 auto const objectExists = [&view, enforce, &j](auto const& keylet) {
507 (void)enforce;
508 if (auto const sle = view.read(keylet))
509 {
510 // Finding the object is bad
511 JLOG(j.fatal()) << "Invariant failed: account deletion left behind a "
512 << ledgerEntryTypeName(*sle) << " object";
513 // The comment above starting with "assert(enforce)" explains this
514 // assert.
515 XRPL_ASSERT(
516 enforce,
517 "xrpl::AccountRootsDeletedClean::finalize::objectExists : "
518 "account deletion left no objects behind");
519 return true;
520 }
521 return false;
522 };
523
524 for (auto const& [before, after] : accountsDeleted_)
525 {
526 auto const accountID = before->getAccountID(sfAccount);
527 // An account should not be deleted with a balance
528 if (after->at(sfBalance) != beast::kZero)
529 {
530 JLOG(j.fatal()) << "Invariant failed: account deletion left "
531 "behind a non-zero balance";
532 XRPL_ASSERT(
533 enforce,
534 "xrpl::AccountRootsDeletedClean::finalize : "
535 "deleted account has zero balance");
536 if (enforce)
537 return false;
538 }
539 // An account should not be deleted with a non-zero owner count
540 if (after->at(sfOwnerCount) != 0)
541 {
542 JLOG(j.fatal()) << "Invariant failed: account deletion left "
543 "behind a non-zero owner count";
544 XRPL_ASSERT(
545 enforce,
546 "xrpl::AccountRootsDeletedClean::finalize : "
547 "deleted account has zero owner count");
548 if (enforce)
549 return false;
550 }
551 // An account should not be deleted with sponsorship fields
552 if (after->isFieldPresent(sfSponsoredOwnerCount) ||
553 after->isFieldPresent(sfSponsoringOwnerCount) ||
554 after->isFieldPresent(sfSponsoringAccountCount) || after->isFieldPresent(sfSponsor))
555 {
556 JLOG(j.fatal()) << "Invariant failed: account deletion left "
557 "behind a sponsorship field";
558 XRPL_ASSERT(
559 enforce,
560 "xrpl::AccountRootsDeletedClean::finalize : "
561 "deleted account has no sponsorship fields");
562 if (enforce)
563 return false;
564 }
565 // Simple types
566 for (auto const& keyletDesc : kDirectAccountKeylets)
567 {
568 if (objectExists(std::invoke(keyletDesc.function, accountID)) && enforce)
569 return false;
570 }
571
572 {
573 // NFT pages. nftpage_min and nftpage_max were already explicitly
574 // checked above as entries in directAccountKeylets. This uses
575 // view.succ() to check for any NFT pages in between the two
576 // endpoints.
577 Keylet const first = keylet::nftokenPageMin(accountID);
578 Keylet const last = keylet::nftokenPageMax(accountID);
579
580 std::optional<UInt256> key = view.succ(first.key, last.key.next());
581
582 // current page
583 if (key && objectExists(Keylet{ltNFTOKEN_PAGE, *key}) && enforce)
584 return false;
585 }
586
587 // If the account is a pseudo account, then the linked object must
588 // also be deleted. e.g. AMM, Vault, etc.
589 for (auto const& field : getPseudoAccountFields())
590 {
591 if (before->isFieldPresent(*field))
592 {
593 auto const key = before->getFieldH256(*field);
594 if (objectExists(keylet::unchecked(key)) && enforce)
595 return false;
596 }
597 }
598 }
599
600 return true;
601}
602
603//------------------------------------------------------------------------------
604
605void
607{
608 if (before && after && before->getType() != after->getType())
609 typeMismatch_ = true;
610
611 if (after)
612 {
613#pragma push_macro("LEDGER_ENTRY")
614#undef LEDGER_ENTRY
615
616#define LEDGER_ENTRY(tag, ...) case tag:
617
618 switch (after->getType())
619 {
620#include <xrpl/protocol/detail/ledger_entries.macro>
621
622 break;
623 default:
624 invalidTypeAdded_ = true;
625 break;
626 }
627
628#undef LEDGER_ENTRY
629#pragma pop_macro("LEDGER_ENTRY")
630 }
631}
632
633bool
635 STTx const&,
636 TER const,
637 XRPAmount const,
638 ReadView const&,
639 beast::Journal const& j) const
640{
641 if ((!typeMismatch_) && (!invalidTypeAdded_))
642 return true;
643
644 if (typeMismatch_)
645 {
646 JLOG(j.fatal()) << "Invariant failed: ledger entry type mismatch";
647 }
648
650 {
651 JLOG(j.fatal()) << "Invariant failed: invalid ledger entry type added";
652 }
653
654 return false;
655}
656
657//------------------------------------------------------------------------------
658
659void
661{
662 bool const overwriteFixEnabled = isFeatureEnabled(fixCleanup3_1_3, true);
663
664 if (after && after->getType() == ltRIPPLE_STATE)
665 {
666 // checking the issue directly here instead of
667 // relying on .native() just in case native somehow
668 // were systematically incorrect
669 bool const isXrp = after->getFieldAmount(sfLowLimit).asset() == xrpIssue() ||
670 after->getFieldAmount(sfHighLimit).asset() == xrpIssue();
671 if (overwriteFixEnabled)
672 {
673 xrpTrustLine_ |= isXrp;
674 }
675 else
676 {
677 xrpTrustLine_ = isXrp;
678 }
679 }
680}
681
682bool
684 STTx const&,
685 TER const,
686 XRPAmount const,
687 ReadView const&,
688 beast::Journal const& j) const
689{
690 if (!xrpTrustLine_)
691 return true;
692
693 JLOG(j.fatal()) << "Invariant failed: an XRP trust line was created";
694 return false;
695}
696
697//------------------------------------------------------------------------------
698
699void
701{
702 if (after && after->getType() == ltRIPPLE_STATE)
703 {
704 bool const overwriteFixEnabled = isFeatureEnabled(fixCleanup3_1_3, true);
705
706 bool const lowFreeze = after->isFlag(lsfLowFreeze);
707 bool const lowDeepFreeze = after->isFlag(lsfLowDeepFreeze);
708
709 bool const highFreeze = after->isFlag(lsfHighFreeze);
710 bool const highDeepFreeze = after->isFlag(lsfHighDeepFreeze);
711
712 bool const bad = (lowDeepFreeze && !lowFreeze) || (highDeepFreeze && !highFreeze);
713 if (overwriteFixEnabled)
714 {
716 }
717 else
718 {
720 }
721 }
722}
723
724bool
726 STTx const&,
727 TER const,
728 XRPAmount const,
729 ReadView const&,
730 beast::Journal const& j) const
731{
733 return true;
734
735 JLOG(j.fatal()) << "Invariant failed: a trust line with deep freeze flag "
736 "without normal freeze was created";
737 return false;
738}
739
740//------------------------------------------------------------------------------
741
742void
744{
745 if (!before && after->getType() == ltACCOUNT_ROOT)
746 {
748 accountSeq_ = (*after)[sfSequence];
750 flags_ = after->getFlags();
751 }
752}
753
754bool
756 STTx const& tx,
757 TER const result,
758 XRPAmount const,
759 ReadView const& view,
760 beast::Journal const& j) const
761{
762 if (accountsCreated_ == 0)
763 return true;
764
765 if (accountsCreated_ > 1)
766 {
767 JLOG(j.fatal()) << "Invariant failed: multiple accounts "
768 "created in a single transaction";
769 return false;
770 }
771
772 // From this point on we know exactly one account was created.
774 isTesSuccess(result))
775 {
776 bool const pseudoAccount =
778 (view.rules().enabled(featureSingleAssetVault) ||
779 view.rules().enabled(featureLendingProtocol)));
780
781 if (pseudoAccount && !hasPrivilege(tx, Privilege::CreatePseudoAcct))
782 {
783 JLOG(j.fatal()) << "Invariant failed: pseudo-account created by a "
784 "wrong transaction type";
785 return false;
786 }
787
788 std::uint32_t const startingSeq = pseudoAccount ? 0 : view.seq();
789
790 if (accountSeq_ != startingSeq)
791 {
792 JLOG(j.fatal()) << "Invariant failed: account created with "
793 "wrong starting sequence number";
794 return false;
795 }
796
797 if (pseudoAccount)
798 {
799 std::uint32_t const expected = (lsfDisableMaster | lsfDefaultRipple | lsfDepositAuth);
800 if (flags_ != expected)
801 {
802 JLOG(j.fatal()) << "Invariant failed: pseudo-account created with "
803 "wrong flags";
804 return false;
805 }
806 }
807
808 return true;
809 }
810
811 JLOG(j.fatal()) << "Invariant failed: account root created illegally";
812 return false;
813} // namespace xrpl
814
815//------------------------------------------------------------------------------
816
819 SLE::const_pointer const& sle,
820 AccountID const& holder,
821 AccountID const& issuer,
822 Currency const& currency)
823{
824 if (!sle)
825 return STAmount{Issue{currency, issuer}};
826
827 if (sle->getType() != ltRIPPLE_STATE ||
828 sle->key() != keylet::trustLine(holder, issuer, currency).key)
829 {
830 return std::nullopt;
831 }
832
833 STAmount balance = sle->getFieldAmount(sfBalance);
834 if (holder > issuer)
835 balance.negate();
836 balance.get<Issue>().account = issuer;
837 return balance;
838}
839
840void
842{
843 if (before && before->getType() == ltRIPPLE_STATE)
844 {
846 iou_.before = before;
847 }
848
849 if (!isDelete && after && after->getType() == ltRIPPLE_STATE)
850 iou_.after = after;
851
852 if (before && before->getType() == ltMPTOKEN)
853 {
855 mpt_.before = before;
856 }
857
858 if (!isDelete && after && after->getType() == ltMPTOKEN)
859 mpt_.after = after;
860}
861
862bool
864 STTx const& tx,
865 TER const result,
866 XRPAmount const,
867 ReadView const& view,
868 beast::Journal const& j) const
869{
870 if (tx.getTxnType() != ttCLAWBACK)
871 return true;
872
873 if (isTesSuccess(result))
874 {
875 if (trustlinesChanged_ > 1)
876 {
877 JLOG(j.fatal()) << "Invariant failed: more than one trustline changed.";
878 return false;
879 }
880
881 if (mptokensChanged_ > 1)
882 {
883 JLOG(j.fatal()) << "Invariant failed: more than one mptokens changed.";
884 return false;
885 }
886
887 bool const mptV2Enabled = view.rules().enabled(featureMPTokensV2);
888 if (trustlinesChanged_ != 0 && mptokensChanged_ != 0)
889 {
890 JLOG(j.fatal()) << "Invariant failed: trustline and MPToken both changed.";
891 if (mptV2Enabled)
892 return false;
893 }
894
895 if (trustlinesChanged_ == 1 || (mptV2Enabled && mptokensChanged_ == 1))
896 {
897 STAmount const& amount = tx.getFieldAmount(sfAmount);
898
899 return amount.asset().visit(
900 [&](Issue const& issue) {
901 AccountID const issuer = tx.getAccountID(sfAccount);
902 AccountID const& holder = amount.getIssuer();
903 STAmount const holderBalance = accountHolds(
904 view, holder, issue.currency, issuer, FreezeHandling::IgnoreFreeze, j);
905
906 if (holderBalance.signum() < 0)
907 {
908 JLOG(j.fatal()) << "Invariant failed: trustline or MPT balance is negative";
909 return false;
910 }
911
912 if (!iou_.before)
913 {
914 JLOG(j.fatal())
915 << "Invariant failed: trustline clawback changed the wrong line";
916 return !mptV2Enabled;
917 }
918
919 auto const beforeBalance = clawbackTrustLineBalanceInHolderTerms(
920 iou_.before, holder, issuer, issue.currency);
921 auto const afterBalance = clawbackTrustLineBalanceInHolderTerms(
922 iou_.after, holder, issuer, issue.currency);
923 if (!beforeBalance || !afterBalance)
924 {
925 JLOG(j.fatal())
926 << "Invariant failed: trustline clawback changed the wrong line";
927 return !mptV2Enabled;
928 }
929
930 STAmount clawAmount = amount;
931 clawAmount.get<Issue>().account = issuer;
932 if (clawAmount <= beast::kZero)
933 {
934 JLOG(j.fatal()) << "Invariant failed: trustline clawback amount is invalid";
935 return !mptV2Enabled;
936 }
937
938 // IOU balances keep 16 significant digits, so a clawback
939 // much smaller than the balance changes it by a rounded
940 // amount, possibly zero. Compare with the same rounded
941 // subtraction the balance update performs.
942 if (*afterBalance > *beforeBalance ||
943 *afterBalance != *beforeBalance - std::min(*beforeBalance, clawAmount))
944 {
945 JLOG(j.fatal())
946 << "Invariant failed: trustline clawback balance change is invalid";
947 return !mptV2Enabled;
948 }
949
950 return true;
951 },
952 [&](MPTIssue const& issue) {
953 auto const holder = tx[~sfHolder];
954 if (!holder)
955 {
956 JLOG(j.fatal()) << "Invariant failed: MPT clawback missing holder";
957 return !mptV2Enabled;
958 }
959
960 if (!mpt_.before || !mpt_.after)
961 {
962 JLOG(j.fatal()) << "Invariant failed: MPT clawback token is missing";
963 return !mptV2Enabled;
964 }
965
966 if (mpt_.before->getAccountID(sfAccount) != *holder ||
967 mpt_.after->getAccountID(sfAccount) != *holder ||
968 (*mpt_.before)[sfMPTokenIssuanceID] != issue.getMptID() ||
969 (*mpt_.after)[sfMPTokenIssuanceID] != issue.getMptID())
970 {
971 JLOG(j.fatal()) << "Invariant failed: MPT clawback changed the wrong token";
972 return !mptV2Enabled;
973 }
974
975 auto const before = mpt_.before->getFieldU64(sfMPTAmount);
976 auto const after = mpt_.after->getFieldU64(sfMPTAmount);
977 if (amount.negative() || amount.mantissa() == 0)
978 {
979 JLOG(j.fatal()) << "Invariant failed: MPT clawback amount is invalid";
980 return !mptV2Enabled;
981 }
982 auto const clawAmount = amount.mantissa();
983
984 // MPT balances are unsigned, so validate the raw holder
985 // debit instead of routing through accountHolds().
986 if (after > before || (before - after) != std::min(before, clawAmount))
987 {
988 JLOG(j.fatal())
989 << "Invariant failed: MPT clawback balance change is invalid";
990 return !mptV2Enabled;
991 }
992
993 return true;
994 });
995 }
996 }
997 else
998 {
999 if (trustlinesChanged_ != 0)
1000 {
1001 JLOG(j.fatal()) << "Invariant failed: some trustlines were changed "
1002 "despite failure of the transaction.";
1003 return false;
1004 }
1005
1006 if (mptokensChanged_ != 0)
1007 {
1008 JLOG(j.fatal()) << "Invariant failed: some mptokens were changed "
1009 "despite failure of the transaction.";
1010 return false;
1011 }
1012 }
1013
1014 return true;
1015}
1016
1017//------------------------------------------------------------------------------
1018
1019// Whether `entry` is the trust line or MPToken through which `account` holds
1020// `asset`. XRP has no such entry.
1021static bool
1022isHolding(SLE const& entry, Asset const& asset, AccountID const& account)
1023{
1024 return asset.visit(
1025 [&](Issue const& issue) {
1026 return !isXRP(issue) && keylet::trustLine(account, issue).check(entry);
1027 },
1028 [&](MPTIssue const& issue) {
1029 return keylet::mptoken(issue.getMptID(), account).check(entry);
1030 });
1031}
1032
1033// Whether `entry`, linked into the owner directory of `pseudo`, belongs to the
1034// AMM, Vault or LoanBroker that the pseudo-account was created for.
1035static bool
1036isOwnedByPseudoAccount(ReadView const& view, SLE const& pseudo, SLE const& entry)
1037{
1038 auto const pseudoAcctID = pseudo[sfAccount];
1039
1040 if (pseudo.isFieldPresent(sfAMMID))
1041 {
1042 if (entry.getType() == ltAMM)
1043 return entry.key() == pseudo[sfAMMID];
1044
1045 auto const amm = view.read(keylet::amm(pseudo[sfAMMID]));
1046 if (!amm)
1047 return false; // LCOV_EXCL_LINE
1048 if (isHolding(entry, (*amm)[sfAsset], pseudoAcctID) ||
1049 isHolding(entry, (*amm)[sfAsset2], pseudoAcctID))
1050 return true;
1051
1052 // The AMM issues LP tokens, so each liquidity provider's trust line
1053 // links into its directory.
1054 if (entry.getType() != ltRIPPLE_STATE)
1055 return false;
1056 auto const low = entry[sfLowLimit].getIssuer();
1057 auto const high = entry[sfHighLimit].getIssuer();
1058 auto const lpCurrency = (*amm)[sfLPTokenBalance].get<Issue>().currency;
1059 return (low == pseudoAcctID || high == pseudoAcctID) &&
1060 entry.key() == keylet::trustLine(low, high, lpCurrency).key;
1061 }
1062
1063 if (pseudo.isFieldPresent(sfVaultID))
1064 {
1065 if (entry.getType() == ltLOAN_BROKER)
1066 return entry[sfVaultID] == pseudo[sfVaultID];
1067
1068 auto const vault = view.read(keylet::vault(pseudo[sfVaultID]));
1069 if (!vault)
1070 return false; // LCOV_EXCL_LINE
1071 return keylet::mptokenIssuance((*vault)[sfShareMPTID]).check(entry) ||
1072 isHolding(entry, (*vault)[sfAsset], pseudoAcctID);
1073 }
1074
1075 if (pseudo.isFieldPresent(sfLoanBrokerID))
1076 {
1077 if (entry.getType() == ltLOAN)
1078 return entry[sfLoanBrokerID] == pseudo[sfLoanBrokerID];
1079
1080 auto const broker = view.read(keylet::loanBroker(pseudo[sfLoanBrokerID]));
1081 if (!broker)
1082 return false; // LCOV_EXCL_LINE
1083 auto const vault = view.read(keylet::vault((*broker)[sfVaultID]));
1084 return vault && isHolding(entry, (*vault)[sfAsset], pseudoAcctID);
1085 }
1086
1087 // A new pseudo-account kind must say what it owns.
1088 return false; // LCOV_EXCL_LINE
1089}
1090
1091void
1093{
1094 if (isDelete)
1095 {
1096 // Deletion is ignored
1097 return;
1098 }
1099
1100 // Only owner directory pages carry sfOwner; book directories don't.
1101 if (after && after->getType() == ltDIR_NODE && after->isFieldPresent(sfOwner))
1102 {
1103 auto const& afterIndexes = after->getFieldV256(sfIndexes);
1105 if (!before)
1106 {
1107 added.assign(afterIndexes.begin(), afterIndexes.end());
1108 }
1109 else
1110 {
1111 auto const& beforeIndexes = before->getFieldV256(sfIndexes);
1112 for (auto const& index : afterIndexes)
1113 {
1114 if (!std::ranges::contains(beforeIndexes, index))
1115 added.push_back(index);
1116 }
1117 }
1118
1119 if (!added.empty())
1120 {
1121 auto& owned = ownerDirAdditions_[after->getAccountID(sfOwner)];
1122 owned.insert(owned.end(), added.begin(), added.end());
1123 }
1124 return;
1125 }
1126
1127 if (after && after->getType() == ltACCOUNT_ROOT)
1128 {
1129 bool const isPseudo = [&] {
1130 // isPseudoAccount checks that any of the pseudo-account fields are
1131 // set.
1133 return true;
1134 // Not all pseudo-accounts have a zero sequence, but all accounts
1135 // with a zero sequence had better be pseudo-accounts.
1136 if (after->at(sfSequence) == 0)
1137 return true;
1138
1139 return false;
1140 }();
1141 if (isPseudo)
1142 {
1143 // Pseudo accounts must have the following properties:
1144 // 1. Exactly one of the pseudo-account fields is set.
1145 // 2. The sequence number is not changed.
1146 // 3. The lsfDisableMaster, lsfDefaultRipple, and lsfDepositAuth
1147 // flags are set.
1148 // 4. The RegularKey is not set.
1149 // 5. The SponsoredOwnerCount, SponsoringOwnerCount, SponsoringAccountCount, Sponsor
1150 // fields are not set.
1151 {
1153
1154 auto const numFields = std::ranges::count_if(
1155 fields,
1156 [&after](SField const* sf) -> bool { return after->isFieldPresent(*sf); });
1157 if (numFields != 1)
1158 {
1159 std::stringstream error;
1160 error << "pseudo-account has " << numFields << " pseudo-account fields set";
1161 errors_.emplace_back(error.str());
1162 }
1163 }
1164 if (before && before->at(sfSequence) != after->at(sfSequence))
1165 {
1166 errors_.emplace_back("pseudo-account sequence changed");
1167 }
1168 if (!after->isFlag(lsfDisableMaster | lsfDefaultRipple | lsfDepositAuth))
1169 {
1170 errors_.emplace_back("pseudo-account flags are not set");
1171 }
1172 if (after->isFieldPresent(sfRegularKey))
1173 {
1174 errors_.emplace_back("pseudo-account has a regular key");
1175 }
1176 if (after->isFieldPresent(sfSponsoredOwnerCount) ||
1177 after->isFieldPresent(sfSponsoringOwnerCount) || after->isFieldPresent(sfSponsor) ||
1178 after->isFieldPresent(sfSponsoringAccountCount))
1179 {
1180 errors_.emplace_back("pseudo-account has a sponsorship field");
1181 }
1182 }
1183 }
1184}
1185
1186bool
1188 STTx const& tx,
1189 TER const,
1190 XRPAmount const,
1191 ReadView const& view,
1192 beast::Journal const& j)
1193{
1194 bool const enforce = view.rules().enabled(featureSingleAssetVault);
1195 XRPL_ASSERT(
1196 errors_.empty() || enforce,
1197 "xrpl::ValidPseudoAccounts::finalize : no bad "
1198 "changes or enforce invariant");
1199 if (!errors_.empty())
1200 {
1201 for (auto const& error : errors_)
1202 {
1203 JLOG(j.fatal()) << "Invariant failed: " << error;
1204 }
1205 if (enforce)
1206 return false;
1207 }
1208
1209 // Pre-fixCleanup3_5_0: what a pseudo-account owns is not checked.
1210 // Post-fixCleanup3_5_0: it may own only what belongs to its AMM, Vault or
1211 // LoanBroker.
1212 if (!view.rules().enabled(fixCleanup3_5_0))
1213 return true;
1214
1215 for (auto const& [owner, indexes] : ownerDirAdditions_)
1216 {
1217 auto const root = view.read(keylet::account(owner));
1218 if (!root || !isPseudoAccount(root))
1219 continue;
1220
1221 for (auto const& index : indexes)
1222 {
1223 auto const sle = view.read(keylet::unchecked(index));
1224 if (!sle)
1225 {
1226 JLOG(j.fatal()) << "Invariant failed: pseudo-account " << toBase58(owner)
1227 << " owner directory links a missing object " << to_string(index);
1228 return false;
1229 }
1230
1231 if (!isOwnedByPseudoAccount(view, *root, *sle))
1232 {
1233 JLOG(j.fatal()) << "Invariant failed: pseudo-account " << toBase58(owner)
1234 << " may not own " << ledgerEntryTypeName(*sle) << " "
1235 << to_string(index);
1236 return false;
1237 }
1238 }
1239 }
1240
1241 return true;
1242}
1243
1244//------------------------------------------------------------------------------
1245
1246void
1248{
1249 if (isDelete || !before)
1250 {
1251 // Creation and deletion are ignored
1252 return;
1253 }
1254
1255 changedEntries_.emplace(before, after);
1256}
1257
1258bool
1260 STTx const& tx,
1261 TER const,
1262 XRPAmount const,
1263 ReadView const& view,
1264 beast::Journal const& j)
1265{
1266 auto const kFieldChanged = [&j, &tx](auto const& before, auto const& after, auto const& field) {
1267 bool const beforeField = before->isFieldPresent(field);
1268 bool const afterField = after->isFieldPresent(field);
1269 bool const changed =
1270 beforeField != afterField || (afterField && before->at(field) != after->at(field));
1271 if (changed)
1272 {
1273 JLOG(j.fatal()) << "Invariant failed: " << field.getName()
1274 << " changed on immutable ledger entry in " << tx.getTransactionID();
1275 }
1276 return changed;
1277 };
1278 for (auto const& slePair : changedEntries_)
1279 {
1280 auto const& before = slePair.first;
1281 auto const& after = slePair.second;
1282 auto const type = after->getType();
1283 // featureLendingProtocol gates enforcement, not detection: changes are
1284 // always logged, but the transaction is only failed once the amendment
1285 // is enabled. Type-specific field lists may add their own gates (see
1286 // ltVAULT).
1287 bool const enforce = view.rules().enabled(featureLendingProtocol);
1288 bool bad = kFieldChanged(before, after, sfLedgerEntryType) ||
1289 kFieldChanged(before, after, sfLedgerIndex);
1290 switch (type)
1291 {
1292 case ltLOAN_BROKER:
1293 bad = bad || kFieldChanged(before, after, sfSequence) ||
1294 kFieldChanged(before, after, sfOwnerNode) ||
1295 kFieldChanged(before, after, sfVaultNode) ||
1296 kFieldChanged(before, after, sfVaultID) ||
1297 kFieldChanged(before, after, sfAccount) ||
1298 kFieldChanged(before, after, sfOwner) ||
1299 kFieldChanged(before, after, sfManagementFeeRate) ||
1300 kFieldChanged(before, after, sfCoverRateMinimum) ||
1301 kFieldChanged(before, after, sfCoverRateLiquidation);
1302 // LoanBroker flags (lsfLoanBrokerPrivate) only exist from
1303 // featureLendingProtocolV1_2 onwards and are set at creation.
1304 if (view.rules().enabled(featureLendingProtocolV1_2))
1305 bad = bad || kFieldChanged(before, after, sfFlags);
1306 break;
1307 case ltLOAN:
1308 bad = bad || kFieldChanged(before, after, sfSequence) ||
1309 kFieldChanged(before, after, sfOwnerNode) ||
1310 kFieldChanged(before, after, sfLoanBrokerNode) ||
1311 kFieldChanged(before, after, sfLoanBrokerID) ||
1312 kFieldChanged(before, after, sfBorrower) ||
1313 kFieldChanged(before, after, sfLoanOriginationFee) ||
1314 kFieldChanged(before, after, sfLoanServiceFee) ||
1315 kFieldChanged(before, after, sfLatePaymentFee) ||
1316 kFieldChanged(before, after, sfClosePaymentFee) ||
1317 kFieldChanged(before, after, sfOverpaymentFee) ||
1318 kFieldChanged(before, after, sfInterestRate) ||
1319 kFieldChanged(before, after, sfLateInterestRate) ||
1320 kFieldChanged(before, after, sfCloseInterestRate) ||
1321 kFieldChanged(before, after, sfOverpaymentInterestRate) ||
1322 kFieldChanged(before, after, sfStartDate) ||
1323 kFieldChanged(before, after, sfPaymentInterval) ||
1324 kFieldChanged(before, after, sfGracePeriod) ||
1325 kFieldChanged(before, after, sfLoanScale);
1326
1327 // lsfLoanOverpayment must never toggle. lsfLoanDefault may only
1328 // transition from unset to set, which combined with ValidLoan's rule that
1329 // only LoanManage may change it makes the flag write-once.
1330 if (view.rules().enabled(featureLendingProtocolV1_1))
1331 {
1332 std::uint32_t const beforeFlags = before->getFlags();
1333 std::uint32_t const afterFlags = after->getFlags();
1334 bool const overpaymentChanged =
1335 (beforeFlags & lsfLoanOverpayment) != (afterFlags & lsfLoanOverpayment);
1336 if (overpaymentChanged)
1337 {
1338 JLOG(j.fatal()) << "Invariant failed: lsfLoanOverpayment flag "
1339 "toggled on immutable ledger entry in "
1340 << tx.getTransactionID();
1341 }
1342 bad = bad || overpaymentChanged;
1343 bool const defaultCleared =
1344 (beforeFlags & lsfLoanDefault) != 0 && (afterFlags & lsfLoanDefault) == 0;
1345 if (defaultCleared)
1346 {
1347 JLOG(j.fatal()) << "Invariant failed: lsfLoanDefault flag "
1348 "cleared on immutable ledger entry in "
1349 << tx.getTransactionID();
1350 }
1351 bad = bad || defaultCleared;
1352 }
1353 break;
1354 case ltVAULT:
1355 /*
1356 * All the fields below are only immutable from
1357 * featureLendingProtocolV1_1 onwards; some of them only exist on
1358 * V1_1 vaults. Before that amendment, sfAsset, sfAccount and
1359 * sfShareMPTID are checked by VaultInvariant instead.
1360 */
1361 if (view.rules().enabled(featureLendingProtocolV1_1))
1362 {
1363 bad = bad || kFieldChanged(before, after, sfVaultKind) ||
1364 kFieldChanged(before, after, sfSubscriptionDate) ||
1365 kFieldChanged(before, after, sfRedemptionDate) ||
1366 kFieldChanged(before, after, sfSequence) ||
1367 kFieldChanged(before, after, sfOwnerNode) ||
1368 kFieldChanged(before, after, sfOwner) ||
1369 kFieldChanged(before, after, sfWithdrawalPolicy) ||
1370 kFieldChanged(before, after, sfScale) ||
1371 kFieldChanged(before, after, sfLEVersion) ||
1372 kFieldChanged(before, after, sfAsset) ||
1373 kFieldChanged(before, after, sfAccount) ||
1374 kFieldChanged(before, after, sfShareMPTID);
1375 }
1376 break;
1377 default:
1378 break;
1379 }
1380 XRPL_ASSERT(
1381 !bad || enforce,
1382 "xrpl::NoModifiedUnmodifiableFields::finalize : no bad "
1383 "changes or enforce invariant");
1384 if (bad)
1385 {
1386 JLOG(j.fatal()) << "Invariant failed: changed an unchangeable field for "
1387 << tx.getTransactionID();
1388 if (enforce)
1389 return false;
1390 }
1391 }
1392 return true;
1393}
1394
1395void
1397 bool isDelete,
1400{
1401 if (!isDelete && after)
1402 afterEntries_.push_back(after);
1403}
1404
1405bool
1407 STTx const&,
1408 TER const,
1409 XRPAmount const,
1410 ReadView const& view,
1411 beast::Journal const& j) const
1412{
1413 bool const badLedgerEntry = std::ranges::any_of(
1414 afterEntries_, [&](auto const& sle) { return hasInvalidAmount(*sle, j); });
1415
1416 if (badLedgerEntry)
1417 {
1418 JLOG(j.fatal())
1419 << "Invariant failed: ledger entry contains non-canonical MPT or XRP amount";
1420 return !view.rules().enabled(fixCleanup3_2_0);
1421 }
1422
1423 return true;
1424}
1425
1426void
1428{
1429 if (!isDelete)
1430 return;
1431
1432 // Before should never be null when isDelete = true
1433 if (!before)
1434 {
1435 // LCOV_EXCL_START
1436 UNREACHABLE(
1437 "xrpl::ObjectHasPseudoAccount::visitEntry : deleted ledger entry missing before state");
1438 return;
1439 // LCOV_EXCL_STOP
1440 }
1441
1442 switch (before->getType())
1443 {
1444 case ltAMM:
1445 case ltVAULT:
1446 case ltLOAN_BROKER:
1447 deletedObjSles_.push_back(before);
1448 break;
1449 default:
1450 return;
1451 }
1452}
1453
1454[[nodiscard]] bool
1456 STTx const&,
1457 TER const,
1458 XRPAmount const,
1459 ReadView const& view,
1460 beast::Journal const& j) const
1461{
1462 if (!view.rules().enabled(fixCleanup3_3_0))
1463 return true;
1464
1465 if (deletedObjSles_.empty())
1466 return true;
1467
1468 bool failed = false;
1469 for (auto const& sle : deletedObjSles_)
1470 {
1471 if (!sle->isFieldPresent(sfAccount))
1472 {
1473 JLOG(j.fatal()) << "Invariant failed: deleted " << ledgerEntryTypeName(*sle)
1474 << " is missing pseudo-account field";
1475 failed = true;
1476 continue;
1477 }
1478
1479 // The pseudo-account must NOT exist on the ledger after the object is deleted.
1480 if (view.exists(keylet::account(sle->getAccountID(sfAccount))))
1481 {
1482 JLOG(j.fatal()) << "Invariant failed: deleted " << ledgerEntryTypeName(*sle)
1483 << " without deleting its pseudo-account";
1484 failed = true;
1485 }
1486 }
1487
1488 return !failed;
1489}
1490
1491} // namespace xrpl
T any_of(T... args)
T assign(T... args)
T begin(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
Stream fatal() const
Definition Journal.h:368
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
std::vector< std::pair< SLE::const_pointer, SLE::const_pointer > > accountsDeleted_
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
Definition Asset.h:117
BaseUInt next() const
Definition base_uint.h:478
A currency issued by an account.
Definition Issue.h:18
Currency currency
Definition Issue.h:20
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
static LedgerFormats const & getInstance()
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
std::set< std::pair< SLE::const_pointer, SLE::const_pointer > > changedEntries_
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
std::vector< SLE::const_pointer > deletedObjSles_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
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.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition ReadView.h:115
virtual std::optional< key_type > succ(key_type const &key, std::optional< key_type > const &last=std::nullopt) const =0
Return the key of the next state item.
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:182
Identifies fields.
Definition SField.h:131
constexpr TIss const & get() const
void negate()
Definition STAmount.h:586
std::uint64_t mantissa() const noexcept
Definition STAmount.h:490
int signum() const noexcept
Definition STAmount.h:522
bool negative() const noexcept
Definition STAmount.h:484
bool native() const noexcept
Definition STAmount.h:471
Asset const & asset() const
Definition STAmount.h:496
AccountID const & getIssuer() const
Definition STAmount.h:516
LedgerEntryType getType() const
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::shared_ptr< STLedgerEntry const > const_pointer
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:643
STAmount const & getFieldAmount(SField const &field) const
Definition STObject.cpp:657
TxType getTxnType() const
Definition STTx.h:250
UInt256 getTransactionID() const
Definition STTx.h:262
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
static bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
std::vector< SLE::const_pointer > afterEntries_
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
std::uint32_t mptokensChanged_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
std::uint32_t trustlinesChanged_
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
std::vector< std::string > errors_
std::map< AccountID, std::vector< uint256 > > ownerDirAdditions_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:163
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
boost::multiprecision::int128_t drops_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &) const
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
T count_if(T... args)
T empty(T... args)
T end(T... args)
T invoke(T... args)
T min(T... args)
constexpr Zero kZero
Definition Zero.h:30
Keylet computation functions.
Definition Indexes.h:39
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Definition Indexes.cpp:483
Keylet nftokenPageMin(AccountID const &owner)
NFT page keylets.
Definition Indexes.cpp:442
Keylet unchecked(UInt256 const &key) noexcept
Any ledger entry.
Definition Indexes.cpp:409
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:565
Keylet nftokenPageMax(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Definition Indexes.cpp:450
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:547
Keylet loanBroker(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:571
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:541
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
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
std::vector< SField const * > const & getPseudoAccountFields()
Returns the list of fields that define an ACCOUNT_ROOT as a pseudo-account if set.
bool isXRP(AccountID const &c)
Definition AccountID.h:84
static bool isOwnedByPseudoAccount(ReadView const &view, SLE const &pseudo, SLE const &entry)
bool isFeatureEnabled(UInt256 const &feature, bool resultIfNoRules)
Check whether a feature is enabled in the current ledger rules.
Definition Rules.cpp:199
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
Number root(Number f, unsigned d)
Privilege
Operations a transaction is permitted to perform, as a bitfield.
Definition TxSettings.h:19
static bool isHolding(SLE const &entry, Asset const &asset, AccountID const &account)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
STLedgerEntry SLE
bool hasPrivilege(STTx const &tx, Privilege priv)
static std::optional< STAmount > clawbackTrustLineBalanceInHolderTerms(SLE::const_pointer const &sle, AccountID const &holder, AccountID const &issuer, Currency const &currency)
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:668
static std::string ledgerEntryTypeName(SLE const &sle)
bool hasInvalidAmount(STBase const &field, beast::Journal j)
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
bool isTesSuccess(TER x) noexcept
Definition TER.h:686
TERSubset< CanCvtToTER > TER
Definition TER.h:657
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
Definition Protocol.h:297
constexpr XRPAmount kInitialXrp
Configure the native currency.
std::array< KeyletDesc< AccountID const & >, 6 > const kDirectAccountKeylets
Definition Indexes.cpp:37
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
T push_back(T... args)
T str(T... args)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
UInt256 key
Definition Keylet.h:21
bool check(STLedgerEntry const &) const
Returns true if the SLE matches the type.
Definition Keylet.cpp:10
T to_string(T... args)