rippled
Loading...
Searching...
No Matches
TestHelpers.cpp
1#include <test/jtx/TestHelpers.h>
2#include <test/jtx/offer.h>
3#include <test/jtx/owners.h>
4
5#include <xrpl/protocol/TxFlags.h>
6
7namespace xrpl {
8namespace test {
9namespace jtx {
10
11// Functions used in debugging
13getAccountOffers(Env& env, AccountID const& acct, bool current)
14{
15 Json::Value jv;
16 jv[jss::account] = to_string(acct);
17 return env.rpc("json", "account_offers", to_string(jv))[jss::result];
18}
19
21getAccountLines(Env& env, AccountID const& acctId)
22{
23 Json::Value jv;
24 jv[jss::account] = to_string(acctId);
25 return env.rpc("json", "account_lines", to_string(jv))[jss::result];
26}
27
28bool
29checkArraySize(Json::Value const& val, unsigned int size)
30{
31 return val.isArray() && val.size() == size;
32}
33
35ownerCount(Env const& env, Account const& account)
36{
37 return env.ownerCount(account);
38}
39
40/* Path finding */
41/******************************************************************************/
42void
43stpath_append_one(STPath& st, Account const& account)
44{
46}
47
48void
50{
51 st.push_back(pe);
52}
53
54bool
55equal(STAmount const& sa1, STAmount const& sa2)
56{
57 return sa1 == sa2 && sa1.issue().account == sa2.issue().account;
58}
59
60// Issue path element
62IPE(Issue const& iss)
63{
64 return STPathElement(
66 xrpAccount(),
67 iss.currency,
68 iss.account);
69}
70
71/******************************************************************************/
72
74txfee(Env const& env, std::uint16_t n)
75{
76 return env.current()->fees().base * n;
77}
78
80xrpMinusFee(Env const& env, std::int64_t xrpAmount)
81{
82 auto feeDrops = env.current()->fees().base;
83 return drops(dropsPerXRP * xrpAmount - feeDrops);
84};
85
86[[nodiscard]] bool
87expectHolding(Env& env, AccountID const& account, STAmount const& value, bool defaultLimits)
88{
89 if (auto const sle = env.le(keylet::line(account, value.issue())))
90 {
91 Issue const issue = value.issue();
92 bool const accountLow = account < issue.account;
93
94 bool expectDefaultTrustLine = true;
95 if (defaultLimits)
96 {
97 STAmount low{issue};
98 STAmount high{issue};
99
100 low.setIssuer(accountLow ? account : issue.account);
101 high.setIssuer(accountLow ? issue.account : account);
102
103 expectDefaultTrustLine =
104 sle->getFieldAmount(sfLowLimit) == low && sle->getFieldAmount(sfHighLimit) == high;
105 }
106
107 auto amount = sle->getFieldAmount(sfBalance);
108 amount.setIssuer(value.issue().account);
109 if (!accountLow)
110 amount.negate();
111 return amount == value && expectDefaultTrustLine;
112 }
113 return false;
114}
115
116[[nodiscard]] bool
117expectHolding(Env& env, AccountID const& account, None const&, Issue const& issue)
118{
119 return !env.le(keylet::line(account, issue));
120}
121
122[[nodiscard]] bool
123expectHolding(Env& env, AccountID const& account, None const&, MPTIssue const& mptIssue)
124{
125 return !env.le(keylet::mptoken(mptIssue.getMptID(), account));
126}
127
128[[nodiscard]] bool
129expectHolding(Env& env, AccountID const& account, None const& value)
130{
131 return std::visit(
132 [&](auto const& issue) { return expectHolding(env, account, value, issue); },
133 value.asset.value());
134}
135
136[[nodiscard]] bool
138 Env& env,
139 AccountID const& account,
140 std::uint16_t size,
141 std::vector<Amounts> const& toMatch)
142{
143 std::uint16_t cnt = 0;
144 std::uint16_t matched = 0;
145 forEachItem(*env.current(), account, [&](std::shared_ptr<SLE const> const& sle) {
146 if (!sle)
147 return false;
148 if (sle->getType() == ltOFFER)
149 {
150 ++cnt;
151 if (std::find_if(toMatch.begin(), toMatch.end(), [&](auto const& a) {
152 return a.in == sle->getFieldAmount(sfTakerPays) &&
153 a.out == sle->getFieldAmount(sfTakerGets);
154 }) != toMatch.end())
155 ++matched;
156 }
157 return true;
158 });
159 return size == cnt && matched == toMatch.size();
160}
161
163ledgerEntryRoot(Env& env, Account const& acct)
164{
165 Json::Value jvParams;
166 jvParams[jss::ledger_index] = "current";
167 jvParams[jss::account_root] = acct.human();
168 return env.rpc("json", "ledger_entry", to_string(jvParams))[jss::result];
169}
170
173 Env& env,
174 Account const& acct_a,
175 Account const& acct_b,
176 std::string const& currency)
177{
178 Json::Value jvParams;
179 jvParams[jss::ledger_index] = "current";
180 jvParams[jss::ripple_state][jss::currency] = currency;
181 jvParams[jss::ripple_state][jss::accounts] = Json::arrayValue;
182 jvParams[jss::ripple_state][jss::accounts].append(acct_a.human());
183 jvParams[jss::ripple_state][jss::accounts].append(acct_b.human());
184 return env.rpc("json", "ledger_entry", to_string(jvParams))[jss::result];
185}
186
188accountBalance(Env& env, Account const& acct)
189{
190 auto const jrr = ledgerEntryRoot(env, acct);
191 return jrr[jss::node][sfBalance.fieldName];
192}
193
194[[nodiscard]] bool
195expectLedgerEntryRoot(Env& env, Account const& acct, STAmount const& expectedValue)
196{
197 return accountBalance(env, acct) == to_string(expectedValue.xrp());
198}
199
200/* Payment Channel */
201/******************************************************************************/
202namespace paychan {
203
206 AccountID const& account,
207 AccountID const& to,
208 STAmount const& amount,
209 NetClock::duration const& settleDelay,
210 PublicKey const& pk,
211 std::optional<NetClock::time_point> const& cancelAfter,
212 std::optional<std::uint32_t> const& dstTag)
213{
214 Json::Value jv;
215 jv[jss::TransactionType] = jss::PaymentChannelCreate;
216 jv[jss::Account] = to_string(account);
217 jv[jss::Destination] = to_string(to);
218 jv[jss::Amount] = amount.getJson(JsonOptions::none);
219 jv[jss::SettleDelay] = settleDelay.count();
220 jv[sfPublicKey.fieldName] = strHex(pk.slice());
221 if (cancelAfter)
222 jv[sfCancelAfter.fieldName] = cancelAfter->time_since_epoch().count();
223 if (dstTag)
224 jv[sfDestinationTag.fieldName] = *dstTag;
225 return jv;
226}
227
230 AccountID const& account,
231 uint256 const& channel,
232 STAmount const& amount,
234{
235 Json::Value jv;
236 jv[jss::TransactionType] = jss::PaymentChannelFund;
237 jv[jss::Account] = to_string(account);
238 jv[sfChannel.fieldName] = to_string(channel);
239 jv[jss::Amount] = amount.getJson(JsonOptions::none);
240 if (expiration)
241 jv[sfExpiration.fieldName] = expiration->time_since_epoch().count();
242 return jv;
243}
244
247 AccountID const& account,
248 uint256 const& channel,
251 std::optional<Slice> const& signature,
252 std::optional<PublicKey> const& pk)
253{
254 Json::Value jv;
255 jv[jss::TransactionType] = jss::PaymentChannelClaim;
256 jv[jss::Account] = to_string(account);
257 jv["Channel"] = to_string(channel);
258 if (amount)
259 jv[jss::Amount] = amount->getJson(JsonOptions::none);
260 if (balance)
261 jv["Balance"] = balance->getJson(JsonOptions::none);
262 if (signature)
263 jv["Signature"] = strHex(*signature);
264 if (pk)
265 jv["PublicKey"] = strHex(pk->slice());
266 return jv;
267}
268
270channel(AccountID const& account, AccountID const& dst, std::uint32_t seqProxyValue)
271{
272 auto const k = keylet::payChan(account, dst, seqProxyValue);
273 return k.key;
274}
275
277channelBalance(ReadView const& view, uint256 const& chan)
278{
279 auto const slep = view.read({ltPAYCHAN, chan});
280 if (!slep)
281 return XRPAmount{-1};
282 return (*slep)[sfBalance];
283}
284
285bool
286channelExists(ReadView const& view, uint256 const& chan)
287{
288 auto const slep = view.read({ltPAYCHAN, chan});
289 return bool(slep);
290}
291
292} // namespace paychan
293
294/* Crossing Limits */
295/******************************************************************************/
296
297void
298n_offers(Env& env, std::size_t n, Account const& account, STAmount const& in, STAmount const& out)
299{
300 auto const ownerCount = env.le(account)->getFieldU32(sfOwnerCount);
301 for (std::size_t i = 0; i < n; i++)
302 {
303 env(offer(account, in, out));
304 env.close();
305 }
306 env.require(owners(account, ownerCount + n));
307}
308
309/* Pay Strand */
310/***************************************************************/
311
312// Currency path element
318
319// All path element
329
330/* LoanBroker */
331/******************************************************************************/
332
333namespace loanBroker {
334
336set(AccountID const& account, uint256 const& vaultId, uint32_t flags)
337{
338 Json::Value jv;
339 jv[sfTransactionType] = jss::LoanBrokerSet;
340 jv[sfAccount] = to_string(account);
341 jv[sfVaultID] = to_string(vaultId);
342 jv[sfFlags] = flags;
343 return jv;
344}
345
347del(AccountID const& account, uint256 const& brokerID, uint32_t flags)
348{
349 Json::Value jv;
350 jv[sfTransactionType] = jss::LoanBrokerDelete;
351 jv[sfAccount] = to_string(account);
352 jv[sfLoanBrokerID] = to_string(brokerID);
353 jv[sfFlags] = flags;
354 return jv;
355}
356
359 AccountID const& account,
360 uint256 const& brokerID,
361 STAmount const& amount,
362 uint32_t flags)
363{
364 Json::Value jv;
365 jv[sfTransactionType] = jss::LoanBrokerCoverDeposit;
366 jv[sfAccount] = to_string(account);
367 jv[sfLoanBrokerID] = to_string(brokerID);
368 jv[sfAmount] = amount.getJson(JsonOptions::none);
369 jv[sfFlags] = flags;
370 return jv;
371}
372
375 AccountID const& account,
376 uint256 const& brokerID,
377 STAmount const& amount,
378 uint32_t flags)
379{
380 Json::Value jv;
381 jv[sfTransactionType] = jss::LoanBrokerCoverWithdraw;
382 jv[sfAccount] = to_string(account);
383 jv[sfLoanBrokerID] = to_string(brokerID);
384 jv[sfAmount] = amount.getJson(JsonOptions::none);
385 jv[sfFlags] = flags;
386 return jv;
387}
388
391{
392 Json::Value jv;
393 jv[sfTransactionType] = jss::LoanBrokerCoverClawback;
394 jv[sfAccount] = to_string(account);
395 jv[sfFlags] = flags;
396 return jv;
397}
398
399} // namespace loanBroker
400
401/* Loan */
402/******************************************************************************/
403namespace loan {
404
406set(AccountID const& account,
407 uint256 const& loanBrokerID,
408 Number principalRequested,
410{
411 Json::Value jv;
412 jv[sfTransactionType] = jss::LoanSet;
413 jv[sfAccount] = to_string(account);
414 jv[sfLoanBrokerID] = to_string(loanBrokerID);
415 jv[sfPrincipalRequested] = to_string(principalRequested);
416 jv[sfFlags] = flags;
417 return jv;
418}
419
421manage(AccountID const& account, uint256 const& loanID, std::uint32_t flags)
422{
423 Json::Value jv;
424 jv[sfTransactionType] = jss::LoanManage;
425 jv[sfAccount] = to_string(account);
426 jv[sfLoanID] = to_string(loanID);
427 jv[sfFlags] = flags;
428 return jv;
429}
430
432del(AccountID const& account, uint256 const& loanID, std::uint32_t flags)
433{
434 Json::Value jv;
435 jv[sfTransactionType] = jss::LoanDelete;
436 jv[sfAccount] = to_string(account);
437 jv[sfLoanID] = to_string(loanID);
438 jv[sfFlags] = flags;
439 return jv;
440}
441
443pay(AccountID const& account, uint256 const& loanID, STAmount const& amount, std::uint32_t flags)
444{
445 Json::Value jv;
446 jv[sfTransactionType] = jss::LoanPay;
447 jv[sfAccount] = to_string(account);
448 jv[sfLoanID] = to_string(loanID);
449 jv[sfAmount] = amount.getJson();
450 jv[sfFlags] = flags;
451 return jv;
452}
453
454} // namespace loan
455} // namespace jtx
456} // namespace test
457} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
bool isArray() const
Value & append(Value const &value)
Append value to array at the end.
UInt size() const
Number of values in array or object.
constexpr value_type const & value() const
Definition Asset.h:154
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
AccountID account
Definition Issue.h:16
constexpr MPTID const & getMptID() const
Definition MPTIssue.h:26
Number is a floating point type that can represent a wide range of values.
Definition Number.h:207
A public key.
Definition PublicKey.h:42
Slice slice() const noexcept
Definition PublicKey.h:103
A view into a ledger.
Definition ReadView.h:31
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
Issue const & issue() const
Definition STAmount.h:470
XRPAmount xrp() const
Definition STAmount.cpp:257
void push_back(STPathElement const &e)
Definition STPathSet.h:382
Immutable cryptographic account descriptor.
Definition Account.h:19
std::string const & human() const
Returns the human readable public key.
Definition Account.h:94
A transaction testing environment.
Definition Env.h:121
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:100
std::uint32_t ownerCount(Account const &account) const
Return the number of objects owned by an account.
Definition Env.cpp:233
std::shared_ptr< SLE const > le(Account const &account) const
Return an account root.
Definition Env.cpp:251
Json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition Env.h:803
void require(Args const &... args)
Check a set of requirements.
Definition Env.h:544
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:328
A balance matches.
Definition balance.h:19
Set Expiration on a JTx.
Match set account flags.
Definition flags.h:108
Match the number of items in the account's owner directory.
Definition owners.h:52
T is_same_v
@ arrayValue
array value (ordered list)
Definition json_value.h:25
Keylet payChan(AccountID const &src, AccountID const &dst, std::uint32_t seq) noexcept
A PaymentChannel.
Definition Indexes.cpp:357
Keylet line(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:220
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:486
Json::Value coverDeposit(AccountID const &account, uint256 const &brokerID, STAmount const &amount, uint32_t flags)
Json::Value coverWithdraw(AccountID const &account, uint256 const &brokerID, STAmount const &amount, uint32_t flags)
Json::Value del(AccountID const &account, uint256 const &brokerID, uint32_t flags)
Json::Value coverClawback(AccountID const &account, std::uint32_t flags)
Json::Value pay(AccountID const &account, uint256 const &loanID, STAmount const &amount, std::uint32_t flags)
Json::Value manage(AccountID const &account, uint256 const &loanID, std::uint32_t flags)
Json::Value del(AccountID const &account, uint256 const &loanID, std::uint32_t flags)
STAmount channelBalance(ReadView const &view, uint256 const &chan)
uint256 channel(AccountID const &account, AccountID const &dst, std::uint32_t seqProxyValue)
Json::Value claim(AccountID const &account, uint256 const &channel, std::optional< STAmount > const &balance, std::optional< STAmount > const &amount, std::optional< Slice > const &signature, std::optional< PublicKey > const &pk)
Json::Value fund(AccountID const &account, uint256 const &channel, STAmount const &amount, std::optional< NetClock::time_point > const &expiration)
bool channelExists(ReadView const &view, uint256 const &chan)
Json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount, NetClock::duration const &settleDelay, PublicKey const &pk, std::optional< NetClock::time_point > const &cancelAfter, std::optional< std::uint32_t > const &dstTag)
bool expectLedgerEntryRoot(Env &env, Account const &acct, STAmount const &expectedValue)
Json::Value getAccountOffers(Env &env, AccountID const &acct, bool current)
PrettyAmount xrpMinusFee(Env const &env, std::int64_t xrpAmount)
bool expectOffers(Env &env, AccountID const &account, std::uint16_t size, std::vector< Amounts > const &toMatch)
bool expectHolding(Env &env, AccountID const &account, STAmount const &value, bool defaultLimits)
Json::Value ledgerEntryState(Env &env, Account const &acct_a, Account const &acct_b, std::string const &currency)
std::uint32_t ownerCount(Env const &env, Account const &account)
XRPAmount txfee(Env const &env, std::uint16_t n)
STPathElement allPathElements(AccountID const &a, Issue const &iss)
Json::Value accountBalance(Env &env, Account const &acct)
STPathElement IPE(Issue const &iss)
STPathElement cpe(Currency const &c)
Json::Value ledgerEntryRoot(Env &env, Account const &acct)
bool equal(STAmount const &sa1, STAmount const &sa2)
constexpr XRPAmount dropsPerXRP
auto const amount
bool checkArraySize(Json::Value const &val, unsigned int size)
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
void stpath_append_one(STPath &st, Account const &account)
void n_offers(Env &env, std::size_t n, Account const &account, STAmount const &in, STAmount const &out)
Json::Value getAccountLines(Env &env, AccountID const &acctId)
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:10
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:600
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:10
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items in the given directory.
Definition View.cpp:661
@ current
This was a new validation and was added.
AccountID const & xrpAccount()
Compute AccountID from public key.
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...
T visit(T... args)