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/UintTypes.h>
22#include <xrpl/protocol/jss.h>
24#include <grpcpp/support/status.h>
25#include <org/xrpl/rpc/v1/get_ledger_entry.pb.h>
40 unsigned const apiVersion)>;
42static std::expected<uint256, json::Value>
47 unsigned const apiVersion);
58 unsigned const apiVersion) -> std::expected<uint256, json::Value> {
63static std::expected<uint256, json::Value>
67 std::string const& expectedType =
"hex string or object")
76static std::expected<uint256, json::Value>
79 if (apiVersion > 2u && params.
isString())
82 if (index == jss::amendments.cStr())
84 if (index == jss::fee.cStr())
86 if (index == jss::nunl)
88 if (index == jss::hashes)
98static std::expected<uint256, json::Value>
102 [[maybe_unused]]
unsigned const apiVersion)
114static std::expected<uint256, json::Value>
118 [[maybe_unused]]
unsigned const apiVersion)
142static std::expected<uint256, json::Value>
146 [[maybe_unused]]
unsigned const apiVersion)
153 if (params[jss::bridge].isString())
163 params, jss::bridge_account,
"malformedBridgeAccount");
169 if (account.value() != bridge->door(chainType))
175static std::expected<uint256, json::Value>
179 [[maybe_unused]]
unsigned const apiVersion)
184static std::expected<uint256, json::Value>
188 [[maybe_unused]]
unsigned const apiVersion)
213static std::expected<uint256, json::Value>
217 [[maybe_unused]]
unsigned const apiVersion)
229 auto const authorize =
237static std::expected<STArray, json::Value>
243 "malformedAuthorizedCredentials", jss::authorized_credentials,
"array");
251 "malformedAuthorizedCredentials",
252 "Invalid field '" +
std::string(jss::authorized_credentials) +
253 "', array too long."));
260 "malformedAuthorizedCredentials",
261 "Invalid field '" +
std::string(jss::authorized_credentials) +
"', array empty."));
264 STArray arr(sfAuthorizeCredentials, n);
265 for (
auto const& jo : jv)
270 "malformedAuthorizedCredentials", jss::authorized_credentials,
"array of objects");
274 jo, {jss::issuer, jss::credential_type},
"malformedAuthorizedCredentials");
281 jo, jss::issuer,
"malformedAuthorizedCredentials");
291 credential.setAccountID(sfIssuer, *issuer);
292 credential.setFieldVL(sfCredentialType, *credentialType);
293 arr.
pushBack(std::move(credential));
299static std::expected<uint256, json::Value>
303 [[maybe_unused]]
unsigned const apiVersion)
310 if ((dp.
isMember(jss::authorized) == dp.
isMember(jss::authorized_credentials)))
314 "Must have exactly one of `authorized` and "
315 "`authorized_credentials`.");
331 "malformedAuthorized", jss::authorized,
"AccountID");
334 auto const& ac(dp[jss::authorized_credentials]);
336 if (!arr.has_value())
344 "malformedAuthorizedCredentials", jss::authorized_credentials,
"array");
350static std::expected<uint256, json::Value>
354 [[maybe_unused]]
unsigned const apiVersion)
365static std::expected<uint256, json::Value>
369 [[maybe_unused]]
unsigned const apiVersion)
376 if (params.
isMember(jss::sub_index) &&
378 params[jss::sub_index].
isBool()))
386 "malformedRequest",
"Must have exactly one of `owner` and `dir_root` fields.");
407 "malformedAddress", jss::owner,
"AccountID");
416static std::expected<uint256, json::Value>
420 [[maybe_unused]]
unsigned const apiVersion)
439static std::expected<uint256, json::Value>
444 [[maybe_unused]]
unsigned const apiVersion)
458static std::expected<uint256, json::Value>
462 unsigned const apiVersion)
467 auto const index = params.
asUInt();
478static std::expected<uint256, json::Value>
482 [[maybe_unused]]
unsigned const apiVersion)
499static std::expected<uint256, json::Value>
503 [[maybe_unused]]
unsigned const apiVersion)
521static std::expected<uint256, json::Value>
525 [[maybe_unused]]
unsigned const apiVersion)
532 auto const mptIssuanceID =
545static std::expected<uint256, json::Value>
549 [[maybe_unused]]
unsigned const apiVersion)
555 "malformedMPTokenIssuance", fieldName,
"Hash192");
561static std::expected<uint256, json::Value>
565 [[maybe_unused]]
unsigned const apiVersion)
570static std::expected<uint256, json::Value>
574 [[maybe_unused]]
unsigned const apiVersion)
581static std::expected<uint256, json::Value>
585 [[maybe_unused]]
unsigned const apiVersion)
603static std::expected<uint256, json::Value>
607 [[maybe_unused]]
unsigned const apiVersion)
626static std::expected<uint256, json::Value>
630 [[maybe_unused]]
unsigned const apiVersion)
635static std::expected<uint256, json::Value>
639 [[maybe_unused]]
unsigned const apiVersion)
649 "malformedRequest", fieldName,
"hex string or object");
664static std::expected<uint256, json::Value>
668 [[maybe_unused]]
unsigned const apiVersion)
677 if (
auto const value =
684 if (!jvRippleState[jss::accounts].isArray() || jvRippleState[jss::accounts].size() != 2)
687 "malformedRequest", jss::accounts,
"length-2 array of Accounts");
695 "malformedAddress", jss::accounts,
"array of Accounts");
700 "malformedRequest",
"Cannot have a trustline to self.");
703 if (!jvRippleState[jss::currency].isString() || jvRippleState[jss::currency] ==
"" ||
704 !
toCurrency(uCurrency, jvRippleState[jss::currency].asString()))
707 "malformedCurrency", jss::currency,
"Currency");
713static std::expected<uint256, json::Value>
717 [[maybe_unused]]
unsigned const apiVersion)
722static std::expected<uint256, json::Value>
726 [[maybe_unused]]
unsigned const apiVersion)
745static std::expected<uint256, json::Value>
749 [[maybe_unused]]
unsigned const apiVersion)
767static std::expected<uint256, json::Value>
771 [[maybe_unused]]
unsigned const apiVersion)
783 claimId, jss::xchain_owned_claim_id,
"malformedXChainOwnedClaimID");
793static std::expected<uint256, json::Value>
797 [[maybe_unused]]
unsigned const apiVersion)
810 jss::xchain_owned_create_account_claim_id,
811 "malformedXChainOwnedCreateAccountClaimID");
836 static auto kLedgerEntryParsers = std::to_array<LedgerEntry>({
837#pragma push_macro("LEDGER_ENTRY")
840#define LEDGER_ENTRY(tag, value, name, rpcName, fields) {jss::rpcName, parse##name, tag},
842#include <xrpl/protocol/detail/ledger_entries.macro>
845#pragma pop_macro("LEDGER_ENTRY")
846 {.fieldName = jss::index, .parseFunction =
parseIndex, .expectedType =
ltANY},
848 {.fieldName = jss::account_root,
850 .expectedType = ltACCOUNT_ROOT},
851 {.fieldName = jss::ripple_state,
853 .expectedType = ltRIPPLE_STATE},
856 auto const hasMoreThanOneMember = [&]() {
859 for (
auto const& ledgerEntry : kLedgerEntryParsers)
871 if (hasMoreThanOneMember)
888 for (
auto const& ledgerEntry : kLedgerEntryParsers)
892 expectedType = ledgerEntry.expectedType;
897 json::Value const& params = ledgerEntry.fieldName == jss::bridge
899 : context.
params[ledgerEntry.fieldName];
901 ledgerEntry.parseFunction(params, ledgerEntry.fieldName, context.
apiVersion);
903 return result.error();
905 uNodeIndex = result.value();
914 jvResult[jss::error] =
"unknownOption";
933 jvResult[jss::index] =
to_string(uNodeIndex);
943 bool bNodeBinary =
false;
954 if ((expectedType !=
ltANY) && (expectedType != sleNode->getType()))
979 org::xrpl::rpc::v1::GetLedgerEntryRequest
const& request = context.
params;
980 org::xrpl::rpc::v1::GetLedgerEntryResponse response;
981 grpc::Status
const status = grpc::Status::OK;
986 grpc::Status errorStatus;
989 errorStatus = grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, status.message());
993 errorStatus = grpc::Status(grpc::StatusCode::NOT_FOUND, status.message());
995 return {response, errorStatus};
1001 grpc::Status
const errorStatus{grpc::StatusCode::INVALID_ARGUMENT,
"index malformed"};
1002 return {response, errorStatus};
1008 grpc::Status
const errorStatus{grpc::StatusCode::NOT_FOUND,
"object not found"};
1009 return {response, errorStatus};
1015 auto& stateObject = *response.mutable_ledger_object();
1017 stateObject.set_key(request.key());
1018 *(response.mutable_ledger()) = request.ledger();
1019 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)
Blob const & peekData() const
An immutable linear range of bytes.
@ UInt
unsigned integer value
std::unexpected< json::Value > missingFieldError(json::StaticString const field, std::optional< std::string > err=std::nullopt)
std::expected< uint256, json::Value > requiredUInt256(json::Value const ¶ms, json::StaticString const fieldName, std::string const &err)
std::expected< STXChainBridge, json::Value > parseBridgeFields(json::Value const ¶ms)
std::optional< T > parse(json::Value const ¶m)
std::unexpected< json::Value > malformedError(std::string const &err, std::string const &message)
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 > invalidFieldError(std::string const &err, json::StaticString const field, std::string const &type)
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< std::uint32_t, json::Value > requiredUInt32(json::Value const ¶ms, json::StaticString const fieldName, std::string const &err)
std::expected< AccountID, json::Value > requiredAccountID(json::Value const ¶ms, json::StaticString const fieldName, std::string const &err)
std::expected< bool, json::Value > hasRequired(json::Value const ¶ms, std::initializer_list< json::StaticString > fields, std::optional< std::string > err=std::nullopt)
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.
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
Status ledgerFromRequest(T &ledger, GRPCContext< R > const &context)
Retrieves a ledger from a gRPC request context.
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
json::Value makeParamError(std::string const &message)
Returns a new json object that indicates invalid parameters.
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 loanBroker(AccountID const &owner, std::uint32_t seq) noexcept
Keylet const & negativeUNL() noexcept
The (fixed) index of the object containing the ledger negativeUNL.
Keylet did(AccountID const &account) noexcept
Keylet unchecked(uint256 const &key) noexcept
Any ledger entry.
Keylet mptokenIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Keylet escrow(AccountID const &src, std::uint32_t seq) noexcept
An escrow entry.
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 loan(uint256 const &loanBrokerID, std::uint32_t loanSeq) noexcept
Keylet offer(AccountID const &id, std::uint32_t seq) noexcept
An offer from an account.
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Keylet xChainCreateAccountClaimID(STXChainBridge const &bridge, std::uint64_t seq)
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet permissionedDomain(AccountID const &account, std::uint32_t seq) noexcept
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Keylet xChainClaimID(STXChainBridge const &bridge, std::uint64_t seq)
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const ¤cy) noexcept
The index of a trust line for a given currency.
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)
uint256 getTicketIndex(AccountID const &account, std::uint32_t uSequence)
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
json::Value doLedgerEntry(RPC::JsonContext &)
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)
std::pair< org::xrpl::rpc::v1::GetLedgerEntryResponse, grpc::Status > doLedgerEntryGrpc(RPC::GRPCContext< org::xrpl::rpc::v1::GetLedgerEntryRequest > &context)
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)
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