19#include <test/jtx/PathSet.h>
20#include <xrpld/app/paths/AMMContext.h>
21#include <xrpld/app/paths/Flow.h>
22#include <xrpld/app/paths/RippleCalc.h>
23#include <xrpld/app/paths/detail/Steps.h>
24#include <xrpld/core/Config.h>
25#include <xrpld/ledger/ApplyViewImpl.h>
26#include <xrpld/ledger/PaymentSandbox.h>
27#include <xrpld/ledger/Sandbox.h>
28#include <xrpl/basics/contract.h>
29#include <xrpl/basics/safe_cast.h>
30#include <xrpl/protocol/Feature.h>
31#include <xrpl/protocol/jss.h>
81 auto const useHigh = src.
id() > dst.
id();
82 return sle->isFlag(
trustFlag(flag, useHigh));
84 Throw<std::runtime_error>(
"No line in getTrustFlag");
120template <
class Iter,
class StepInfo,
class... Args>
124 if (!
equal(*i, std::forward<StepInfo>(si)))
129template <
class... Args>
131equal(Strand
const& strand, Args&&... args)
133 if (strand.size() !=
sizeof...(Args))
199 return state_ & (1 << safe_cast<int>(s));
205 for (
auto const s : sb)
216 for (
auto const s : sb)
263 class CurrencyFactory>
269 CurrencyFactory&& currencyF,
383 assert(!offererIndex || offererIndex < numAct);
392 constexpr size_t bufSize = 32;
395 for (
size_t id = 0;
id < numAct; ++id)
397 snprintf(buf, bufSize,
"A%zu",
id);
401 for (
size_t id = 0;
id < numCur; ++id)
404 snprintf(buf, bufSize,
"CC%zu",
id);
406 snprintf(buf, bufSize,
"C%zu",
id);
408 snprintf(buf, bufSize,
"%zu",
id);
420 for (
auto ai2 =
accounts.begin(); ai2 != aie; ++ai2)
426 env.
trust((*ai1)[cn](1'000'000), *ai2);
432 auto const& src = *ai1;
433 auto const& dst = *ai2;
434 env(
pay(src, dst, src[cn](500000)));
448 for (
auto takerPays = ious.
begin(), ie = ious.
end(); takerPays != ie;
451 for (
auto takerGets = ious.
begin(); takerGets != ie; ++takerGets)
453 if (takerPays == takerGets)
456 offererIndex ?
accounts[*offererIndex] : takerGets->account;
457 if (owner.id() != takerGets->account.id())
458 env(
pay(takerGets->account, owner, (*takerGets)(1000)));
460 env(
offer(owner, (*takerPays)(1000), (*takerGets)(1000)),
467 for (
auto const& iou : ious)
470 offererIndex ?
accounts[*offererIndex] : iou.account;
481 auto add = [&](
auto const& a) {
486 auto const b = (*sle)[sfBalance];
504 auto const sle = v.
read(k);
507 return (*sle)[sfBalance];
510 auto const sle = v.
read(k);
513 return (*sle)[sfBalance];
522 auto const b1 = xrpBalance(v1, ak);
523 auto const b2 = xrpBalance(v2, ak);
529 for (
auto ai2 =
accounts.begin(); ai2 != aie; ++ai2)
537 auto const b1 = lineBalance(v1, lk);
538 auto const b2 = lineBalance(v2, lk);
544 return diffs.
empty();
576 prefix.
empty() ? nullptr : &prefix.
back();
581 auto const resultSize = prefix.
size() + suffix.
size() + 2;
582 outerResult.
reserve(resultSize);
587 outerResult = prefix;
601 result = outerResult;
611 f(sendMax, deliver, result);
626 auto const alice =
Account(
"alice");
627 auto const bob =
Account(
"bob");
628 auto const carol =
Account(
"carol");
631 auto const USD = gw[
"USD"];
632 auto const EUR = gw[
"EUR"];
634 auto const eurC = EUR.currency;
635 auto const usdC = USD.currency;
643 auto test = [&,
this](
645 Issue const& deliver,
649 auto&&... expSteps) {
662 BEAST_EXPECT(
ter == expTer);
663 if (
sizeof...(expSteps) != 0)
665 strand,
std::forward<
decltype(expSteps)>(expSteps)...));
669 Env env(*
this, features);
670 env.
fund(
XRP(10000), alice, bob, gw);
671 env.
trust(USD(1000), alice, bob);
672 env.
trust(EUR(1000), alice, bob);
673 env(
pay(gw, alice, EUR(100)));
713 Env env(*
this, features);
714 env.
fund(
XRP(10000), alice, bob, carol, gw);
718 env.
trust(USD(1000), alice, bob, carol);
721 env(
pay(gw, alice, USD(100)));
722 env(
pay(gw, carol, USD(100)));
733 env.
trust(EUR(1000), alice, bob);
758 env.
trust(carol[
"USD"](1000), bob);
766 B{USD, carol[
"USD"]},
767 D{carol, bob, usdC});
903 Env env(*
this, features);
905 env.
fund(
XRP(10000), alice, bob, carol, gw);
906 env.
trust(USD(10000), alice, bob, carol);
907 env.
trust(EUR(10000), alice, bob, carol);
909 env(
pay(gw, bob, USD(100)));
910 env(
pay(gw, bob, EUR(100)));
912 env(
offer(bob,
XRP(100), USD(100)));
917 env(
pay(alice, carol, USD(100)),
918 path(~USD, ~EUR, ~USD),
925 Env env(*
this, features);
927 env.
trust(USD(1000), alice, bob);
928 env(
pay(gw, alice, USD(100)));
934 Env env(*
this, features);
935 env.
fund(
XRP(10000), alice, bob, gw);
936 env.
trust(USD(1000), alice, bob);
937 env(
pay(gw, alice, USD(100)));
959 Env env(*
this, features);
960 env.
fund(
XRP(10000), alice, bob, gw);
961 env.
trust(USD(1000), alice, bob);
962 env(
pay(gw, alice, USD(100)));
972 Env env(*
this, features);
973 env.
fund(
XRP(10000), alice, bob, gw);
975 env.
trust(USD(1000), alice, bob);
979 env(
pay(gw, alice, USD(100)));
997 BEAST_EXPECT(
equal(strand, D{alice, gw, usdC}));
1002 Env env(*
this, features);
1003 env.
fund(
XRP(10000), alice, bob, gw);
1004 env.
trust(USD(1000), alice, bob);
1005 env(
pay(gw, alice, USD(100)));
1035 using namespace jtx;
1038 auto const alice =
Account(
"alice");
1039 auto const bob =
Account(
"bob");
1040 auto const carol =
Account(
"carol");
1041 auto const gw =
Account(
"gw");
1042 auto const USD = gw[
"USD"];
1043 auto const EUR = gw[
"EUR"];
1046 Env env(*
this, features);
1047 env.
fund(
XRP(10000), alice, bob, gw);
1049 env.
trust(USD(1000), alice, bob);
1050 env.
trust(EUR(1000), alice, bob);
1051 env.
trust(bob[
"USD"](1000), alice, gw);
1052 env.
trust(bob[
"EUR"](1000), alice, gw);
1057 env(
offer(bob, bob[
"USD"](100), bob[
"EUR"](100)),
1061 Path const p = [&] {
1070 env(
pay(alice, alice, EUR(1)),
1078 Env env(*
this, features);
1080 env.
fund(
XRP(10000), alice, bob, carol, gw);
1081 env.
trust(USD(10000), alice, bob, carol);
1083 env(
pay(gw, bob, USD(100)));
1089 env(
pay(alice, carol,
XRP(100)),
1096 Env env(*
this, features);
1098 env.
fund(
XRP(10000), alice, bob, carol, gw);
1099 env.
trust(USD(10000), alice, bob, carol);
1101 env(
pay(gw, bob, USD(100)));
1107 env(
pay(alice, carol,
XRP(100)),
1119 using namespace jtx;
1121 auto const alice =
Account(
"alice");
1122 auto const bob =
Account(
"bob");
1123 auto const carol =
Account(
"carol");
1124 auto const gw =
Account(
"gw");
1125 auto const USD = gw[
"USD"];
1126 auto const EUR = gw[
"EUR"];
1127 auto const CNY = gw[
"CNY"];
1130 Env env(*
this, features);
1132 env.
fund(
XRP(10000), alice, bob, carol, gw);
1133 env.
trust(USD(10000), alice, bob, carol);
1135 env(
pay(gw, bob, USD(100)));
1136 env(
pay(gw, alice, USD(100)));
1142 env(
pay(alice, carol, USD(100)),
1149 Env env(*
this, features);
1151 env.
fund(
XRP(10000), alice, bob, carol, gw);
1152 env.
trust(USD(10000), alice, bob, carol);
1153 env.
trust(EUR(10000), alice, bob, carol);
1154 env.
trust(CNY(10000), alice, bob, carol);
1156 env(
pay(gw, bob, USD(100)));
1157 env(
pay(gw, bob, EUR(100)));
1158 env(
pay(gw, bob, CNY(100)));
1165 env(
pay(alice, carol, CNY(100)),
1167 path(~USD, ~EUR, ~USD, ~CNY),
1177 using namespace jtx;
1179 auto const alice =
Account(
"alice");
1180 auto const bob =
Account(
"bob");
1181 auto const gw =
Account(
"gw");
1182 auto const USD = gw[
"USD"];
1184 Env env(*
this, features);
1185 env.
fund(
XRP(10000), alice, bob, gw);
1256 using namespace jtx;
1271BEAST_DEFINE_TESTSUITE(PayStrand, app,
ripple);
testcase_t testcase
Memberspace for declaring test cases.
void fail(String const &reason, char const *file, int line)
Record a failure.
Maintains AMM info per overall payment engine execution and individual iteration.
A currency issued by an account.
beast::Journal journal(std::string const &name)
A wrapper which makes credits unavailable to balances.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
Issue const & issue() const
static Output rippleCalculate(PaymentSandbox &view, STAmount const &saMaxAmountReq, STAmount const &saDstAmountReq, AccountID const &uDstAccountID, AccountID const &uSrcAccountID, STPathSet const &spsPaths, Logs &l, Input const *const pInputs=nullptr)
bool hasAny(std::initializer_list< SB > sb) const
void emplace_into(Col &col, AccFactory &&accF, IssFactory &&issF, CurrencyFactory &¤cyF, std::optional< AccountID > const &existingAcc, std::optional< Currency > const &existingCur, std::optional< AccountID > const &existingIss)
ElementComboIter(STPathElement const *prev=nullptr)
bool const allowCompound_
STPathElement const * prev_
size_t count(std::initializer_list< SB > sb) const
Path & push_back(Issue const &iss)
Immutable cryptographic account descriptor.
AccountID id() const
Returns the Account ID.
A transaction testing environment.
void require(Args const &... args)
Check a set of requirements.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
std::shared_ptr< SLE const > le(Account const &account) const
Return an account root.
Set Paths, SendMax on a JTx.
Sets the SendMax on a JTx.
Set the expected result code for a JTx The test will fail if the code doesn't match.
T emplace_back(T... args)
Keylet line(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
Keylet account(AccountID const &id) noexcept
AccountID root.
Json::Value fclear(Account const &account, std::uint32_t off)
Remove account flag.
Json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
STPathElement cpe(Currency const &c)
STPathElement allpe(AccountID const &a, Issue const &iss)
XRP_t const XRP
Converts to XRP Issue or STAmount.
FeatureBitset supported_amendments()
bool xrpEndpointStepEqual(Step const &step, AccountID const &acc)
bool getTrustFlag(jtx::Env const &env, jtx::Account const &src, jtx::Account const &dst, Currency const &cur, TrustFlag flag)
STPathElement iape(AccountID const &account)
STPathElement ape(AccountID const &a)
std::uint32_t trustFlag(TrustFlag f, bool useHigh)
bool bookStepEqual(Step const &step, ripple::Book const &book)
bool strandEqualHelper(Iter i)
bool equal(std::unique_ptr< Step > const &s1, DirectStepInfo const &dsi)
STPathElement ipe(Issue const &iss)
bool directStepEqual(Step const &step, AccountID const &src, AccountID const &dst, Currency const ¤cy)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
AccountID const & noAccount()
A placeholder for empty accounts.
constexpr std::uint32_t asfGlobalFreeze
AccountID const & xrpAccount()
Compute AccountID from public key.
std::pair< TER, Strand > toStrand(ReadView const &view, AccountID const &src, AccountID const &dst, Issue const &deliver, std::optional< Quality > const &limitQuality, std::optional< Issue > const &sendMaxIssue, STPath const &path, bool ownerPaysTransferFee, OfferCrossing offerCrossing, AMMContext &ammContext, beast::Journal j)
Create a Strand for the specified path.
constexpr std::uint32_t tfPassive
constexpr std::uint32_t tfPartialPayment
constexpr std::uint32_t tfSetfAuth
Currency const & xrpCurrency()
XRP currency.
constexpr std::uint32_t tfNoRippleDirect
constexpr std::uint32_t asfRequireAuth
constexpr std::uint32_t tfSetFreeze
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
A pair of SHAMap key and LedgerEntryType.
StateGuard(ExistingElementPool &p)
std::int64_t totalXRP(ReadView const &v, bool incRoot)
void for_each_element_pair(STAmount const &sendMax, STAmount const &deliver, std::vector< STPathElement > const &prefix, std::vector< STPathElement > const &suffix, std::optional< AccountID > const &existingAcc, std::optional< Currency > const &existingCur, std::optional< AccountID > const &existingIss, F &&f)
jtx::Account getAvailAccount()
ripple::Currency getCurrency(size_t id)
ResetState getResetState() const
ripple::Currency getAvailCurrency()
bool checkBalances(ReadView const &v1, ReadView const &v2)
std::vector< jtx::Account > accounts
std::vector< std::string > currencyNames
std::vector< ripple::Currency > currencies
void resetTo(ResetState const &s)
jtx::Account getAccount(size_t id)
void setupEnv(jtx::Env &env, size_t numAct, size_t numCur, std::optional< size_t > const &offererIndex)
void testToStrand(FeatureBitset features)
void testNoAccount(FeatureBitset features)
void run() override
Runs the suite.
void testLoop(FeatureBitset features)
void testRIPD1373(FeatureBitset features)