1#include <xrpld/rpc/Context.h>
2#include <xrpld/rpc/GRPCHandlers.h>
3#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
4#include <xrpld/rpc/handlers/ledger/LedgerEntryHelpers.h>
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/basics/strHex.h>
8#include <xrpl/json/json_errors.h>
9#include <xrpl/json/json_value.h>
10#include <xrpl/ledger/ReadView.h>
11#include <xrpl/ledger/helpers/CredentialHelpers.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/ErrorCodes.h>
14#include <xrpl/protocol/Indexes.h>
15#include <xrpl/protocol/Keylet.h>
16#include <xrpl/protocol/LedgerFormats.h>
17#include <xrpl/protocol/Protocol.h>
18#include <xrpl/protocol/SField.h>
19#include <xrpl/protocol/STArray.h>
20#include <xrpl/protocol/STXChainBridge.h>
21#include <xrpl/protocol/SeqProxy.h>
22#include <xrpl/protocol/UintTypes.h>
23#include <xrpl/protocol/jss.h>
25#include <grpcpp/support/status.h>
26#include <org/xrpl/rpc/v1/get_ledger_entry.pb.h>
41 unsigned const apiVersion)>;
43static std::expected<uint256, json::Value>
48 unsigned const apiVersion);
59 unsigned const apiVersion) -> std::expected<uint256, json::Value> {
64static std::expected<uint256, json::Value>
68 std::string const& expectedType =
"hex string or object")
77static std::expected<uint256, json::Value>
80 if (apiVersion > 2u && params.
isString())
83 if (index == jss::amendments.cStr())
85 if (index == jss::fee.cStr())
87 if (index == jss::nunl)
89 if (index == jss::hashes)
99static std::expected<uint256, json::Value>
103 [[maybe_unused]]
unsigned const apiVersion)
115static std::expected<uint256, json::Value>
119 [[maybe_unused]]
unsigned const apiVersion)
144static std::expected<uint256, json::Value>
148 [[maybe_unused]]
unsigned const apiVersion)
155 if (params[jss::bridge].isString())
165 params, jss::bridge_account,
"malformedBridgeAccount");
171 if (account.value() != bridge->door(chainType))
177static std::expected<uint256, json::Value>
181 [[maybe_unused]]
unsigned const apiVersion)
186static std::expected<uint256, json::Value>
190 [[maybe_unused]]
unsigned const apiVersion)
215static std::expected<uint256, json::Value>
219 [[maybe_unused]]
unsigned const apiVersion)
231 auto const authorize =
239static std::expected<STArray, json::Value>
245 "malformedAuthorizedCredentials", jss::authorized_credentials,
"array");
253 "malformedAuthorizedCredentials",
254 "Invalid field '" +
std::string(jss::authorized_credentials) +
255 "', array too long."));
262 "malformedAuthorizedCredentials",
263 "Invalid field '" +
std::string(jss::authorized_credentials) +
"', array empty."));
266 STArray arr(sfAuthorizeCredentials, n);
267 for (
auto const& jo : jv)
272 "malformedAuthorizedCredentials", jss::authorized_credentials,
"array of objects");
276 jo, {jss::issuer, jss::credential_type},
"malformedAuthorizedCredentials");
283 jo, jss::issuer,
"malformedAuthorizedCredentials");
293 credential.setAccountID(sfIssuer, *issuer);
294 credential.setFieldVL(sfCredentialType, *credentialType);
295 arr.
pushBack(std::move(credential));
301static std::expected<uint256, json::Value>
305 [[maybe_unused]]
unsigned const apiVersion)
312 if ((dp.
isMember(jss::authorized) == dp.
isMember(jss::authorized_credentials)))
316 "Must have exactly one of `authorized` and "
317 "`authorized_credentials`.");
333 "malformedAuthorized", jss::authorized,
"AccountID");
336 auto const& ac(dp[jss::authorized_credentials]);
338 if (!arr.has_value())
346 "malformedAuthorizedCredentials", jss::authorized_credentials,
"array");
352static std::expected<uint256, json::Value>
356 [[maybe_unused]]
unsigned const apiVersion)
367static std::expected<uint256, json::Value>
371 [[maybe_unused]]
unsigned const apiVersion)
378 if (params.
isMember(jss::sub_index) &&
380 params[jss::sub_index].
isBool()))
383 "malformedRequest", jss::sub_index,
"number");
389 "malformedRequest",
"Must have exactly one of `owner` and `dir_root` fields.");
410 "malformedAddress", jss::owner,
"AccountID");
419static std::expected<uint256, json::Value>
423 [[maybe_unused]]
unsigned const apiVersion)
443static std::expected<uint256, json::Value>
448 [[maybe_unused]]
unsigned const apiVersion)
462static std::expected<uint256, json::Value>
466 unsigned const apiVersion)
471 auto const index = params.
asUInt();
482static std::expected<uint256, json::Value>
486 [[maybe_unused]]
unsigned const apiVersion)
504static std::expected<uint256, json::Value>
508 [[maybe_unused]]
unsigned const apiVersion)
527static std::expected<uint256, json::Value>
531 [[maybe_unused]]
unsigned const apiVersion)
539 params, jss::mpt_issuance_id,
"malformedMPTIssuanceID");
551static std::expected<uint256, json::Value>
555 [[maybe_unused]]
unsigned const apiVersion)
561 "malformedMPTokenIssuance", fieldName,
"Hash192");
567static std::expected<uint256, json::Value>
571 [[maybe_unused]]
unsigned const apiVersion)
576static std::expected<uint256, json::Value>
580 [[maybe_unused]]
unsigned const apiVersion)
587static std::expected<uint256, json::Value>
591 [[maybe_unused]]
unsigned const apiVersion)
611static std::expected<uint256, json::Value>
615 [[maybe_unused]]
unsigned const apiVersion)
628 params, jss::oracle_document_id,
"malformedDocumentID");
635static std::expected<uint256, json::Value>
639 [[maybe_unused]]
unsigned const apiVersion)
644static std::expected<uint256, json::Value>
648 [[maybe_unused]]
unsigned const apiVersion)
658 "malformedRequest", fieldName,
"hex string or object");
674static std::expected<uint256, json::Value>
678 [[maybe_unused]]
unsigned const apiVersion)
687 if (
auto const value =
694 if (!jvRippleState[jss::accounts].isArray() || jvRippleState[jss::accounts].size() != 2)
697 "malformedRequest", jss::accounts,
"length-2 array of Accounts");
705 "malformedAddress", jss::accounts,
"array of Accounts");
710 "malformedRequest",
"Cannot have a trustline to self.");
713 if (!jvRippleState[jss::currency].isString() || jvRippleState[jss::currency] ==
"" ||
714 !
toCurrency(uCurrency, jvRippleState[jss::currency].asString()))
717 "malformedCurrency", jss::currency,
"Currency");
723static std::expected<uint256, json::Value>
727 [[maybe_unused]]
unsigned const apiVersion)
732static std::expected<uint256, json::Value>
736 [[maybe_unused]]
unsigned const apiVersion)
741 auto const sponsorID =
746 auto const sponseeID =
754static std::expected<uint256, json::Value>
758 [[maybe_unused]]
unsigned const apiVersion)
779static std::expected<uint256, json::Value>
783 [[maybe_unused]]
unsigned const apiVersion)
802static std::expected<uint256, json::Value>
806 [[maybe_unused]]
unsigned const apiVersion)
818 claimId, jss::xchain_owned_claim_id,
"malformedXChainOwnedClaimID");
828static std::expected<uint256, json::Value>
832 [[maybe_unused]]
unsigned const apiVersion)
845 jss::xchain_owned_create_account_claim_id,
846 "malformedXChainOwnedCreateAccountClaimID");
871 static auto kLedgerEntryParsers = std::to_array<LedgerEntry>({
872#pragma push_macro("LEDGER_ENTRY")
875#define LEDGER_ENTRY(tag, value, name, rpcName, fields) {jss::rpcName, parse##name, tag},
877#include <xrpl/protocol/detail/ledger_entries.macro>
880#pragma pop_macro("LEDGER_ENTRY")
881 {.fieldName = jss::index, .parseFunction =
parseIndex, .expectedType =
ltANY},
883 {.fieldName = jss::account_root,
885 .expectedType = ltACCOUNT_ROOT},
886 {.fieldName = jss::ripple_state,
888 .expectedType = ltRIPPLE_STATE},
891 auto const hasMoreThanOneMember = [&]() {
894 for (
auto const& ledgerEntry : kLedgerEntryParsers)
906 if (hasMoreThanOneMember)
923 for (
auto const& ledgerEntry : kLedgerEntryParsers)
927 expectedType = ledgerEntry.expectedType;
932 json::Value const& params = ledgerEntry.fieldName == jss::bridge
934 : context.
params[ledgerEntry.fieldName];
936 ledgerEntry.parseFunction(params, ledgerEntry.fieldName, context.
apiVersion);
938 return result.error();
940 uNodeIndex = result.value();
949 jvResult[jss::error] =
"unknownOption";
968 jvResult[jss::index] =
to_string(uNodeIndex);
978 bool bNodeBinary =
false;
989 if ((expectedType !=
ltANY) && (expectedType != sleNode->getType()))
1014 org::xrpl::rpc::v1::GetLedgerEntryRequest
const& request = context.
params;
1015 org::xrpl::rpc::v1::GetLedgerEntryResponse response;
1016 grpc::Status
const status = grpc::Status::OK;
1021 grpc::Status errorStatus;
1024 errorStatus = grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, status.message());
1028 errorStatus = grpc::Status(grpc::StatusCode::NOT_FOUND, status.message());
1030 return {response, errorStatus};
1036 grpc::Status
const errorStatus{grpc::StatusCode::INVALID_ARGUMENT,
"index malformed"};
1037 return {response, errorStatus};
1043 grpc::Status
const errorStatus{grpc::StatusCode::NOT_FOUND,
"object not found"};
1044 return {response, errorStatus};
1050 auto& stateObject = *response.mutable_ledger_object();
1052 stateObject.set_key(request.key());
1053 *(response.mutable_ledger()) = request.ledger();
1054 return {response, status};
Lightweight wrapper to tag static string.
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
UInt size() const
Number of values in array or object.
bool isConvertibleTo(ValueType other) const
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.
static std::optional< BaseUInt > fromVoidChecked(T const &from)
void pushBack(STObject const &object)
static STObject makeInnerObject(SField const &name)
static ChainType srcChain(bool wasLockingChainSend)
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
static constexpr SeqProxy rawTicket(std::uint32_t v)
Factory function to return a ticket-based SeqProxy.
Blob const & peekData() const
An immutable linear range of bytes.
@ UInt
unsigned integer value
std::set< std::pair< AccountID, Slice > > makeSorted(STArray const &credentials)
Keylet computation functions.
Keylet const & skip() noexcept
The index of the "short" skip list.
Keylet oracle(AccountID const &account, std::uint32_t const documentID) noexcept
Keylet const & negativeUNL() noexcept
The (fixed) index of the object containing the ledger negativeUNL.
Keylet escrow(AccountID const &src, SeqProxy const &seq) noexcept
An escrow entry.
Keylet did(AccountID const &account) noexcept
Keylet offer(AccountID const &id, SeqProxy const &seq) noexcept
An offer from an account.
Keylet unchecked(uint256 const &key) noexcept
Any ledger entry.
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Keylet loan(uint256 const &loanBrokerID, SeqProxy const &loanSeq) noexcept
Keylet bridge(STXChainBridge const &bridge, STXChainBridge::ChainType chainType)
Keylet const & feeSettings() noexcept
The (fixed) index of the object containing the ledger fees.
Keylet const & amendments() noexcept
The index of the amendment table.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Keylet ticket(AccountID const &id, SeqProxy const &ticketSeq)
A ticket belonging to an account.
Keylet page(uint256 const &root, std::uint64_t const index=0) noexcept
A page in a directory.
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Keylet permissionedDomain(AccountID const &account, SeqProxy const &seq) noexcept
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
Keylet loanBroker(AccountID const &owner, SeqProxy const &seq) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet xChainClaimID(STXChainBridge const &bridge, std::uint64_t const seq)
Keylet sponsorship(AccountID const &sponsor, AccountID const &sponsee) noexcept
A Sponsorship.
Keylet xChainCreateAccountClaimID(STXChainBridge const &bridge, std::uint64_t const seq)
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
std::optional< T > parse(json::Value const ¶m)
std::expected< AccountID, json::Value > requiredAccountID(json::Value const ¶ms, json::StaticString const fieldName, std::string const &err)
std::expected< STXChainBridge, json::Value > parseBridgeFields(json::Value const ¶ms)
std::expected< Asset, json::Value > requiredAsset(json::Value const ¶ms, json::StaticString const fieldName, std::string const &err)
std::expected< uint192, json::Value > requiredUInt192(json::Value const ¶ms, json::StaticString const fieldName, std::string const &err)
std::expected< uint256, json::Value > requiredUInt256(json::Value const ¶ms, json::StaticString const fieldName, std::string const &err)
std::expected< Blob, json::Value > requiredHexBlob(json::Value const ¶ms, json::StaticString const fieldName, std::size_t maxLength, std::string const &err)
std::unexpected< json::Value > malformedError(std::string const &err, std::string const &message)
std::expected< std::uint32_t, json::Value > requiredUInt32(json::Value const ¶ms, json::StaticString const fieldName, std::string const &err)
std::unexpected< json::Value > missingFieldError(json::StaticString const field, std::optional< std::string > err=std::nullopt)
std::unexpected< json::Value > invalidFieldError(std::string const &err, json::StaticString const field, std::string const &type)
std::expected< bool, json::Value > hasRequired(json::Value const ¶ms, std::initializer_list< json::StaticString > fields, std::optional< std::string > err=std::nullopt)
Status ledgerFromRequest(T &ledger, GRPCContext< R > const &context)
Retrieves a ledger from a gRPC request context.
json::Value makeParamError(std::string const &message)
Returns a new json object that indicates invalid parameters.
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext const &context, json::Value &result)
Looks up a ledger from a request and fills a json::Value with ledger data.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static std::expected< uint256, json::Value > parsePermissionedDomain(json::Value const &pd, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseLoanBroker(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
@ RpcUnexpectedLedgerType
static std::expected< uint256, json::Value > parseNFTokenPage(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< STArray, json::Value > parseAuthorizeCredentials(json::Value const &jv)
auto const parseFeeSettings
std::function< std::expected< uint256, json::Value >( json::Value const &, json::StaticString const, unsigned const apiVersion)> FunctionType
static std::expected< uint256, json::Value > parseMPTokenIssuance(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseVault(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseSignerList(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
constexpr std::size_t kMaxCredentialTypeLength
The maximum length of a CredentialType inside a Credential.
std::string strHex(FwdIt begin, FwdIt end)
static std::expected< uint256, json::Value > parseBridge(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parsePayChannel(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseOracle(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
static bool authorized(Port const &port, std::map< std::string, std::string > const &h)
static std::expected< uint256, json::Value > parseLedgerHashes(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
static std::expected< uint256, json::Value > parseDID(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static FunctionType fixed(Keylet const &keylet)
static std::expected< uint256, json::Value > parseObjectID(json::Value const ¶ms, json::StaticString const fieldName, std::string const &expectedType="hex string or object")
static std::expected< uint256, json::Value > parseAccountRoot(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
std::string to_string(BaseUInt< Bits, Tag > const &a)
auto const parseAmendments
static std::expected< uint256, json::Value > parseSponsorship(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseIndex(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseXChainOwnedClaimID(json::Value const &claimId, json::StaticString const fieldName, unsigned const apiVersion)
constexpr std::size_t kMaxCredentialsArraySize
The maximum number of credentials can be passed in array.
static std::expected< uint256, json::Value > parseOffer(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseDelegate(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
auto const parseNegativeUNL
static std::expected< uint256, json::Value > parseEscrow(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseNFTokenOffer(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseCheck(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
json::Value doLedgerEntry(rpc::JsonContext &)
std::pair< org::xrpl::rpc::v1::GetLedgerEntryResponse, grpc::Status > doLedgerEntryGrpc(rpc::GRPCContext< org::xrpl::rpc::v1::GetLedgerEntryRequest > &context)
static std::expected< uint256, json::Value > parseRippleState(json::Value const &jvRippleState, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseTicket(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseMPToken(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseAMM(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseCredential(json::Value const &cred, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseDirectoryNode(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
LedgerEntryType
Identifiers for on-ledger objects.
@ ltANY
A special type, matching any ledger entry type.
static std::expected< uint256, json::Value > parseXChainOwnedCreateAccountClaimID(json::Value const &claimId, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseDepositPreauth(json::Value const &dp, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseLoan(json::Value const ¶ms, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< uint256, json::Value > parseFixed(Keylet const &keylet, json::Value const ¶ms, json::StaticString const &fieldName, unsigned const apiVersion)
A pair of SHAMap key and LedgerEntryType.
json::StaticString fieldName
FunctionType parseFunction
LedgerEntryType expectedType