rippled
Loading...
Searching...
No Matches
InvariantCheck.h
1#pragma once
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/protocol/MPTIssue.h>
7#include <xrpl/protocol/STLedgerEntry.h>
8#include <xrpl/protocol/STTx.h>
9#include <xrpl/protocol/TER.h>
10
11#include <cstdint>
12#include <tuple>
13#include <unordered_set>
14
15namespace xrpl {
16
17class ReadView;
18
19#if GENERATING_DOCS
29{
30public:
31 explicit InvariantChecker_PROTOTYPE() = default;
32
40 void
42
55 bool
56 finalize(STTx const& tx, TER const tec, XRPAmount const fee, ReadView const& view, beast::Journal const& j);
57};
58#endif
59
67{
68public:
69 void
71
72 bool
73 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
74};
75
85{
87
88public:
89 void
91
92 bool
93 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
94};
95
105{
107
108public:
109 void
111
112 bool
113 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
114};
115
127{
128 // Pair is <before, after>. Before is used for most of the checks, so that
129 // if, for example, an object ID field is cleared, but the object is not
130 // deleted, it can still be found. After is used specifically for any checks
131 // that are expected as part of the deletion, such as zeroing out the
132 // balance.
134
135public:
136 void
138
139 bool
140 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
141};
142
151{
152 bool bad_ = false;
153
154public:
155 void
157
158 bool
159 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
160};
161
167{
168 bool typeMismatch_ = false;
169 bool invalidTypeAdded_ = false;
170
171public:
172 void
174
175 bool
176 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
177};
178
186{
187 bool xrpTrustLine_ = false;
188
189public:
190 void
192
193 bool
194 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
195};
196
205{
207
208public:
209 void
211
212 bool
213 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
214};
215
223{
229
235
238
240
241public:
242 void
244
245 bool
246 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
247
248private:
249 bool
251
254 std::shared_ptr<SLE const> const& before,
256 bool isDelete);
257
258 void
259 recordBalance(Issue const& issue, BalanceChange change);
260
261 void
263
265 findIssuer(AccountID const& issuerID, ReadView const& view);
266
267 bool
269 std::shared_ptr<SLE const> const& issuer,
270 IssuerChanges const& changes,
271 STTx const& tx,
272 beast::Journal const& j,
273 bool enforce);
274
275 bool
277 BalanceChange const& change,
278 bool high,
279 STTx const& tx,
280 beast::Journal const& j,
281 bool enforce,
282 bool globalFreeze);
283};
284
293{
294 bool bad_ = false;
295
296public:
297 void
299
300 bool
301 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
302};
303
309{
310 bool bad_ = false;
311
312public:
313 void
315
316 bool
317 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
318};
319
326{
329 bool pseudoAccount_ = false;
331
332public:
333 void
335
336 bool
337 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
338};
339
352{
353 bool badEntry_ = false;
354 bool badLink_ = false;
355 bool badSort_ = false;
356 bool badURI_ = false;
357 bool invalidSize_ = false;
358 bool deletedFinalPage_ = false;
359 bool deletedLink_ = false;
360
361public:
362 void
364
365 bool
366 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
367};
368
396
406{
409
410public:
411 void
413
414 bool
415 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
416};
417
419{
422
425 // non-MPT transactions may attempt to create
426 // MPToken by an issuer
428
429public:
430 void
432
433 bool
434 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
435};
436
448{
450 {
452 bool isSorted_ = false;
453 bool isUnique_ = false;
454 bool isDelete_ = false;
455 };
457
458public:
459 void
461
462 bool
463 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
464};
465
475{
477
478public:
479 void
481
482 bool
483 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
484};
485
487{
488 bool regularOffers_ = false;
489 bool badHybrids_ = false;
491
492public:
493 void
495
496 bool
497 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
498};
499
501{
506
507public:
508 enum class ZeroAllowed : bool { No = false, Yes = true };
509
511 {
512 }
513 void
515
516 bool
517 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
518
519private:
520 bool
521 finalizeBid(bool enforce, beast::Journal const&) const;
522 bool
523 finalizeVote(bool enforce, beast::Journal const&) const;
524 bool
525 finalizeCreate(STTx const&, ReadView const&, bool enforce, beast::Journal const&) const;
526 bool
527 finalizeDelete(bool enforce, TER res, beast::Journal const&) const;
528 bool
529 finalizeDeposit(STTx const&, ReadView const&, bool enforce, beast::Journal const&) const;
530 // Includes clawback
531 bool
532 finalizeWithdraw(STTx const&, ReadView const&, bool enforce, beast::Journal const&) const;
533 bool
534 finalizeDEX(bool enforce, beast::Journal const&) const;
535 bool
536 generalInvariant(STTx const&, ReadView const&, ZeroAllowed zeroAllowed, beast::Journal const&) const;
537};
538
547{
548 // Pair is <before, after>.
550
551public:
552 void
554
555 bool
556 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
557};
558
568{
569 // Not all of these elements will necessarily be populated. Remaining items
570 // will be looked up as needed.
572 {
574 // After is used for most of the checks, except
575 // those that check changed values.
577 };
578 // Collect all the LoanBrokers found directly or indirectly through
579 // pseudo-accounts. Key is the brokerID / index. It will be used to find the
580 // LoanBroker object if brokerBefore and brokerAfter are nullptr
582 // Collect all the modified trust lines. Their high and low accounts will be
583 // loaded to look for LoanBroker pseudo-accounts.
585 // Collect all the modified MPTokens. Their accounts will be loaded to look
586 // for LoanBroker pseudo-accounts.
588
589 bool
590 goodZeroDirectory(ReadView const& view, SLE::const_ref dir, beast::Journal const& j) const;
591
592public:
593 void
595
596 bool
597 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
598};
599
607{
608 // Pair is <before, after>. After is used for most of the checks, except
609 // those that check changed values.
611
612public:
613 void
615
616 bool
617 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
618};
619
620/*
621 * @brief Invariants: Vault object and MPTokenIssuance for vault shares
622 *
623 * - vault deleted and vault created is empty
624 * - vault created must be linked to pseudo-account for shares and assets
625 * - vault must have MPTokenIssuance for shares
626 * - vault without shares outstanding must have no shares
627 * - loss unrealized does not exceed the difference between assets total and
628 * assets available
629 * - assets available do not exceed assets total
630 * - vault deposit increases assets and share issuance, and adds to:
631 * total assets, assets available, shares outstanding
632 * - vault withdrawal and clawback reduce assets and share issuance, and
633 * subtracts from: total assets, assets available, shares outstanding
634 * - vault set must not alter the vault assets or shares balance
635 * - no vault transaction can change loss unrealized (it's updated by loan
636 * transactions)
637 *
638 */
640{
641 Number static constexpr zero{};
642
643 struct Vault final
644 {
645 uint256 key = beast::zero;
649 uint192 shareMPTID = beast::zero;
654
655 Vault static make(SLE const&);
656 };
657
658 struct Shares final
659 {
663
664 Shares static make(SLE const&);
665 };
666
672
673public:
674 void
676
677 bool
678 finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);
679};
680
681// additional invariant checks can be declared above and then added to this
682// tuple
702 ValidAMM,
706 ValidLoan,
707 ValidVault>;
708
717inline InvariantChecks
719{
720 return InvariantChecks{};
721}
722
723} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Invariant: a deleted account must not have any objects left.
std::vector< std::pair< std::shared_ptr< SLE const >, std::shared_ptr< SLE const > > > accountsDeleted_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
Invariant: we cannot remove an account ledger entry.
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
Prototype for invariant check implementations.
bool finalize(STTx const &tx, TER const tec, XRPAmount const fee, ReadView const &view, beast::Journal const &j)
called after all ledger entries have been visited to determine the final status of the check
void visitEntry(bool isDelete, std::shared_ptr< SLE const > const &before, std::shared_ptr< SLE const > const &after)
called for each ledger entry in the current transaction.
A currency issued by an account.
Definition Issue.h:13
Invariant: corresponding modified ledger entries should match in type and added entries should be a v...
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
Invariant: Validates counts of NFTokens after all transaction types.
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
Invariant: offers should be for non-negative amounts and must not be XRP to XRP.
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
Invariant: Trust lines with deep freeze flag are not allowed if normal freeze flag is not set.
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
Invariants: Some fields are unmodifiable.
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
std::set< std::pair< SLE::const_pointer, SLE::const_pointer > > changedEntries_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
Invariant: Trust lines using XRP are not allowed.
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
Invariant: an escrow entry must take a value between 0 and INITIAL_XRP drops exclusive.
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
Number is a floating point type that can represent a wide range of values.
Definition Number.h:207
A view into a ledger.
Definition ReadView.h:31
Invariant: We should never charge a transaction a negative fee or a fee that is larger than what the ...
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
Invariant: frozen trust line balance change is not allowed.
std::shared_ptr< SLE const > findIssuer(AccountID const &issuerID, ReadView const &view)
void recordBalance(Issue const &issue, BalanceChange change)
bool isValidEntry(std::shared_ptr< SLE const > const &before, std::shared_ptr< SLE const > const &after)
bool validateIssuerChanges(std::shared_ptr< SLE const > const &issuer, IssuerChanges const &changes, STTx const &tx, beast::Journal const &j, bool enforce)
std::map< AccountID, std::shared_ptr< SLE const > const > possibleIssuers_
STAmount calculateBalanceChange(std::shared_ptr< SLE const > const &before, std::shared_ptr< SLE const > const &after, bool isDelete)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
bool validateFrozenState(BalanceChange const &change, bool high, STTx const &tx, beast::Journal const &j, bool enforce, bool globalFreeze)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
void recordBalanceChanges(std::shared_ptr< SLE const > const &after, STAmount const &balanceChange)
std::optional< AccountID > ammAccount_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
bool finalizeWithdraw(STTx const &, ReadView const &, bool enforce, beast::Journal const &) const
bool finalizeBid(bool enforce, beast::Journal const &) const
bool finalizeCreate(STTx const &, ReadView const &, bool enforce, beast::Journal const &) const
bool finalizeDEX(bool enforce, beast::Journal const &) const
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalizeDeposit(STTx const &, ReadView const &, bool enforce, beast::Journal const &) const
bool generalInvariant(STTx const &, ReadView const &, ZeroAllowed zeroAllowed, beast::Journal const &) const
std::optional< STAmount > lptAMMBalanceAfter_
bool finalizeVote(bool enforce, beast::Journal const &) const
bool finalizeDelete(bool enforce, TER res, beast::Journal const &) const
std::optional< STAmount > lptAMMBalanceBefore_
Invariant: Token holder's trustline balance cannot be negative after Clawback.
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
std::uint32_t trustlinesChanged
std::uint32_t mptokensChanged
Invariants: Loan brokers are internally consistent.
std::map< uint256, BrokerInfo > brokers_
bool goodZeroDirectory(ReadView const &view, SLE::const_ref dir, beast::Journal const &j) const
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
std::vector< SLE::const_pointer > mpts_
std::vector< SLE::const_pointer > lines_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
Invariants: Loans are internally consistent.
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
std::vector< std::pair< SLE::const_pointer, SLE::const_pointer > > loans_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::uint32_t mptokensCreated_
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::uint32_t mptokensDeleted_
std::uint32_t mptIssuancesCreated_
std::uint32_t mptIssuancesDeleted_
Invariant: Validates several invariants for NFToken pages.
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
Invariant: a new account root must be the consequence of a payment, must have the right starting sequ...
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
hash_set< uint256 > domains_
Invariants: Permissioned Domains must have some rules and AcceptedCredentials must have length betwee...
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
std::vector< SleStatus > sleStatus_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
Invariants: Pseudo-accounts have valid and consistent properties.
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
std::vector< std::string > errors_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
static Number constexpr zero
std::vector< Shares > afterMPTs_
std::unordered_map< uint256, Number > deltas_
std::vector< Vault > afterVault_
std::vector< Shares > beforeMPTs_
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
std::vector< Vault > beforeVault_
Invariant: An account XRP balance must be in XRP and take a value between 0 and INITIAL_XRP drops,...
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
Invariant: A transaction must not create XRP and should only destroy the XRP fee.
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
InvariantChecks getInvariantChecks()
get a tuple of all invariant checks
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:3436
std::shared_ptr< SLE const > const line
std::vector< BalanceChange > senders
std::vector< BalanceChange > receivers
static Shares make(SLE const &)
static Vault make(SLE const &)