21#include <test/jtx/PathSet.h>
23#include <xrpld/app/paths/AMMContext.h>
24#include <xrpld/app/paths/Flow.h>
25#include <xrpld/app/paths/detail/Steps.h>
26#include <xrpld/app/paths/detail/StrandFlow.h>
28#include <xrpl/basics/contract.h>
29#include <xrpl/basics/random.h>
30#include <xrpl/ledger/PaymentSandbox.h>
31#include <xrpl/protocol/Feature.h>
32#include <xrpl/protocol/jss.h>
59 for (
auto const&
path : jv[jss::Paths])
62 for (
auto const& pe :
path)
64 if (pe.isMember(jss::account))
67 !pe.isMember(jss::currency) &&
68 !pe.isMember(jss::issuer));
70 *parseBase58<AccountID>(
71 pe[jss::account].asString()),
76 pe.isMember(jss::currency) && pe.isMember(jss::issuer))
82 issuer = *parseBase58<AccountID>(
83 pe[jss::issuer].asString());
85 assert(
isXRP(*parseBase58<AccountID>(
86 pe[jss::issuer].asString())));
94 paths.emplace_back(std::move(p));
138 jv[field.jsonName] = value;
150 IOU const iou{peer, currency};
183 IOU const iou{acc, currency};
227 sstr <<
rate <<
" (" << q <<
")";
231 template <
class Stream>
235 stream <<
"Strand:\n";
236 for (
auto const& step : strand)
237 stream <<
"\n" << *step;
282 auto compareClose = [](Quality
const& q1, Quality
const& q2) {
285 constexpr double tolerance = 0.0000001;
286 return relativeDistance(q1, q2) <= tolerance;
289 for (
auto const& strand : sr.second)
291 Quality
const theoreticalQ = *qualityUpperBound(sb, strand);
292 auto const f = flow<IOUAmount, IOUAmount>(
293 sb, strand, IOUAmount(10, 0), IOUAmount(5, 0), dummyJ);
294 BEAST_EXPECT(f.success);
295 Quality
const actualQ(f.out, f.in);
296 if (actualQ != theoreticalQ && !compareClose(actualQ, theoreticalQ))
298 BEAST_EXPECT(actualQ == theoreticalQ);
299 log <<
"\nAcutal != Theoretical\n";
304 if (expectedQ && expectedQ != theoreticalQ &&
305 !compareClose(*expectedQ, theoreticalQ))
307 BEAST_EXPECT(expectedQ == theoreticalQ);
308 log <<
"\nExpected != Theoretical\n";
352 int const numTestIterations = reqNumIterations.
value_or(250);
363 for (
int i = 0; i < numTestIterations; ++i)
368 auto const alice =
Account(
"alice" + iterAsStr);
369 auto const bob =
Account(
"bob" + iterAsStr);
370 auto const carol =
Account(
"carol" + iterAsStr);
371 auto const dan =
Account(
"dan" + iterAsStr);
374 numAccounts == 4,
"Path is only correct for four accounts");
375 path const accountsPath(accounts[1], accounts[2]);
376 env.
fund(
XRP(10000), alice, bob, carol, dan);
389 if (j == numAccounts)
393 env, accounts[ii], accounts[j], currency);
395 env, accounts[ii], accounts[j], currency);
399 IOU const iou{accounts.back(), currency};
401 pay(accounts.front(), accounts.back(), iou(paymentAmount)),
424 int const numTestIterations = reqNumIterations.
value_or(100);
438 for (
int i = 0; i < numTestIterations; ++i)
441 auto const alice =
Account(
"alice" + iterAsStr);
442 auto const bob =
Account(
"bob" + iterAsStr);
443 auto const carol =
Account(
"carol" + iterAsStr);
444 auto const dan =
Account(
"dan" + iterAsStr);
445 auto const oscar =
Account(
"oscar" + iterAsStr);
446 auto const USDB = bob[
"USD"];
447 auto const EURC = carol[
"EUR"];
450 {alice, bob, carol, dan, oscar}};
455 path const bookPath(~EURC);
457 env.
fund(
XRP(10000), alice, bob, carol, dan, oscar);
460 for (
auto const& acc : accounts)
463 for (
auto const& currency : {usdCurrency, eurCurrency})
466 env, alice, bob, currency);
468 env, carol, dan, currency);
470 env, oscar, bob, currency);
472 env, oscar, carol, currency);
480 env(
offer(oscar, USDB(50), EURC(50)));
484 IOU const srcIOU{bob, usdCurrency};
485 IOU const dstIOU{carol, eurCurrency};
487 pay(alice, dan, dstIOU(paymentAmount)),
488 sendmax(srcIOU(100 * paymentAmount)),
499 testcase(
"Relative quality distance");
502 int exponent = 0) -> Quality {
508 return Quality{
one, v};
511 BEAST_EXPECT(relativeDistance(toQuality(100), toQuality(100)) == 0);
512 BEAST_EXPECT(relativeDistance(toQuality(100), toQuality(100, 1)) == 9);
513 BEAST_EXPECT(relativeDistance(toQuality(100), toQuality(110)) == .1);
515 relativeDistance(toQuality(100, 90), toQuality(110, 90)) == .1);
517 relativeDistance(toQuality(100, 90), toQuality(110, 91)) == 10);
519 relativeDistance(toQuality(100, 0), toQuality(100, 90)) == 1e90);
525 relativeDistance(toQuality(102, 0), toQuality(101, 90)) >= 1e89);
539 auto const r = stoi(s, &pos);
555BEAST_DEFINE_TESTSUITE_PRIO(TheoreticalQuality, app,
ripple, 3);
bool isMember(char const *key) const
Return true if the object has a member named key.
A generic endpoint for log messages.
static Sink & getNullSink()
Returns a Sink which does nothing.
log_os< char > log
Logging output stream.
testcase_t testcase
Memberspace for declaring test cases.
std::string const & arg() const
Return the argument associated with the runner.
Maintains AMM info per overall payment engine execution and individual iteration.
A wrapper which makes credits unavailable to balances.
Issue const & issue() const
void emplace_back(Args &&... args)
void setupTrustLine(jtx::Env &env, jtx::Account const &acc, jtx::Account const &peer, Currency const ¤cy)
std::uniform_real_distribution zeroOneDist_
void maybeInsertQuality(Json::Value &jv, QualityDirection qDir)
void maybeSetInitialBalance(jtx::Env &env, jtx::Account const &acc, jtx::Account const &peer, Currency const ¤cy)
void setInitialBalance(jtx::Env &env, jtx::Account const &acc, jtx::Account const &peer, Currency const ¤cy)
beast::xor_shift_engine engine_
static constexpr double probRedeem_
RandomAccountParams(std::uint32_t trustAmount=100, std::uint32_t initialBalance=50)
std::uint32_t const initialBalance_
std::uniform_real_distribution transferRateDist_
std::uniform_real_distribution qualityPercentDist_
void setupTrustLines(jtx::Env &env, jtx::Account const &acc1, jtx::Account const &acc2, Currency const ¤cy)
std::uint32_t const trustAmount_
static constexpr double probChangeDefault_
void maybeSetTransferRate(jtx::Env &env, jtx::Account const &acc)
void run() override
Runs the suite.
static std::string prettyQuality(Quality const &q)
void testBookStep(std::optional< int > const &reqNumIterations)
void testRelativeQDistance()
void testDirectStep(std::optional< int > const &reqNumIterations)
static void logStrand(Stream &stream, Strand const &strand)
void testCase(RippleCalcTestParams const &rcp, std::shared_ptr< ReadView const > closed, std::optional< Quality > const &expectedQ={})
Immutable cryptographic account descriptor.
A transaction testing environment.
Json::Value json(JsonValue &&jv, FN const &... fN)
Create JSON from parameters.
std::shared_ptr< ReadView const > closed()
Returns the last closed ledger.
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)
Converts to IOU Issue or STAmount.
Set Paths, SendMax on a JTx.
Sets the SendMax on a JTx.
Json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
FeatureBitset testable_amendments()
Json::Value rate(Account const &account, double multiplier)
Set a transfer rate.
Json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
XRP_t const XRP
Converts to XRP Issue or STAmount.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool isXRP(AccountID const &c)
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
STAmount amountFromJson(SField const &name, Json::Value const &v)
constexpr std::uint32_t tfPartialPayment
Issue const & noIssue()
Returns an asset specifier that represents no account and currency.
constexpr std::uint32_t tfNoRippleDirect
std::pair< TER, std::vector< Strand > > toStrands(ReadView const &view, AccountID const &src, AccountID const &dst, Issue const &deliver, std::optional< Quality > const &limitQuality, std::optional< Issue > const &sendMax, STPathSet const &paths, bool addDefaultPath, bool ownerPaysTransferFee, OfferCrossing offerCrossing, AMMContext &ammContext, std::optional< uint256 > const &domainID, beast::Journal j)
Create a Strand for each specified path (including the default path, if indicated)
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
RippleCalcTestParams(Json::Value const &jv)
std::optional< STAmount > sendMax