xrpld
Loading...
Searching...
No Matches
InvariantsBase.h
1#pragma once
2
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/amount.h>
6
7#include <xrpld/app/main/Application.h>
8
9#include <xrpl/basics/base_uint.h>
10#include <xrpl/beast/unit_test/suite.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/Feature.h>
13#include <xrpl/protocol/Keylet.h>
14#include <xrpl/protocol/STObject.h>
15#include <xrpl/protocol/STTx.h>
16#include <xrpl/protocol/TER.h>
17#include <xrpl/protocol/TxFormats.h>
18#include <xrpl/protocol/XRPAmount.h>
19#include <xrpl/tx/ApplyContext.h>
20
21#include <cstdint>
22#include <functional>
23#include <initializer_list>
24#include <memory>
25#include <source_location>
26#include <string>
27#include <vector>
28
29namespace xrpl {
30
31class Transactor;
32
33// Test-only factory — not part of the public API.
34// The returned Transactor holds a raw reference to ctx; the caller must ensure
35// the ApplyContext outlives the Transactor. Implemented in applySteps.cpp
36std::unique_ptr<Transactor>
38
39} // namespace xrpl
40
41namespace xrpl::test {
42
44{
45protected:
46 // The optional Preclose function is used to process additional transactions
47 // on the ledger after creating two accounts, but before closing it, and
48 // before the Precheck function. These should only be valid functions, and
49 // not direct manipulations. Preclose is not commonly used.
51 bool(test::jtx::Account const& a, test::jtx::Account const& b, test::jtx::Env& env)>;
52
53 // this is common setup/method for running a failing invariant check. The
54 // precheck function is used to manipulate the ApplyContext with view
55 // changes that will cause the check to fail.
57 bool(test::jtx::Account const& a, test::jtx::Account const& b, ApplyContext& ac)>;
58
59 enum class TxAccount : int { None = 0, A1, A2 };
60
62 makeEnv(FeatureBitset features);
63
81 void
83 std::vector<std::string> const& expectLogs,
84 Precheck const& precheck,
85 XRPAmount fee = XRPAmount{},
86 STTx tx = STTx{ttACCOUNT_SET, [](STObject&) {}},
87 std::initializer_list<TER> ters = {tecINVARIANT_FAILED, tefINVARIANT_FAILED},
88 Preclose const& preclose = {},
89 TxAccount setTxAccount = TxAccount::None,
90 std::source_location const& loc = std::source_location::current(),
91 // Result fed to the invariant checker on the first pass. Set it to a
92 // tec to exercise result-dependent invariants; the harness runs no
93 // transactor, so one never arises on its own.
94 TER initialResult = tesSUCCESS);
95
96 void
98 test::jtx::Env&& env,
99 std::vector<std::string> const& expectLogs,
100 Precheck const& precheck,
101 XRPAmount fee = XRPAmount{},
102 STTx tx = STTx{ttACCOUNT_SET, [](STObject&) {}},
103 std::initializer_list<TER> ters = {tecINVARIANT_FAILED, tefINVARIANT_FAILED},
104 Preclose const& preclose = {},
105 TxAccount setTxAccount = TxAccount::None,
106 std::source_location const& loc = std::source_location::current(),
107 TER initialResult = tesSUCCESS);
108
109 void
111 // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
112 test::jtx::Env&& env,
113 test::jtx::Account const& a1,
114 test::jtx::Account const& a2,
115 std::vector<std::string> const& expectLogs,
116 Precheck const& precheck,
117 XRPAmount fee = XRPAmount{},
118 STTx tx = STTx{ttACCOUNT_SET, [](STObject&) {}},
119 std::initializer_list<TER> ters = {tecINVARIANT_FAILED, tefINVARIANT_FAILED},
120 std::source_location const& loc = std::source_location::current(),
121 TER initialResult = tesSUCCESS);
122
123 Keylet
124 createLoanBroker(jtx::Account const& a, jtx::Env& env, jtx::PrettyAsset const& asset);
125
126 // Build an ltLOAN SLE with every SoeRequired field explicitly set and
127 // every SoeDefault field the invariants read via `at()` materialized, so
128 // rawInsert-based tests don't accidentally trip an unrelated invariant
129 // or throw from a missing SoeDefault field.
130 static SLE::pointer
131 makeLoanSle(UInt256 const& loanBrokerID, std::uint32_t loanSeq, AccountID const& borrower);
132};
133
134} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
State information when applying a tx.
std::shared_ptr< STLedgerEntry > pointer
Keylet createLoanBroker(jtx::Account const &a, jtx::Env &env, jtx::PrettyAsset const &asset)
std::function< bool(test::jtx::Account const &a, test::jtx::Account const &b, ApplyContext &ac)> Precheck
test::jtx::Env makeEnv(FeatureBitset features)
void doInvariantCheck(std::vector< std::string > const &expectLogs, Precheck const &precheck, XRPAmount fee=XRPAmount{}, STTx tx=STTx{ttACCOUNT_SET, [](STObject &) {}}, std::initializer_list< TER > ters={tecINVARIANT_FAILED, tefINVARIANT_FAILED}, Preclose const &preclose={}, TxAccount setTxAccount=TxAccount::None, std::source_location const &loc=std::source_location::current(), TER initialResult=tesSUCCESS)
Run a specific test case to put the ledger into a state that will be detected by an invariant.
static SLE::pointer makeLoanSle(UInt256 const &loanBrokerID, std::uint32_t loanSeq, AccountID const &borrower)
std::function< bool(test::jtx::Account const &a, test::jtx::Account const &b, test::jtx::Env &env)> Preclose
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
A transaction testing environment.
Definition Env.h:161
T current(T... args)
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:54
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ tefINVARIANT_FAILED
Definition TER.h:178
BaseUInt< 256 > UInt256
Definition base_uint.h:580
std::unique_ptr< Transactor > makeTransactor(ApplyContext &ctx)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
TERSubset< CanCvtToTER > TER
Definition TER.h:654
@ tecINVARIANT_FAILED
Definition TER.h:321
@ tesSUCCESS
Definition TER.h:250