1#include <test/jtx/Account.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/TestHelpers.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/balance.h>
6#include <test/jtx/domain.h>
7#include <test/jtx/envconfig.h>
8#include <test/jtx/mpt.h>
9#include <test/jtx/offer.h>
10#include <test/jtx/pay.h>
11#include <test/jtx/permissioned_dex.h>
13#include <xrpld/core/Config.h>
14#include <xrpld/rpc/Context.h>
15#include <xrpld/rpc/RPCHandler.h>
16#include <xrpld/rpc/Role.h>
17#include <xrpld/rpc/detail/AccountAssets.h>
18#include <xrpld/rpc/detail/AssetCache.h>
19#include <xrpld/rpc/detail/RPCHelpers.h>
20#include <xrpld/rpc/detail/Tuning.h>
22#include <xrpl/basics/base_uint.h>
23#include <xrpl/beast/unit_test/suite.h>
24#include <xrpl/core/Job.h>
25#include <xrpl/core/JobQueue.h>
26#include <xrpl/json/json_value.h>
27#include <xrpl/protocol/AccountID.h>
28#include <xrpl/protocol/ApiVersion.h>
29#include <xrpl/protocol/ErrorCodes.h>
30#include <xrpl/protocol/Indexes.h>
31#include <xrpl/protocol/PathAsset.h>
32#include <xrpl/protocol/SField.h>
33#include <xrpl/protocol/STAmount.h>
34#include <xrpl/protocol/STPathSet.h>
35#include <xrpl/protocol/UintTypes.h>
36#include <xrpl/protocol/jss.h>
37#include <xrpl/resource/Charge.h>
38#include <xrpl/resource/Consumer.h>
39#include <xrpl/resource/Fees.h>
58 jv[jss::command] =
"ripple_path_find";
59 jv[jss::source_account] =
toBase58(src);
65 for (
auto const&
id : numSrc)
73 jv[jss::destination_account] = d;
96 cfg->pathSearchOld = 7;
98 cfg->pathSearchMax = 10;
108 using namespace std::chrono_literals;
111 auto const gw =
Account(
"gateway");
112 auto const alice =
Account(
"alice");
113 auto const bob =
Account(
"bob");
115 env.
fund(
XRP(10'000),
"alice",
"bob", gw);
118 MPTTester({.env = env, .issuer = gw, .holders = {alice, bob}, .maxAmt = 100});
120 auto& app = env.
app();
128 .loadType = loadType,
129 .netOps = app.getOPs(),
130 .ledgerMaster = app.getLedgerMaster(),
148 app.getJobQueue().postCoro(
JtClient,
"RPC-Client", [&](
auto const& coro) {
155 BEAST_EXPECT(!result.
isMember(jss::error));
159 app.getJobQueue().postCoro(
JtClient,
"RPC-Client", [&](
auto const& coro) {
166 BEAST_EXPECT(result[jss::error] ==
"srcCurMalformed");
168 BEAST_EXPECT(result[jss::error_message] ==
"Source currency is malformed.");
174 auto const curm =
MPTTester({.env = env, .issuer = alice, .holders = {bob}});
177 app.getJobQueue().postCoro(
JtClient,
"RPC-Client", [&](
auto const& coro) {
184 BEAST_EXPECT(!result.
isMember(jss::error));
187 auto const curm =
MPTTester({.env = env, .issuer = alice, .holders = {bob}});
188 app.getJobQueue().postCoro(
JtClient,
"RPC-Client", [&](
auto const& coro) {
195 BEAST_EXPECT(result[jss::error] ==
"internal");
196 BEAST_EXPECT(result[jss::error_code] ==
RpcInternal);
197 BEAST_EXPECT(result[jss::error_message] ==
"Internal error.");
203 testcase(
"no direct path no intermediary no alternatives");
208 env.
fund(
XRP(10'000),
"alice",
"bob");
210 auto const usdm =
MPTTester({.env = env, .issuer =
"bob"});
212 auto const result =
findPaths(env,
"alice",
"bob", usdm(5));
213 BEAST_EXPECT(std::get<0>(result).empty());
219 testcase(
"direct path no intermediary");
222 env.
fund(
XRP(10'000),
"alice",
"bob");
224 MPT const usd =
MPTTester({.env = env, .issuer =
"alice", .holders = {
"bob"}});
229 BEAST_EXPECT(st.empty());
230 BEAST_EXPECT(
equal(sa, usd(5)));
239 auto const gw =
Account(
"gateway");
240 env.
fund(
XRP(10'000),
"alice",
"bob", gw);
241 MPT const usd =
MPTTester({.env = env, .issuer = gw, .holders = {
"alice",
"bob"}});
242 env(
pay(gw,
"alice", usd(70)));
243 env(
pay(
"alice",
"bob", usd(24)));
251 testcase(
"source currency with send_max");
255 auto const alice =
Account(
"alice");
256 auto const bob =
Account(
"bob");
257 auto const gw =
Account(
"gateway");
258 env.
fund(
XRP(10'000), alice, bob, gw);
260 MPT const usd =
MPTTester({.env = env, .issuer = gw, .holders = {alice, bob}});
261 env(
pay(gw, alice, usd(25)));
274 BEAST_EXPECTS(!result.isMember(jss::error), result.toStyledString());
276 auto const& alternatives = result[jss::alternatives];
277 if (BEAST_EXPECT(alternatives.size() == 1))
281 BEAST_EXPECTS(
equal(sa, usd(10)), sa.getFullText());
282 BEAST_EXPECTS(
equal(da, usd(10)), da.getFullText());
289 testcase(
"maxed-out MPT pathfinding");
292 auto const hasMPT = [](
auto const& assets,
MPT const& mpt) {
294 return asset.template holds<MPTID>() && asset.template
get<MPTID>() == mpt.mpt();
299 auto const gw =
Account(
"gateway");
300 auto const alice =
Account(
"alice");
301 auto const bob =
Account(
"bob");
302 auto const carol =
Account(
"carol");
304 env.
fund(
XRP(10'000), gw, alice, bob, carol);
308 MPTTester({.env = env, .issuer = gw, .holders = {alice, bob, carol}, .maxAmt = 100});
309 env(
pay(gw, alice, usd(90)));
310 env(
pay(gw, bob, usd(10)));
324 auto [st, sa, da] =
findPaths(env, alice, bob, usd(5));
325 BEAST_EXPECT(st.empty());
326 BEAST_EXPECT(
equal(sa, usd(5)));
327 BEAST_EXPECT(
equal(da, usd(5)));
333 BEAST_EXPECT(sa == usd(5));
335 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
337 auto const& pathElem = st[0][0];
339 pathElem.isOffer() && pathElem.getIssuerID() ==
xrpAccount() &&
350 auto const gw =
Account(
"gateway");
351 env.
fund(
XRP(10'000),
"alice",
"bob", gw);
352 MPT const usd =
MPTTester({.env = env, .issuer = gw, .holders = {
"alice",
"bob"}});
353 env(
pay(gw,
"alice", usd(70)));
354 env(
pay(gw,
"bob", usd(50)));
364 env,
"alice",
"bob", usd(5), std::nullopt, std::nullopt, std::nullopt, domainID);
367 BEAST_EXPECT(st.empty());
368 BEAST_EXPECT(
equal(sa, usd(5)));
369 BEAST_EXPECT(
equal(da, usd(5)));
376 std::string(
"path find consume all") + (domainEnabled ?
" w/ " :
" w/o ") +
"domain");
381 auto const gw =
Account(
"gateway");
382 env.
fund(
XRP(10'000),
"alice",
"bob",
"carol", gw);
383 MPT const usd =
MPTTester({.env = env, .issuer = gw, .holders = {
"bob",
"carol"}});
385 env(
pay(gw,
"carol", usd(100)));
389 domainID =
setupDomain(env, {
"alice",
"bob",
"carol",
"gateway"});
394 env(
offer(
"carol",
XRP(100), usd(100)));
410 BEAST_EXPECT(st.
empty());
420 if (BEAST_EXPECT(st.
size() == 1 && st[0].
size() == 1))
422 auto const& pathElem = st[0][0];
424 pathElem.isOffer() && pathElem.getIssuerID() == gw.id() &&
427 BEAST_EXPECT(sa ==
XRP(100));
428 BEAST_EXPECT(
equal(da, usd(100)));
442 BEAST_EXPECT(st.
empty());
451 std::string(
"alternative path consume best transfer") +
452 (domainEnabled ?
" w/ " :
" w/o ") +
"domain");
455 auto const gw =
Account(
"gateway");
456 auto const gw2 =
Account(
"gateway2");
457 env.
fund(
XRP(10'000),
"alice",
"bob", gw, gw2);
458 MPT const usd =
MPTTester({.env = env, .issuer = gw, .holders = {
"alice",
"bob"}});
460 {.env = env, .issuer = gw2, .holders = {
"alice",
"bob"}, .transferFee = 1'000});
464 domainID =
setupDomain(env, {
"alice",
"bob",
"gateway",
"gateway2"});
465 env(
pay(gw,
"alice", usd(70)),
Domain(*domainID));
466 env(
pay(gw2,
"alice", gw2Usd(70)),
Domain(*domainID));
467 env(
pay(
"alice",
"bob", usd(70)),
Domain(*domainID));
471 env(
pay(gw,
"alice", usd(70)));
472 env(
pay(gw2,
"alice", gw2Usd(70)));
473 env(
pay(
"alice",
"bob", usd(70)));
486 auto const alice =
Account(
"alice");
487 auto const bob =
Account(
"bob");
488 auto const charlie =
Account(
"charlie");
493 env.
fund(
XRP(10'000), alice, bob, charlie, gw);
495 MPT const usd =
MPTTester({.env = env, .issuer = gw, .holders = {alice, bob, charlie}});
496 env(
pay(gw, charlie, usd(10)));
501 domainID =
setupDomain(env, {alice, bob, charlie, gw});
506 env(
offer(charlie,
XRP(10), usd(10)));
510 env, alice, bob, usd(-1),
XRP(100).value(), std::nullopt, std::nullopt, domainID);
511 BEAST_EXPECT(sa ==
XRP(10));
512 BEAST_EXPECT(
equal(da, usd(10)));
513 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
515 auto const& pathElem = st[0][0];
517 pathElem.isOffer() && pathElem.getIssuerID() == gw.id() &&
518 pathElem.getMPTID() == usd.
mpt());
524 env.
fund(
XRP(10'000), alice, bob, charlie, gw);
526 MPT const usd =
MPTTester({.env = env, .issuer = gw, .holders = {alice, bob, charlie}});
527 env(
pay(gw, alice, usd(10)));
532 domainID =
setupDomain(env, {alice, bob, charlie, gw});
537 env(
offer(charlie, usd(10),
XRP(10)));
541 env, alice, bob,
drops(-1), usd(100).value(), std::nullopt, std::nullopt, domainID);
542 BEAST_EXPECT(sa == usd(10));
544 if (BEAST_EXPECT(st.size() == 1 && st[0].size() == 1))
546 auto const& pathElem = st[0][0];
548 pathElem.isOffer() && pathElem.getIssuerID() ==
xrpAccount() &&
609 testcase(
"convert_all + send_max: srcAmount governs path ranking");
613 auto const alice =
Account(
"alice");
614 auto const bob =
Account(
"bob");
615 auto const gw =
Account(
"gateway");
616 auto const charlie =
Account(
"charlie");
617 auto const dave =
Account(
"dave");
618 auto const eve =
Account(
"eve");
619 auto const frank =
Account(
"frank");
621 env.
fund(
XRP(10'000), alice, bob, gw, charlie, dave, eve, frank);
628 MPTTester({.env = env, .issuer = gw, .holders = {charlie, dave, eve, frank, bob}});
631 env(
pay(gw, charlie, usd(500)));
632 env(
pay(gw, dave, usd(500)));
633 env(
pay(gw, eve, usd(500)));
634 env(
pay(gw, frank, usd(75)));
638 auto const aud = charlie[
"AUD"];
639 auto const eur = dave[
"EUR"];
640 auto const gbp = eve[
"GBP"];
641 auto const jpy = frank[
"JPY"];
645 env(
offer(charlie,
XRP(1'000), aud(1'000)));
646 env(
offer(charlie, aud(1'000), usd(500)));
647 env(
offer(dave,
XRP(1'000), eur(1'000)));
648 env(
offer(dave, eur(1'000), usd(500)));
649 env(
offer(eve,
XRP(1'000), gbp(1'000)));
650 env(
offer(eve, gbp(1'000), usd(500)));
654 env(
offer(frank,
XRP(50), jpy(50)));
655 env(
offer(frank, jpy(50), usd(75)));
668 BEAST_EXPECT(sa ==
XRP(100));
669 BEAST_EXPECT(
equal(da, usd(100)));
682 for (
auto const domainEnabled : {
false,
true})
TestcaseT testcase
Memberspace for declaring test cases.
bool isMember(char const *key) const
Return true if the object has a member named key.
std::vector< STPath >::size_type size() const
virtual beast::Journal getJournal(std::string const &name)=0
An endpoint that consumes resources.
void pathFindConsumeAll(bool const domainEnabled)
void noDirectPathNoIntermediaryNoAlternatives()
void receiveMax(bool const domainEnabled)
void sourceCurrenciesLimit()
void directPathNoIntermediary()
void paymentAutoPathFind()
void sourceCurrencyWithSendMax()
void run() override
Runs the suite.
void convertAllSendMaxRanking()
void alternativePathsConsumeBestTransfer(bool const domainEnabled)
void maxedOutMPTPathfinding()
void pathFind(bool const domainEnabled)
Immutable cryptographic account descriptor.
A transaction testing environment.
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
beast::Journal const journal
void require(Args const &... args)
Check a set of requirements.
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
bool waitFor(std::chrono::duration< Rep, Period > const &relTime)
Test helper for creating, mutating, and asserting MPT and confidential MPT ledger state.
Converts to MPT Issue or STAmount.
xrpl::MPTID const & mpt() const
@ Array
array value (ordered list)
@ Object
object value (collection of name/value pairs).
Charge const kFeeReferenceRpc
static constexpr int kMaxSrcCur
Maximum number of source currencies allowed in a path find request.
static constexpr int kMaxAutoSrcCur
Maximum number of auto source currencies in a path find request.
static constexpr auto kApiVersionIfUnspecified
::rpc::Status doCommand(rpc::JsonContext &context, json::Value &result)
Execute an RPC command and store the results in a json::Value.
static json::Value rpf(jtx::Account const &src, jtx::Account const &dst, xrpl::test::jtx::MPT const &usd, std::vector< MPTID > const &numSrc)
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
XrpT const XRP
Converts to XRP Issue or STAmount.
std::tuple< STPathSet, STAmount, STAmount > findPaths(jtx::Env &env, jtx::Account const &src, jtx::Account const &dst, STAmount const &saDstAmount, std::optional< STAmount > const &saSendMax, std::optional< PathAsset > const &srcAsset, std::optional< AccountID > const &srcIssuer, std::optional< UInt256 > const &domain)
json::Value findPathsRequest(jtx::Env &env, jtx::Account const &src, jtx::Account const &dst, STAmount const &saDstAmount, std::optional< STAmount > const &saSendMax, std::optional< PathAsset > const &srcAsset, std::optional< AccountID > const &srcIssuer, std::optional< UInt256 > const &domain)
bool equal(STAmount const &sa1, STAmount const &sa2)
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
UInt256 setupDomain(jtx::Env &env, std::vector< jtx::Account > const &accounts, jtx::Account const &domainOwner, std::string const &credType)
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
BaseUInt< 256 > Domain
Domain is a 256-bit hash representing a specific domain.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Currency const & xrpCurrency()
XRP currency.
std::string to_string(BaseUInt< Bits, Tag > const &a)
HashSet< PathAsset > accountDestAssets(AccountID const &account, std::shared_ptr< AssetCache > const &lrCache, bool includeXRP)
STAmount amountFromJson(SField const &name, json::Value const &v)
HashSet< PathAsset > accountSourceAssets(AccountID const &account, std::shared_ptr< AssetCache > const &lrCache, bool includeXRP)
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
AccountID const & xrpAccount()
Compute AccountID from public key.
std::shared_ptr< JobQueue::Coro > coro