1#include <xrpld/rpc/detail/PathRequest.h>
3#include <xrpld/app/main/Application.h>
4#include <xrpld/core/Config.h>
5#include <xrpld/rpc/detail/AccountAssets.h>
6#include <xrpld/rpc/detail/PathRequestManager.h>
7#include <xrpld/rpc/detail/Pathfinder.h>
8#include <xrpld/rpc/detail/PathfinderUtils.h>
9#include <xrpld/rpc/detail/Tuning.h>
11#include <xrpl/basics/Log.h>
12#include <xrpl/basics/UnorderedContainers.h>
13#include <xrpl/basics/base_uint.h>
14#include <xrpl/beast/utility/Journal.h>
15#include <xrpl/beast/utility/Zero.h>
16#include <xrpl/beast/utility/instrumentation.h>
17#include <xrpl/json/json_value.h>
18#include <xrpl/ledger/ApplyView.h>
19#include <xrpl/ledger/PaymentSandbox.h>
20#include <xrpl/protocol/AccountID.h>
21#include <xrpl/protocol/Asset.h>
22#include <xrpl/protocol/ErrorCodes.h>
23#include <xrpl/protocol/Indexes.h>
24#include <xrpl/protocol/LedgerFormats.h>
25#include <xrpl/protocol/PathAsset.h>
26#include <xrpl/protocol/Protocol.h>
27#include <xrpl/protocol/RPCErr.h>
28#include <xrpl/protocol/STAmount.h>
29#include <xrpl/protocol/STPathSet.h>
30#include <xrpl/protocol/SystemParameters.h>
31#include <xrpl/protocol/TER.h>
32#include <xrpl/protocol/UintTypes.h>
33#include <xrpl/protocol/jss.h>
34#include <xrpl/resource/Consumer.h>
35#include <xrpl/server/InfoSub.h>
36#include <xrpl/server/LoadFeeTrack.h>
37#include <xrpl/tx/paths/RippleCalc.h>
167 XRPL_ASSERT(
inProgress_,
"xrpl::PathRequest::updateComplete : in progress");
190 auto const& lrLedger = crCache->getLedger();
222 bool const disallowXRP(sleDest->isFlag(lsfDisallowXRP));
226 for (
auto const& asset : destAssets)
229 jvStatus_[jss::destination_tag] = (sleDest->getFlags() & lsfRequireDestTag);
233 jvStatus_[jss::ledger_index] = lrLedger->seq();
258 if (
auto const stream =
journal_.debug())
278 if (!jvParams.
isMember(jss::source_account))
281 return PFR_PJ_INVALID;
284 if (!jvParams.
isMember(jss::destination_account))
287 return PFR_PJ_INVALID;
290 if (!jvParams.
isMember(jss::destination_amount))
293 return PFR_PJ_INVALID;
300 return PFR_PJ_INVALID;
307 return PFR_PJ_INVALID;
313 return PFR_PJ_INVALID;
321 return PFR_PJ_INVALID;
324 if (jvParams.
isMember(jss::send_max))
330 return PFR_PJ_INVALID;
340 return PFR_PJ_INVALID;
344 if (jvParams.
isMember(jss::source_currencies))
346 json::Value const& jvSrcCurrencies = jvParams[jss::source_currencies];
347 if (!jvSrcCurrencies.
isArray() || jvSrcCurrencies.
size() == 0 ||
351 return PFR_PJ_INVALID;
356 for (
auto const& c : jvSrcCurrencies)
362 return PFR_PJ_INVALID;
366 if (c.isMember(jss::currency))
369 if (!c[jss::currency].isString() ||
370 !
toCurrency(currency, c[jss::currency].asString()))
373 return PFR_PJ_INVALID;
375 srcPathAsset = currency;
380 if (!c[jss::mpt_issuance_id].isString() ||
381 !u.
parseHex(c[jss::mpt_issuance_id].asString()))
384 return PFR_PJ_INVALID;
391 if (c.isMember(jss::issuer) &&
392 (c.isMember(jss::mpt_issuance_id) || !c[jss::issuer].isString() ||
393 !
toIssuer(srcIssuerID, c[jss::issuer].asString())))
396 return PFR_PJ_INVALID;
406 return PFR_PJ_INVALID;
409 else if (srcIssuerID.
isZero())
420 auto const status = srcPathAsset.
visit(
431 return PFR_PJ_INVALID;
448 return PFR_PJ_NOCHANGE;
450 [&](
MPTID const& mpt) {
452 return PFR_PJ_NOCHANGE;
454 if (status == PFR_PJ_INVALID)
470 jvId_ = jvParams[jss::id];
475 if (!jvParams[jss::domain].isString() || !num.
parseHex(jvParams[jss::domain].
asString()))
478 return PFR_PJ_INVALID;
484 return PFR_PJ_NOCHANGE;
519 auto const i = currencyMap.find(currency);
520 if (i != currencyMap.end())
534 if (pathfinder->findPaths(level, continueCallback))
536 pathfinder->computePathRanks(
kMaxPaths, continueCallback);
542 return currencyMap[currency] = std::move(pathfinder);
557 if (sourceAssets.empty())
563 for (
auto const& asset : assets)
566 [&]<
typename TAsset>(TAsset
const& a) {
592 for (
auto const& asset : sourceAssets)
594 if (continueCallback && !continueCallback())
608 auto ps = pathfinder->getBestPaths(
609 kMaxPaths, fullLiquidityPath,
context_[asset], asset.getIssuer(), continueCallback);
612 auto const& sourceAccount = [&] {
613 if (!
isXRP(asset.getIssuer()))
614 return asset.getIssuer();
626 [&](
Issue const& issue) {
657 ps.pushBack(fullLiquidityPath);
687 if (rc.actualAmountIn.holds<
Issue>())
688 rc.actualAmountIn.get<
Issue>().account = sourceAccount;
694 jvEntry[jss::destination_amount] =
717 int const size = sourceAssets.size();
746 for (
auto const& asset : assets)
755 newStatus[jss::full_reply] = !fast;
758 newStatus[jss::id] =
jvId_;
760 bool const loaded =
app_.getFeeTrack().isLoadedLocal();
774 else if ((
iLevel_ ==
app_.config().pathSearchFast) && !fast)
778 if (loaded && (
iLevel_ >
app_.config().pathSearchFast))
785 (loaded && (
iLevel_ >
app_.config().pathSearchFast)))
791 if (!loaded && (
iLevel_ <
app_.config().pathSearchMax))
793 if (loaded && (
iLevel_ >
app_.config().pathSearchFast))
803 newStatus[jss::alternatives] = std::move(jvArray);
811 if (fast &&
quickReply_ == steady_clock::time_point{})
816 else if (!fast &&
fullReply_ == steady_clock::time_point{})
A generic endpoint for log messages.
Value & append(Value const &value)
Append value to array at the end.
UInt size() const
Number of values in array or object.
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
std::shared_ptr< InfoSub > pointer
A currency issued by an account.
constexpr auto visit(Visitors &&... visitors) const -> decltype(auto)
constexpr bool holds() const
PathRequest(Application &app, std::shared_ptr< InfoSub > const &subscriber, int id, PathRequestManager &, beast::Journal journal)
std::recursive_mutex lock_
std::chrono::steady_clock::time_point quickReply_
json::Value doUpdate(std::shared_ptr< AssetCache > const &, bool fast, std::function< bool()> const &continueCallback={})
int parseJson(json::Value const &)
static unsigned int const kMaxPaths
bool isValid(std::shared_ptr< AssetCache > const &crCache)
PathRequestManager & owner_
std::optional< UInt256 > domain_
std::chrono::steady_clock::time_point const created_
bool needsUpdate(bool newOnly, LedgerIndex index)
resource::Consumer & consumer_
json::Value doClose() override
std::recursive_mutex indexLock_
std::function< void()> fCompletion_
std::weak_ptr< InfoSub > wpSubscriber_
InfoSub::pointer getSubscriber() const
json::Value doStatus(json::Value const &) override
std::optional< STAmount > saSendMax_
std::chrono::steady_clock::time_point fullReply_
std::set< Asset > sciSourceAssets_
std::optional< AccountID > raDstAccount_
std::unique_ptr< Pathfinder > const & getPathFinder(std::shared_ptr< AssetCache > const &, HashMap< PathAsset, std::unique_ptr< Pathfinder > > &, PathAsset const &, STAmount const &, int const, std::function< bool()> const &)
bool findPaths(std::shared_ptr< AssetCache > const &, int const, json::Value &, std::function< bool()> const &)
Finds and sets a PathSet in the JSON argument.
std::pair< bool, json::Value > doCreate(std::shared_ptr< AssetCache > const &, json::Value const &)
std::optional< AccountID > raSrcAccount_
std::map< Asset, STPathSet > context_
std::string getFullText() const override
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, ServiceRegistry ®istry, Input const *const pInputs=nullptr)
An endpoint that consumes resources.
T duration_cast(T... args)
JSON (JavaScript Object Notation).
@ Array
array value (ordered list)
@ Object
object value (collection of name/value pairs).
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Keylet account(AccountID const &id) noexcept
AccountID root.
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
STAmount convertAmount(STAmount const &amt, bool all)
bool isXRP(AccountID const &c)
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::uint32_t LedgerIndex
A ledger index.
bool validJSONAsset(json::Value const &jv)
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
std::string transHuman(TER code)
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
bool validAsset(Asset const &asset)
std::string to_string(BaseUInt< Bits, Tag > const &a)
HashSet< PathAsset > accountDestAssets(AccountID const &account, std::shared_ptr< AssetCache > const &lrCache, bool includeXRP)
json::Value rpcError(ErrorCodeI iError)
bool amountFromJsonNoThrow(STAmount &result, json::Value const &jvSource)
BaseUInt< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
HashSet< PathAsset > accountSourceAssets(AccountID const &account, std::shared_ptr< AssetCache > const &lrCache, bool includeXRP)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
bool isTesSuccess(TER x) noexcept
AccountID const & xrpAccount()
Compute AccountID from public key.
static std::string const & systemCurrencyCode()
std::unordered_map< Key, Value, Hash, Pred, Allocator > HashMap
bool toIssuer(AccountID &, std::string const &)
Convert hex or base58 string to AccountID.