27#include "data/AmendmentCenterInterface.hpp"
28#include "data/BackendInterface.hpp"
29#include "data/Types.hpp"
31#include "rpc/common/Types.hpp"
32#include "util/JsonUtils.hpp"
33#include "util/Taggable.hpp"
34#include "util/log/Logger.hpp"
35#include "web/Context.hpp"
37#include <boost/asio/spawn.hpp>
38#include <boost/json/array.hpp>
39#include <boost/json/object.hpp>
40#include <boost/json/value.hpp>
41#include <boost/regex.hpp>
42#include <boost/regex/v5/regex_fwd.hpp>
43#include <boost/regex/v5/regex_match.hpp>
45#include <xrpl/basics/base_uint.h>
46#include <xrpl/json/json_value.h>
47#include <xrpl/protocol/AccountID.h>
48#include <xrpl/protocol/Book.h>
49#include <xrpl/protocol/Fees.h>
50#include <xrpl/protocol/Indexes.h>
51#include <xrpl/protocol/Issue.h>
52#include <xrpl/protocol/Keylet.h>
53#include <xrpl/protocol/LedgerHeader.h>
54#include <xrpl/protocol/PublicKey.h>
55#include <xrpl/protocol/Rate.h>
56#include <xrpl/protocol/STAmount.h>
57#include <xrpl/protocol/STBase.h>
58#include <xrpl/protocol/STLedgerEntry.h>
59#include <xrpl/protocol/STObject.h>
60#include <xrpl/protocol/STTx.h>
61#include <xrpl/protocol/SecretKey.h>
62#include <xrpl/protocol/Seed.h>
63#include <xrpl/protocol/TxMeta.h>
64#include <xrpl/protocol/UintTypes.h>
65#include <xrpl/protocol/XRPAmount.h>
89std::optional<ripple::AccountID>
110getStartHint(ripple::SLE
const& sle, ripple::AccountID
const& accountID);
118std::optional<AccountCursor>
128std::pair<std::shared_ptr<ripple::STTx const>, std::shared_ptr<ripple::STObject const>>
139std::pair<std::shared_ptr<ripple::STTx const>, std::shared_ptr<ripple::TxMeta const>>
151std::pair<boost::json::object, boost::json::object>
154 std::uint32_t apiVersion,
156 std::optional<uint16_t> networkId = std::nullopt
189 boost::json::object& metaJson,
190 std::shared_ptr<ripple::STTx const>
const& txn,
191 std::shared_ptr<ripple::TxMeta const>
const& meta,
205 boost::json::object& metaJson,
206 std::shared_ptr<ripple::STTx const>
const& txn,
207 std::shared_ptr<ripple::TxMeta const>
const& meta
217toJson(ripple::STBase
const& obj);
226toJson(ripple::SLE
const& sle);
237toJson(ripple::LedgerHeader
const& info,
bool binary, std::uint32_t apiVersion);
246toJson(ripple::TxMeta
const& meta);
248using RippledJson = Json::Value;
270 ripple::LedgerHeader
const& lgrInfo,
271 ripple::Fees
const& fees,
272 std::string
const& ledgerRange,
273 std::uint32_t txnCount
283std::expected<ripple::LedgerHeader, Status>
296std::expected<ripple::LedgerHeader, Status>
299 boost::asio::yield_context yield,
300 std::optional<std::string> ledgerHash,
301 std::optional<uint32_t> ledgerIndex,
318std::expected<AccountCursor, Status>
321 ripple::Keylet
const& owner,
322 ripple::uint256
const& hexMarker,
323 std::uint32_t startHint,
324 std::uint32_t sequence,
326 boost::asio::yield_context yield,
327 std::function<
void(ripple::SLE)> atOwnedNode
345std::expected<AccountCursor, Status>
348 ripple::AccountID
const& accountID,
349 std::uint32_t sequence,
351 std::optional<std::string> jsonCursor,
352 boost::asio::yield_context yield,
353 std::function<
void(ripple::SLE)> atOwnedNode,
354 bool nftIncluded =
false
366std::shared_ptr<ripple::SLE const>
368 std::shared_ptr<data::BackendInterface const>
const& backend,
369 ripple::Keylet
const& keylet,
370 ripple::LedgerHeader
const& lgrInfo,
380std::vector<ripple::AccountID>
390std::vector<unsigned char>
406 ripple::AccountID
const& issuer,
407 boost::asio::yield_context yield
424 std::uint32_t sequence,
425 ripple::AccountID
const& account,
426 ripple::Currency
const& currency,
427 ripple::AccountID
const& issuer,
428 boost::asio::yield_context yield
444 std::uint32_t sequence,
445 ripple::Keylet
const& keylet,
446 std::vector<std::uint32_t>
const& flags,
447 boost::asio::yield_context yield
467 std::uint32_t sequence,
468 ripple::AccountID
const& account,
469 ripple::Currency
const& currency,
470 ripple::AccountID
const& issuer,
471 boost::asio::yield_context yield
488 std::uint32_t sequence,
489 ripple::AccountID
const& account,
490 ripple::Issue
const& asset,
491 ripple::Issue
const& asset2,
492 boost::asio::yield_context yield
510 std::uint32_t sequence,
511 ripple::STAmount
const& amount,
512 ripple::AccountID
const&
id,
513 boost::asio::yield_context yield
533 std::uint32_t sequence,
534 ripple::AccountID
const& account,
535 ripple::Currency
const& currency,
536 ripple::AccountID
const& issuer,
538 boost::asio::yield_context yield
556 std::uint32_t sequence,
557 ripple::AccountID
const& account,
558 ripple::Currency
const& currency,
559 ripple::AccountID
const& issuer,
560 bool const zeroIfFrozen,
561 boost::asio::yield_context yield
576 std::uint32_t sequence,
577 ripple::AccountID
const& issuer,
578 boost::asio::yield_context yield
593 std::uint32_t sequence,
594 ripple::AccountID
const&
id,
595 boost::asio::yield_context yield
612 std::vector<data::LedgerObject>
const& offers,
613 ripple::Book
const& book,
614 ripple::AccountID
const& takerID,
617 std::uint32_t ledgerSequence,
618 boost::asio::yield_context yield
631std::expected<ripple::Book, Status>
633 ripple::Currency pays,
634 ripple::AccountID payIssuer,
635 ripple::Currency gets,
636 ripple::AccountID getIssuer,
637 std::optional<std::string>
const& domain
646std::expected<ripple::Book, Status>
647parseBook(boost::json::object
const& request);
655std::expected<ripple::AccountID, Status>
684isAdminCmd(std::string
const& method, boost::json::object
const& request);
692std::expected<ripple::uint256, Status>
693getNFTID(boost::json::object
const& request);
703std::optional<std::string>
704encodeCTID(uint32_t ledgerSeq, uint16_t txnIndex, uint16_t networkId)
noexcept;
714inline std::optional<std::tuple<uint32_t, uint16_t, uint16_t>>
717 auto const getCTID64 = [](T
const ctid)
noexcept -> std::optional<uint64_t> {
718 if constexpr (std::is_convertible_v<T, std::string>) {
719 std::string
const ctidString(ctid);
720 static constexpr std::size_t kCTID_STRING_LENGTH = 16;
721 if (ctidString.length() != kCTID_STRING_LENGTH)
724 if (!boost::regex_match(ctidString, boost::regex(
"^[0-9A-F]+$")))
727 return std::stoull(ctidString,
nullptr, 16);
730 if constexpr (std::is_same_v<T, uint64_t>)
736 auto const ctidValue = getCTID64(ctid).value_or(0);
738 static constexpr uint64_t kCTID_PREFIX = 0xC000'0000'0000'0000ULL;
739 static constexpr uint64_t kCTID_PREFIX_MASK = 0xF000'0000'0000'0000ULL;
741 if ((ctidValue & kCTID_PREFIX_MASK) != kCTID_PREFIX)
744 uint32_t
const ledgerSeq = (ctidValue >> 32) & 0xFFFF'FFFUL;
745 uint16_t
const txnIndex = (ctidValue >> 16) & 0xFFFFU;
746 uint16_t
const networkId = ctidValue & 0xFFFFU;
747 return {{ledgerSeq, txnIndex, networkId}};
758template <
typename DurationType>
762 using boost::json::serialize;
765 static constexpr std::int64_t kDURATION_ERROR_THRESHOLD_SECONDS = 10;
767 auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
768 auto const seconds = std::chrono::duration_cast<std::chrono::seconds>(dur).count();
769 auto const msg = fmt::format(
770 "Request processing duration = {} milliseconds. request = {}", millis, serialize(
util::removeSecret(request))
773 if (seconds > kDURATION_ERROR_THRESHOLD_SECONDS) {
774 LOG(log.error()) << tag << msg;
775 }
else if (seconds > 1) {
776 LOG(log.warn()) << tag << msg;
778 LOG(log.info()) << tag << msg;
787std::optional<ripple::Seed>
802std::expected<AccountCursor, Status>
805 std::uint32_t sequence,
806 ripple::AccountID
const& accountID,
807 ripple::uint256 nextPage,
809 boost::asio::yield_context yield,
810 std::function<
void(ripple::SLE)> atOwnedNode
819std::optional<std::uint32_t>
831 std::shared_ptr<ripple::STTx const>
const& txn,
832 std::shared_ptr<ripple::TxMeta const>
const& meta
844std::optional<ripple::STAmount>
846 std::shared_ptr<ripple::STTx const>
const& txn,
847 std::shared_ptr<ripple::TxMeta const>
const& meta,
848 std::uint32_t ledgerSequence,
The interface of an amendment center.
Definition AmendmentCenterInterface.hpp:36
The interface to the database used by Clio.
Definition BackendInterface.hpp:140
Represents any tag decorator.
Definition Taggable.hpp:74
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:37
std::pair< std::shared_ptr< ripple::STTx const >, std::shared_ptr< ripple::STObject const > > deserializeTxPlusMeta(data::TransactionAndMetadata const &blobs)
Deserialize a TransactionAndMetadata into a pair of STTx and STObject.
Definition RPCHelpers.cpp:209
std::expected< ripple::uint256, Status > getNFTID(boost::json::object const &request)
Get the NFTID from the request.
Definition RPCHelpers.cpp:1542
std::uint64_t getStartHint(ripple::SLE const &sle, ripple::AccountID const &accountID)
Get the start hint for the account.
Definition RPCHelpers.cpp:572
ripple::Issue parseIssue(boost::json::object const &issue)
Parse the json object into a ripple::Issue object.
Definition RPCHelpers.cpp:1492
bool insertMPTIssuanceID(boost::json::object &metaJson, std::shared_ptr< ripple::STTx const > const &txn, std::shared_ptr< ripple::TxMeta const > const &meta)
Add "mpt_issuance_id" into MPTokenIssuanceCreate transaction json.
Definition RPCHelpers.cpp:367
bool isFrozen(BackendInterface const &backend, std::uint32_t sequence, ripple::AccountID const &account, ripple::Currency const ¤cy, ripple::AccountID const &issuer, boost::asio::yield_context yield)
Whether the account is frozen.
Definition RPCHelpers.cpp:928
bool insertDeliveredAmount(boost::json::object &metaJson, std::shared_ptr< ripple::STTx const > const &txn, std::shared_ptr< ripple::TxMeta const > const &meta, uint32_t date)
Add "DeliveredAmount" to the transaction json object.
Definition RPCHelpers.cpp:306
boost::json::object toJsonWithBinaryTx(data::TransactionAndMetadata const &txnPlusMeta, std::uint32_t const apiVersion)
Convert a TransactionAndMetadata to JSON object containing tx and metadata data in hex format....
Definition RPCHelpers.cpp:1558
bool isDeepFrozen(BackendInterface const &backend, std::uint32_t sequence, ripple::AccountID const &account, ripple::Currency const ¤cy, ripple::AccountID const &issuer, boost::asio::yield_context yield)
Whether the trustline is deep frozen.
Definition RPCHelpers.cpp:957
std::optional< AccountCursor > parseAccountCursor(std::optional< std::string > jsonCursor)
Parse the account cursor from the JSON.
Definition RPCHelpers.cpp:112
std::expected< ripple::LedgerHeader, Status > ledgerHeaderFromRequest(std::shared_ptr< data::BackendInterface const > const &backend, web::Context const &ctx)
Get ledger info from the request.
Definition RPCHelpers.cpp:470
std::vector< ripple::AccountID > getAccountsFromTransaction(boost::json::object const &transaction)
Get the account associated with a transaction.
Definition RPCHelpers.cpp:866
void logDuration(boost::json::object const &request, util::BaseTagDecorator const &tag, DurationType const &dur)
Log the duration of the request processing.
Definition RPCHelpers.hpp:760
ripple::STAmount ammAccountHolds(BackendInterface const &backend, std::uint32_t sequence, ripple::AccountID const &account, ripple::Currency const ¤cy, ripple::AccountID const &issuer, bool const zeroIfFrozen, boost::asio::yield_context yield)
Get the amount that an LPToken owner holds.
Definition RPCHelpers.cpp:1046
std::optional< std::string > encodeCTID(uint32_t ledgerSeq, uint16_t txnIndex, uint16_t networkId) noexcept
Encode CTID as string.
Definition RPCHelpers.cpp:289
boost::json::object generatePubLedgerMessage(ripple::LedgerHeader const &lgrInfo, ripple::Fees const &fees, std::string const &ledgerRange, std::uint32_t txnCount)
Generate a JSON object to publish ledger message.
std::optional< std::tuple< uint32_t, uint16_t, uint16_t > > decodeCTID(T const ctid) noexcept
Decode the CTID from a string or a uint64_t.
Definition RPCHelpers.hpp:715
std::expected< ripple::Book, Status > parseBook(ripple::Currency pays, ripple::AccountID payIssuer, ripple::Currency gets, ripple::AccountID getIssuer, std::optional< std::string > const &domain)
Parse the book from the request.
Definition RPCHelpers.cpp:1312
boost::json::array postProcessOrderBook(std::vector< data::LedgerObject > const &offers, ripple::Book const &book, ripple::AccountID const &takerID, data::BackendInterface const &backend, data::AmendmentCenterInterface const &amendmentCenter, std::uint32_t const ledgerSequence, boost::asio::yield_context yield)
Post process an order book.
Definition RPCHelpers.cpp:1198
bool isOwnedByAccount(ripple::SLE const &sle, ripple::AccountID const &accountID)
Check whether the SLE is owned by the account.
std::pair< boost::json::object, boost::json::object > toExpandedJson(data::TransactionAndMetadata const &blobs, std::uint32_t const apiVersion, NFTokenjson nftEnabled, std::optional< uint16_t > networkId)
Convert a TransactionAndMetadata to two JSON objects.
Definition RPCHelpers.cpp:253
std::optional< ripple::AccountID > accountFromStringStrict(std::string const &account)
Get a ripple::AccountID from its string representation.
Definition RPCHelpers.cpp:187
ripple::STAmount accountHolds(BackendInterface const &backend, data::AmendmentCenterInterface const &amendmentCenter, std::uint32_t sequence, ripple::AccountID const &account, ripple::Currency const ¤cy, ripple::AccountID const &issuer, bool const zeroIfFrozen, boost::asio::yield_context yield)
Get the amount that an account holds.
Definition RPCHelpers.cpp:1091
std::expected< ripple::LedgerHeader, Status > getLedgerHeaderFromHashOrSeq(BackendInterface const &backend, boost::asio::yield_context yield, std::optional< std::string > ledgerHash, std::optional< uint32_t > ledgerIndex, uint32_t maxSeq)
Get ledger info from hash or sequence.
Definition RPCHelpers.cpp:521
ripple::STAmount accountFunds(BackendInterface const &backend, data::AmendmentCenterInterface const &amendmentCenter, std::uint32_t const sequence, ripple::STAmount const &amount, ripple::AccountID const &id, boost::asio::yield_context yield)
Get the account funds.
Definition RPCHelpers.cpp:1029
boost::json::object toJson(ripple::STBase const &obj)
Convert STBase object to JSON.
Definition RPCHelpers.cpp:245
ripple::XRPAmount xrpLiquid(BackendInterface const &backend, std::uint32_t sequence, ripple::AccountID const &id, boost::asio::yield_context yield)
Get the XRP liquidity.
Definition RPCHelpers.cpp:994
std::expected< ripple::AccountID, Status > parseTaker(boost::json::value const &taker)
Parse the taker from the request.
Definition RPCHelpers.cpp:1478
bool canHaveDeliveredAmount(std::shared_ptr< ripple::STTx const > const &txn, std::shared_ptr< ripple::TxMeta const > const &meta)
Whether the transaction can have a delivered amount.
Definition RPCHelpers.cpp:171
std::shared_ptr< ripple::SLE const > read(std::shared_ptr< data::BackendInterface const > const &backend, ripple::Keylet const &keylet, ripple::LedgerHeader const &lgrInfo, web::Context const &context)
Read SLE from the backend.
Definition RPCHelpers.cpp:831
NFTokenjson
Enum for NFT json manipulation.
Definition RPCHelpers.hpp:81
std::optional< ripple::Seed > parseRippleLibSeed(boost::json::value const &value)
Parse a ripple-lib seed.
Definition RPCHelpers.cpp:846
void insertDeliverMaxAlias(boost::json::object &txJson, std::uint32_t const apiVersion)
Add "DeliverMax" which is the alias of "Amount" for "Payment" transaction to transaction json....
Definition RPCHelpers.cpp:386
bool specifiesCurrentOrClosedLedger(boost::json::object const &request)
Check whether the request specifies the current or closed ledger.
Definition RPCHelpers.cpp:1504
std::optional< ripple::STAmount > getDeliveredAmount(std::shared_ptr< ripple::STTx const > const &txn, std::shared_ptr< ripple::TxMeta const > const &meta, std::uint32_t const ledgerSequence, uint32_t date)
Get the delivered amount.
Definition RPCHelpers.cpp:143
ripple::Rate transferRate(BackendInterface const &backend, std::uint32_t sequence, ripple::AccountID const &issuer, boost::asio::yield_context yield)
Get the transfer rate.
Definition RPCHelpers.cpp:1176
bool isGlobalFrozen(BackendInterface const &backend, std::uint32_t sequence, ripple::AccountID const &issuer, boost::asio::yield_context yield)
Whether global frozen is set.
Definition RPCHelpers.cpp:885
boost::json::value toBoostJson(Json::Value const &value)
Convert a RippledJson to boost::json::value.
Definition RPCHelpers.cpp:405
std::expected< AccountCursor, Status > traverseOwnedNodes(BackendInterface const &backend, ripple::AccountID const &accountID, std::uint32_t sequence, std::uint32_t limit, std::optional< std::string > jsonCursor, boost::asio::yield_context yield, std::function< void(ripple::SLE)> atOwnedNode, bool nftIncluded)
Traverse nodes owned by an account.
Definition RPCHelpers.cpp:644
bool isLPTokenFrozen(BackendInterface const &backend, std::uint32_t sequence, ripple::AccountID const &account, ripple::Issue const &asset, ripple::Issue const &asset2, boost::asio::yield_context yield)
Whether the account that owns a LPToken is frozen for the assets in the pool.
Definition RPCHelpers.cpp:980
std::optional< std::uint32_t > parseStringAsUInt(std::string const &value)
Parse the string as a uint32_t.
Definition RPCHelpers.cpp:457
bool isAdminCmd(std::string const &method, boost::json::object const &request)
Check whether a request requires administrative privileges on rippled side.
Definition RPCHelpers.cpp:1517
std::expected< AccountCursor, Status > traverseNFTObjects(BackendInterface const &backend, std::uint32_t sequence, ripple::AccountID const &accountID, ripple::uint256 nextPage, std::uint32_t limit, boost::asio::yield_context yield, std::function< void(ripple::SLE)> atOwnedNode)
Traverse NFT objects and call the callback for each owned node.
Definition RPCHelpers.cpp:592
bool fetchAndCheckAnyFlagsExists(BackendInterface const &backend, std::uint32_t sequence, ripple::Keylet const &keylet, std::vector< std::uint32_t > const &flags, boost::asio::yield_context yield)
Fetches a ledger object and checks if any of the specified flag is set on the account.
Definition RPCHelpers.cpp:908
std::vector< unsigned char > ledgerHeaderToBlob(ripple::LedgerHeader const &info, bool includeHash)
Convert a ledger header to a blob.
Definition RPCHelpers.cpp:554
boost::json::object removeSecret(boost::json::object const &object)
Removes any detected secret information from a response JSON object.
Definition JsonUtils.hpp:67
Context that is used by the Webserver to pass around information about an incoming request.
Definition Context.hpp:40