19#include <test/jtx/PathSet.h>
21#include <xrpld/app/paths/AMMContext.h>
22#include <xrpld/app/paths/RippleCalc.h>
23#include <xrpld/app/paths/detail/Steps.h>
24#include <xrpld/core/Config.h>
26#include <xrpl/basics/contract.h>
27#include <xrpl/basics/safe_cast.h>
28#include <xrpl/ledger/PaymentSandbox.h>
29#include <xrpl/protocol/Feature.h>
30#include <xrpl/protocol/jss.h>
82 auto const useHigh = src.
id() > dst.
id();
83 return sle->isFlag(
trustFlag(flag, useHigh));
85 Throw<std::runtime_error>(
"No line in getTrustFlag");
121template <
class Iter,
class StepInfo,
class... Args>
130template <
class... Args>
132equal(Strand
const& strand, Args&&... args)
134 if (strand.size() !=
sizeof...(Args))
200 return state_ & (1 << safe_cast<int>(s));
206 for (
auto const s : sb)
217 for (
auto const s : sb)
264 class CurrencyFactory>
270 CurrencyFactory&& currencyF,
384 assert(!offererIndex || offererIndex < numAct);
393 constexpr size_t bufSize = 32;
396 for (
size_t id = 0;
id < numAct; ++id)
398 snprintf(buf, bufSize,
"A%zu",
id);
402 for (
size_t id = 0;
id < numCur; ++id)
405 snprintf(buf, bufSize,
"CC%zu",
id);
407 snprintf(buf, bufSize,
"C%zu",
id);
409 snprintf(buf, bufSize,
"%zu",
id);
421 for (
auto ai2 =
accounts.begin(); ai2 != aie; ++ai2)
427 env.
trust((*ai1)[cn](1'000'000), *ai2);
433 auto const& src = *ai1;
434 auto const& dst = *ai2;
435 env(
pay(src, dst, src[cn](500000)));
449 for (
auto takerPays = ious.
begin(), ie = ious.
end(); takerPays != ie;
452 for (
auto takerGets = ious.
begin(); takerGets != ie; ++takerGets)
454 if (takerPays == takerGets)
457 offererIndex ?
accounts[*offererIndex] : takerGets->account;
458 if (owner.id() != takerGets->account.id())
459 env(
pay(takerGets->account, owner, (*takerGets)(1000)));
461 env(
offer(owner, (*takerPays)(1000), (*takerGets)(1000)),
468 for (
auto const& iou : ious)
471 offererIndex ?
accounts[*offererIndex] : iou.account;
482 auto add = [&](
auto const& a) {
487 auto const b = (*sle)[sfBalance];
505 auto const sle = v.
read(k);
508 return (*sle)[sfBalance];
511 auto const sle = v.
read(k);
514 return (*sle)[sfBalance];
523 auto const b1 = xrpBalance(v1, ak);
524 auto const b2 = xrpBalance(v2, ak);
530 for (
auto ai2 =
accounts.begin(); ai2 != aie; ++ai2)
538 auto const b1 = lineBalance(v1, lk);
539 auto const b2 = lineBalance(v2, lk);
545 return diffs.
empty();
577 prefix.
empty() ? nullptr : &prefix.
back();
582 auto const resultSize = prefix.
size() + suffix.
size() + 2;
583 outerResult.
reserve(resultSize);
588 outerResult = prefix;
602 result = outerResult;
612 f(sendMax, deliver, result);
627 auto const alice =
Account(
"alice");
628 auto const bob =
Account(
"bob");
629 auto const carol =
Account(
"carol");
632 auto const USD = gw[
"USD"];
633 auto const EUR = gw[
"EUR"];
635 auto const eurC = EUR.currency;
636 auto const usdC = USD.currency;
644 auto test = [&,
this](
646 Issue const& deliver,
650 auto&&... expSteps) {
664 BEAST_EXPECT(
ter == expTer);
665 if (
sizeof...(expSteps) != 0)
667 strand,
std::forward<
decltype(expSteps)>(expSteps)...));
671 Env env(*
this, features);
672 env.
fund(
XRP(10000), alice, bob, gw);
673 env.
trust(USD(1000), alice, bob);
674 env.
trust(EUR(1000), alice, bob);
675 env(
pay(gw, alice, EUR(100)));
717 Env env(*
this, features);
718 env.
fund(
XRP(10000), alice, bob, carol, gw);
722 env.
trust(USD(1000), alice, bob, carol);
725 env(
pay(gw, alice, USD(100)));
726 env(
pay(gw, carol, USD(100)));
737 env.
trust(EUR(1000), alice, bob);
762 env.
trust(carol[
"USD"](1000), bob);
771 D{carol, bob, usdC});
910 Env env(*
this, features);
912 env.
fund(
XRP(10000), alice, bob, carol, gw);
913 env.
trust(USD(10000), alice, bob, carol);
914 env.
trust(EUR(10000), alice, bob, carol);
916 env(
pay(gw, bob, USD(100)));
917 env(
pay(gw, bob, EUR(100)));
919 env(
offer(bob,
XRP(100), USD(100)));
924 env(
pay(alice, carol, USD(100)),
925 path(~USD, ~EUR, ~USD),
932 Env env(*
this, features);
934 env.
trust(USD(1000), alice, bob);
935 env(
pay(gw, alice, USD(100)));
941 Env env(*
this, features);
942 env.
fund(
XRP(10000), alice, bob, gw);
943 env.
trust(USD(1000), alice, bob);
944 env(
pay(gw, alice, USD(100)));
966 Env env(*
this, features);
967 env.
fund(
XRP(10000), alice, bob, gw);
968 env.
trust(USD(1000), alice, bob);
969 env(
pay(gw, alice, USD(100)));
979 Env env(*
this, features);
980 env.
fund(
XRP(10000), alice, bob, gw);
982 env.
trust(USD(1000), alice, bob);
986 env(
pay(gw, alice, USD(100)));
1005 BEAST_EXPECT(
equal(strand, D{alice, gw, usdC}));
1010 Env env(*
this, features);
1011 env.
fund(
XRP(10000), alice, bob, gw);
1012 env.
trust(USD(1000), alice, bob);
1013 env(
pay(gw, alice, USD(100)));
1044 using namespace jtx;
1047 auto const alice =
Account(
"alice");
1048 auto const bob =
Account(
"bob");
1049 auto const carol =
Account(
"carol");
1050 auto const gw =
Account(
"gw");
1051 auto const USD = gw[
"USD"];
1052 auto const EUR = gw[
"EUR"];
1055 Env env(*
this, features);
1056 env.
fund(
XRP(10000), alice, bob, gw);
1058 env.
trust(USD(1000), alice, bob);
1059 env.
trust(EUR(1000), alice, bob);
1060 env.
trust(bob[
"USD"](1000), alice, gw);
1061 env.
trust(bob[
"EUR"](1000), alice, gw);
1066 env(
offer(bob, bob[
"USD"](100), bob[
"EUR"](100)),
1070 Path const p = [&] {
1079 env(
pay(alice, alice, EUR(1)),
1087 Env env(*
this, features);
1089 env.
fund(
XRP(10000), alice, bob, carol, gw);
1090 env.
trust(USD(10000), alice, bob, carol);
1092 env(
pay(gw, bob, USD(100)));
1098 env(
pay(alice, carol,
XRP(100)),
1105 Env env(*
this, features);
1107 env.
fund(
XRP(10000), alice, bob, carol, gw);
1108 env.
trust(USD(10000), alice, bob, carol);
1110 env(
pay(gw, bob, USD(100)));
1116 env(
pay(alice, carol,
XRP(100)),
1128 using namespace jtx;
1130 auto const alice =
Account(
"alice");
1131 auto const bob =
Account(
"bob");
1132 auto const carol =
Account(
"carol");
1133 auto const gw =
Account(
"gw");
1134 auto const USD = gw[
"USD"];
1135 auto const EUR = gw[
"EUR"];
1136 auto const CNY = gw[
"CNY"];
1139 Env env(*
this, features);
1141 env.
fund(
XRP(10000), alice, bob, carol, gw);
1142 env.
trust(USD(10000), alice, bob, carol);
1144 env(
pay(gw, bob, USD(100)));
1145 env(
pay(gw, alice, USD(100)));
1151 env(
pay(alice, carol, USD(100)),
1158 Env env(*
this, features);
1160 env.
fund(
XRP(10000), alice, bob, carol, gw);
1161 env.
trust(USD(10000), alice, bob, carol);
1162 env.
trust(EUR(10000), alice, bob, carol);
1163 env.
trust(CNY(10000), alice, bob, carol);
1165 env(
pay(gw, bob, USD(100)));
1166 env(
pay(gw, bob, EUR(100)));
1167 env(
pay(gw, bob, CNY(100)));
1174 env(
pay(alice, carol, CNY(100)),
1176 path(~USD, ~EUR, ~USD, ~CNY),
1186 using namespace jtx;
1188 auto const alice =
Account(
"alice");
1189 auto const bob =
Account(
"bob");
1190 auto const gw =
Account(
"gw");
1191 auto const USD = gw[
"USD"];
1193 Env env(*
this, features);
1194 env.
fund(
XRP(10000), alice, bob, gw);
1269 using namespace jtx;
1277 testLoop(sa - featurePermissionedDEX);
1284BEAST_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, std::optional< uint256 > const &domainID, 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.
FeatureBitset testable_amendments()
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.
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.
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
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.
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)