1#include <xrpld/rpc/Context.h>
2#include <xrpld/rpc/detail/RPCHelpers.h>
3#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
4#include <xrpld/rpc/detail/SpecBridge.hpp>
5#include <xrpld/rpc/detail/Tuning.h>
7#include <xrpl/basics/base_uint.h>
8#include <xrpl/beast/utility/Zero.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/json/json_value.h>
11#include <xrpl/ledger/ReadView.h>
12#include <xrpl/ledger/helpers/SponsorHelpers.h>
13#include <xrpl/protocol/AccountID.h>
14#include <xrpl/protocol/ErrorCodes.h>
15#include <xrpl/protocol/Indexes.h>
16#include <xrpl/protocol/LedgerFormats.h>
17#include <xrpl/protocol/RPCErr.h>
18#include <xrpl/protocol/SField.h>
19#include <xrpl/protocol/jss.h>
20#include <xrpl/protocol/nftPageMask.h>
21#include <xrpl/resource/Fees.h>
55 if (!dirIndex.
isZero() && !ledger.
read({ltDIR_NODE, dirIndex}))
61 return it != typeFilter.
end();
64 auto const sponsoredMatchesFilter =
68 return sponsor.has_value() == *sponsoredFilter;
73 bool iterateNFTPages =
74 (!typeFilter.has_value() || typeMatchesFilter(typeFilter.value(), ltNFTOKEN_PAGE)) &&
80 if (iterateNFTPages && entryIndex.
isNonZero())
86 iterateNFTPages =
false;
93 uint32_t limitLeft = limit;
99 entryIndex.
isZero() ? firstNFTPage :
Keylet{ltNFTOKEN_PAGE, entryIndex};
105 auto currentPage = ledger.
read(
Keylet{ltNFTOKEN_PAGE, currentKey});
110 bool const canAppendNFT = sponsoredMatchesFilter(nftSponsor);
112 jvObjects.append(currentPage->getJson());
113 auto const npm = (*currentPage)[~sfNextPageMin];
116 currentPage = ledger.
read(
Keylet(ltNFTOKEN_PAGE, *npm));
120 currentPage =
nullptr;
123 if (--limitLeft == 0 && currentPage)
125 jvResult[jss::limit] = limit;
144 auto startEntryFound =
false;
149 startEntryFound =
true;
152 auto dir = ledger.
read({ltDIR_NODE, dirIndex});
158 if (limitLeft >= limit)
173 auto const& dirEntries = dir->getFieldV256(sfIndexes);
174 auto entryIter = dirEntries.begin();
176 if (!startEntryFound)
178 entryIter =
std::find(entryIter, dirEntries.end(), entryIndex);
179 if (entryIter == dirEntries.end())
182 startEntryFound =
true;
187 if (itemsAdded == limitLeft && limitLeft < limit && entryIter != dirEntries.end())
189 jvResult[jss::limit] = limit;
194 for (; entryIter != dirEntries.end(); ++entryIter)
200 UNREACHABLE(
"xrpl::doAccountObjects : null SLE");
205 bool canAppend =
true;
207 if (typeFilter.has_value() &&
208 !typeMatchesFilter(typeFilter.value(), sleNode->getType()))
215 if (sleNode->getType() == ltRIPPLE_STATE)
226 if (!sponsoredMatchesFilter(sponsor))
232 if (++itemsAdded == limitLeft)
234 if (++entryIter != dirEntries.end())
236 jvResult[jss::limit] = limit;
245 auto const nodeIndex = dir->getFieldU64(sfIndexNext);
250 dir = ledger.
read({ltDIR_NODE, dirIndex});
254 if (itemsAdded == limitLeft)
256 auto const& currentDirEntries = dir->getFieldV256(sfIndexes);
257 if (!currentDirEntries.empty())
259 jvResult[jss::limit] = limit;
260 jvResult[jss::marker] =
272 auto const& params = context.
params;
273 if (!params.isMember(jss::account))
276 if (!params[jss::account].isString())
281 if (ledger ==
nullptr)
290 auto const accountID{
id.value()};
297 if (params.isMember(jss::deletion_blockers_only) &&
298 params[jss::deletion_blockers_only].asBool())
304 }
static constexpr kDeletionBlockers[] = {
305 {.name = jss::check, .type = ltCHECK},
306 {.name = jss::escrow, .type = ltESCROW},
307 {.name = jss::nft_page, .type = ltNFTOKEN_PAGE},
308 {.name = jss::payment_channel, .type = ltPAYCHAN},
309 {.name = jss::state, .type = ltRIPPLE_STATE},
310 {.name = jss::mpt_issuance, .type = ltMPTOKEN_ISSUANCE},
311 {.name = jss::mptoken, .type = ltMPTOKEN},
312 {.name = jss::permissioned_domain, .type = ltPERMISSIONED_DOMAIN},
313 {.name = jss::vault, .type = ltVAULT},
314 {.name = jss::sponsorship, .type = ltSPONSORSHIP},
315 {.name = jss::transaction_proposal, .type = ltTRANSACTION_PROPOSAL},
319 typeFilter->reserve(
std::size(kDeletionBlockers));
321 for (
auto [name, type] : kDeletionBlockers)
323 if (params.isMember(jss::type) && name != params[jss::type])
328 typeFilter->push_back(type);
341 rpc::injectSpecError(result, rpcStatus);
350 unsigned int limit = 0;
356 if (params.isMember(jss::marker))
358 auto const& marker = params[jss::marker];
359 if (!marker.isString())
362 auto const& markerStr = marker.asString();
363 auto const& idx = markerStr.find(
',');
364 if (idx == std::string::npos)
367 if (!dirIndex.
parseHex(markerStr.substr(0, idx)))
370 if (!entryIndex.
parseHex(markerStr.substr(idx + 1)))
375 if (params.isMember(jss::sponsored))
377 auto const& sponsoredJv = params[jss::sponsored];
378 if (!sponsoredJv.isBool())
381 sponsoredFilter = sponsoredJv.asBool();
385 *ledger, accountID, typeFilter, dirIndex, entryIndex, limit, sponsoredFilter, result))
388 result[jss::account] =
toBase58(accountID);
Lightweight wrapper to tag static string.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
virtual std::optional< key_type > succ(key_type const &key, std::optional< key_type > const &last=std::nullopt) const =0
Return the key of the next state item.
@ Array
array value (ordered list)
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet nftokenPageMin(AccountID const &owner)
NFT page keylets.
Keylet nftokenPageMax(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Keylet page(UInt256 const &root, std::uint64_t const index=0) noexcept
A page in a directory.
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet child(UInt256 const &key) noexcept
Any item that can be in an owner dir.
constexpr UInt256 kPageMask(std::string_view("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))
Charge const kFeeMediumBurdenRpc
static constexpr LimitRange kAccountObjects
Limits for the account_objects command.
json::Value expectedFieldError(std::string const &name, std::string const &type)
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
json::Value invalidFieldError(std::string const &name)
::rpc::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 missingFieldError(std::string const &name)
std::pair<::rpc::Status, LedgerEntryType > chooseLedgerEntryType(json::Value const ¶ms)
Chooses the ledger entry type based on RPC parameters.
bool isAccountObjectsValidType(LedgerEntryType const &type)
Checks if the type is a valid filtering type for the account_objects method.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::optional< AccountID > getLedgerEntryReserveSponsorID(SLE::ConstRef sle, SF_ACCOUNT const &field=sfSponsor)
Return the AccountID stored in the given sponsor field of a ledger entry, or nullopt if absent.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
bool getAccountObjects(ReadView const &ledger, AccountID const &account, std::optional< std::vector< LedgerEntryType > > const &typeFilter, UInt256 dirIndex, UInt256 entryIndex, std::uint32_t const limit, std::optional< bool > const sponsoredFilter, json::Value &jvResult)
Gathers all objects for an account in a ledger.
Number root(Number f, unsigned d)
std::string to_string(BaseUInt< Bits, Tag > const &a)
json::Value rpcError(ErrorCodeI iError)
json::Value doAccountObjects(rpc::JsonContext &context)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
LedgerEntryType
Identifiers for on-ledger objects.
@ ltANY
A special type, matching any ledger entry type.
bool isLedgerEntrySupportedBySponsorship(SLE const &sle)
Whether this ledger entry type can have a reserve sponsor attached to it.
A pair of SHAMap key and LedgerEntryType.
resource::Charge & loadType