2#include <test/jtx/PathSet.h>
4#include <xrpld/app/paths/AMMContext.h>
5#include <xrpld/app/paths/RippleCalc.h>
6#include <xrpld/app/paths/detail/Steps.h>
7#include <xrpld/core/Config.h>
9#include <xrpl/basics/contract.h>
10#include <xrpl/basics/safe_cast.h>
11#include <xrpl/ledger/PaymentSandbox.h>
12#include <xrpl/protocol/Feature.h>
13#include <xrpl/protocol/jss.h>
60 auto const useHigh = src.
id() > dst.
id();
61 return sle->isFlag(
trustFlag(flag, useHigh));
63 Throw<std::runtime_error>(
"No line in getTrustFlag");
99template <
class Iter,
class StepInfo,
class... Args>
108template <
class... Args>
110equal(Strand
const& strand, Args&&... args)
112 if (strand.size() !=
sizeof...(Args))
162 static_assert(safe_cast<size_t>(
SB::last) <=
sizeof(
decltype(
state_)) * 8,
"");
171 return state_ & (1 << safe_cast<int>(s));
177 for (
auto const s : sb)
188 for (
auto const s : sb)
225 template <
class Col,
class AccFactory,
class IssFactory,
class CurrencyFactory>
231 CurrencyFactory&& currencyF,
340 assert(!offererIndex || offererIndex < numAct);
349 constexpr size_t bufSize = 32;
352 for (
size_t id = 0;
id < numAct; ++id)
354 snprintf(buf, bufSize,
"A%zu",
id);
358 for (
size_t id = 0;
id < numCur; ++id)
361 snprintf(buf, bufSize,
"CC%zu",
id);
363 snprintf(buf, bufSize,
"C%zu",
id);
365 snprintf(buf, bufSize,
"%zu",
id);
376 for (
auto ai2 =
accounts.begin(); ai2 != aie; ++ai2)
382 env.
trust((*ai1)[cn](1'000'000), *ai2);
388 auto const& src = *ai1;
389 auto const& dst = *ai2;
390 env(
pay(src, dst, src[cn](500000)));
404 for (
auto takerPays = ious.
begin(), ie = ious.
end(); takerPays != ie; ++takerPays)
406 for (
auto takerGets = ious.
begin(); takerGets != ie; ++takerGets)
408 if (takerPays == takerGets)
410 auto const owner = offererIndex ?
accounts[*offererIndex] : takerGets->account;
411 if (owner.id() != takerGets->account.id())
412 env(
pay(takerGets->account, owner, (*takerGets)(1000)));
420 for (
auto const& iou : ious)
422 auto const owner = offererIndex ?
accounts[*offererIndex] : iou.account;
433 auto add = [&](
auto const& a) {
438 auto const b = (*sle)[sfBalance];
456 auto const sle = v.
read(k);
459 return (*sle)[sfBalance];
462 auto const sle = v.
read(k);
465 return (*sle)[sfBalance];
473 auto const b1 = xrpBalance(v1, ak);
474 auto const b2 = xrpBalance(v2, ak);
480 for (
auto ai2 =
accounts.begin(); ai2 != aie; ++ai2)
488 auto const b1 = lineBalance(v1, lk);
489 auto const b2 = lineBalance(v2, lk);
495 return diffs.
empty();
531 auto const resultSize = prefix.
size() + suffix.
size() + 2;
532 outerResult.
reserve(resultSize);
537 outerResult = prefix;
538 outer.emplace_into(outerResult, accF, issF, currencyF, existingAcc, existingCur, existingIss);
544 result = outerResult;
545 inner.
emplace_into(result, accF, issF, currencyF, existingAcc, existingCur, existingIss);
547 f(sendMax, deliver, result);
562 auto const alice =
Account(
"alice");
563 auto const bob =
Account(
"bob");
564 auto const carol =
Account(
"carol");
567 auto const USD = gw[
"USD"];
568 auto const EUR = gw[
"EUR"];
570 auto const eurC = EUR.currency;
571 auto const usdC = USD.currency;
579 auto test = [&,
this](
581 Issue const& deliver,
585 auto&&... expSteps) {
599 BEAST_EXPECT(
ter == expTer);
600 if (
sizeof...(expSteps) != 0)
605 Env env(*
this, features);
606 env.
fund(
XRP(10000), alice, bob, gw);
607 env.
trust(USD(1000), alice, bob);
608 env.
trust(EUR(1000), alice, bob);
609 env(
pay(gw, alice, EUR(100)));
650 Env env(*
this, features);
651 env.
fund(
XRP(10000), alice, bob, carol, gw);
655 env.
trust(USD(1000), alice, bob, carol);
658 env(
pay(gw, alice, USD(100)));
659 env(
pay(gw, carol, USD(100)));
662 test(env, USD,
std::nullopt,
STPath(),
tesSUCCESS, D{alice, gw, usdC}, D{gw, bob, usdC});
663 env.
trust(EUR(1000), alice, bob);
688 env.
trust(carol[
"USD"](1000), bob);
697 D{carol, bob, usdC});
816 Env env(*
this, features);
818 env.
fund(
XRP(10000), alice, bob, carol, gw);
819 env.
trust(USD(10000), alice, bob, carol);
820 env.
trust(EUR(10000), alice, bob, carol);
822 env(
pay(gw, bob, USD(100)));
823 env(
pay(gw, bob, EUR(100)));
825 env(
offer(bob,
XRP(100), USD(100)));
830 env(
pay(alice, carol, USD(100)),
831 path(~USD, ~EUR, ~USD),
838 Env env(*
this, features);
840 env.
trust(USD(1000), alice, bob);
841 env(
pay(gw, alice, USD(100)));
847 Env env(*
this, features);
848 env.
fund(
XRP(10000), alice, bob, gw);
849 env.
trust(USD(1000), alice, bob);
850 env(
pay(gw, alice, USD(100)));
872 Env env(*
this, features);
873 env.
fund(
XRP(10000), alice, bob, gw);
874 env.
trust(USD(1000), alice, bob);
875 env(
pay(gw, alice, USD(100)));
885 Env env(*
this, features);
886 env.
fund(
XRP(10000), alice, bob, gw);
888 env.
trust(USD(1000), alice, bob);
892 env(
pay(gw, alice, USD(100)));
911 BEAST_EXPECT(
equal(strand, D{alice, gw, usdC}));
916 Env env(*
this, features);
917 env.
fund(
XRP(10000), alice, bob, gw);
918 env.
trust(USD(1000), alice, bob);
919 env(
pay(gw, alice, USD(100)));
949 auto const alice =
Account(
"alice");
950 auto const bob =
Account(
"bob");
951 auto const carol =
Account(
"carol");
953 auto const USD = gw[
"USD"];
954 auto const EUR = gw[
"EUR"];
957 Env env(*
this, features);
958 env.
fund(
XRP(10000), alice, bob, gw);
960 env.
trust(USD(1000), alice, bob);
961 env.
trust(EUR(1000), alice, bob);
962 env.
trust(bob[
"USD"](1000), alice, gw);
963 env.
trust(bob[
"EUR"](1000), alice, gw);
980 env(
pay(alice, alice, EUR(1)),
988 Env env(*
this, features);
990 env.
fund(
XRP(10000), alice, bob, carol, gw);
991 env.
trust(USD(10000), alice, bob, carol);
993 env(
pay(gw, bob, USD(100)));
1003 Env env(*
this, features);
1005 env.
fund(
XRP(10000), alice, bob, carol, gw);
1006 env.
trust(USD(10000), alice, bob, carol);
1008 env(
pay(gw, bob, USD(100)));
1014 env(
pay(alice, carol,
XRP(100)),
1026 using namespace jtx;
1028 auto const alice =
Account(
"alice");
1029 auto const bob =
Account(
"bob");
1030 auto const carol =
Account(
"carol");
1031 auto const gw =
Account(
"gw");
1032 auto const USD = gw[
"USD"];
1033 auto const EUR = gw[
"EUR"];
1034 auto const CNY = gw[
"CNY"];
1037 Env env(*
this, features);
1039 env.
fund(
XRP(10000), alice, bob, carol, gw);
1040 env.
trust(USD(10000), alice, bob, carol);
1042 env(
pay(gw, bob, USD(100)));
1043 env(
pay(gw, alice, USD(100)));
1049 env(
pay(alice, carol, USD(100)),
1056 Env env(*
this, features);
1058 env.
fund(
XRP(10000), alice, bob, carol, gw);
1059 env.
trust(USD(10000), alice, bob, carol);
1060 env.
trust(EUR(10000), alice, bob, carol);
1061 env.
trust(CNY(10000), alice, bob, carol);
1063 env(
pay(gw, bob, USD(100)));
1064 env(
pay(gw, bob, EUR(100)));
1065 env(
pay(gw, bob, CNY(100)));
1072 env(
pay(alice, carol, CNY(100)),
1074 path(~USD, ~EUR, ~USD, ~CNY),
1084 using namespace jtx;
1086 auto const alice =
Account(
"alice");
1087 auto const bob =
Account(
"bob");
1088 auto const gw =
Account(
"gw");
1089 auto const USD = gw[
"USD"];
1091 Env env(*
this, features);
1092 env.
fund(
XRP(10000), alice, bob, gw);
1117 sb, noAccountAmount, deliver, dstAcc, srcAcc, pathSet,
std::nullopt, env.
app().
logs(), &inputs);
1122 sb, sendMax, noAccountAmount, dstAcc, srcAcc, pathSet,
std::nullopt, env.
app().
logs(), &inputs);
1135 using namespace jtx;
1143 testLoop(sa - featurePermissionedDEX);
1150BEAST_DEFINE_TESTSUITE(PayStrand, app,
xrpl);
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, std::optional< uint256 > const &domainID, Logs &l, Input const *const pInputs=nullptr)
ElementComboIter(STPathElement const *prev=nullptr)
bool const allowCompound_
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)
bool hasAny(std::initializer_list< SB > sb) const
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.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
std::shared_ptr< SLE const > le(Account const &account) const
Return an account root.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
void require(Args const &... args)
Check a set of requirements.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
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 trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
XRP_t const XRP
Converts to XRP Issue or STAmount.
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
STPathElement allpe(AccountID const &a, Issue const &iss)
Json::Value fclear(Account const &account, std::uint32_t off)
Remove account flag.
STPathElement cpe(Currency const &c)
FeatureBitset testable_amendments()
Json::Value fset(Account const &account, std::uint32_t on, std::uint32_t off=0)
Add and/or remove flag.
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
STPathElement ape(AccountID const &a)
bool bookStepEqual(Step const &step, xrpl::Book const &book)
STPathElement ipe(Issue const &iss)
bool getTrustFlag(jtx::Env const &env, jtx::Account const &src, jtx::Account const &dst, Currency const &cur, TrustFlag flag)
std::uint32_t trustFlag(TrustFlag f, bool useHigh)
bool strandEqualHelper(Iter i)
bool xrpEndpointStepEqual(Step const &step, AccountID const &acc)
bool directStepEqual(Step const &step, AccountID const &src, AccountID const &dst, Currency const ¤cy)
bool equal(std::unique_ptr< Step > const &s1, DirectStepInfo const &dsi)
STPathElement iape(AccountID const &account)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
constexpr std::uint32_t asfGlobalFreeze
constexpr std::uint32_t tfPassive
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Currency const & xrpCurrency()
XRP currency.
constexpr std::uint32_t tfNoRippleDirect
constexpr std::uint32_t tfSetfAuth
constexpr std::uint32_t asfRequireAuth
AccountID const & noAccount()
A placeholder for empty accounts.
AccountID const & xrpAccount()
Compute AccountID from public key.
constexpr std::uint32_t tfPartialPayment
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
constexpr std::uint32_t tfSetFreeze
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, std::optional< uint256 > const &domainID, beast::Journal j)
Create a Strand for the specified path.
A pair of SHAMap key and LedgerEntryType.
StateGuard(ExistingElementPool &p)
std::vector< jtx::Account > accounts
std::int64_t totalXRP(ReadView const &v, bool incRoot)
bool checkBalances(ReadView const &v1, ReadView const &v2)
ResetState getResetState() const
std::vector< xrpl::Currency > currencies
void resetTo(ResetState const &s)
jtx::Account getAccount(size_t id)
jtx::Account getAvailAccount()
void setupEnv(jtx::Env &env, size_t numAct, size_t numCur, std::optional< size_t > const &offererIndex)
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)
std::vector< std::string > currencyNames
xrpl::Currency getCurrency(size_t id)
xrpl::Currency getAvailCurrency()
void testRIPD1373(FeatureBitset features)
void testLoop(FeatureBitset features)
void run() override
Runs the suite.
void testToStrand(FeatureBitset features)
void testNoAccount(FeatureBitset features)