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/Tuning.h>
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/beast/utility/Zero.h>
8#include <xrpl/beast/utility/instrumentation.h>
9#include <xrpl/json/json_value.h>
10#include <xrpl/ledger/ReadView.h>
11#include <xrpl/ledger/helpers/SponsorHelpers.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/ErrorCodes.h>
14#include <xrpl/protocol/Indexes.h>
15#include <xrpl/protocol/LedgerFormats.h>
16#include <xrpl/protocol/RPCErr.h>
17#include <xrpl/protocol/SField.h>
18#include <xrpl/protocol/jss.h>
19#include <xrpl/protocol/nftPageMask.h>
20#include <xrpl/resource/Fees.h>
54 if (!dirIndex.
isZero() && !ledger.
read({ltDIR_NODE, dirIndex}))
60 return it != typeFilter.
end();
66 return sponsor.has_value() == *sponsoredFilter;
71 bool iterateNFTPages =
72 (!typeFilter.has_value() || typeMatchesFilter(typeFilter.value(), ltNFTOKEN_PAGE)) &&
78 if (iterateNFTPages && entryIndex.
isNonZero())
84 iterateNFTPages =
false;
91 uint32_t limitLeft = limit;
97 entryIndex.
isZero() ? firstNFTPage :
Keylet{ltNFTOKEN_PAGE, entryIndex};
103 auto currentPage = ledger.
read(
Keylet{ltNFTOKEN_PAGE, currentKey});
108 bool const canAppendNFT = sponsoredMatchesFilter(nftSponsor);
110 jvObjects.append(currentPage->getJson());
111 auto const npm = (*currentPage)[~sfNextPageMin];
114 currentPage = ledger.
read(
Keylet(ltNFTOKEN_PAGE, *npm));
118 currentPage =
nullptr;
121 if (--limitLeft == 0 && currentPage)
123 jvResult[jss::limit] = limit;
142 auto startEntryFound =
false;
147 startEntryFound =
true;
150 auto dir = ledger.
read({ltDIR_NODE, dirIndex});
156 if (limitLeft >= limit)
171 auto const& dirEntries = dir->getFieldV256(sfIndexes);
172 auto entryIter = dirEntries.begin();
174 if (!startEntryFound)
176 entryIter =
std::find(entryIter, dirEntries.end(), entryIndex);
177 if (entryIter == dirEntries.end())
180 startEntryFound =
true;
185 if (itemsAdded == limitLeft && limitLeft < limit && entryIter != dirEntries.end())
187 jvResult[jss::limit] = limit;
192 for (; entryIter != dirEntries.end(); ++entryIter)
198 UNREACHABLE(
"xrpl::doAccountObjects : null SLE");
203 bool canAppend =
true;
205 if (typeFilter.has_value() &&
206 !typeMatchesFilter(typeFilter.value(), sleNode->getType()))
213 if (sleNode->getType() == ltRIPPLE_STATE)
224 if (!sponsoredMatchesFilter(sponsor))
230 if (++itemsAdded == limitLeft)
232 if (++entryIter != dirEntries.end())
234 jvResult[jss::limit] = limit;
243 auto const nodeIndex = dir->getFieldU64(sfIndexNext);
248 dir = ledger.
read({ltDIR_NODE, dirIndex});
252 if (itemsAdded == limitLeft)
254 auto const& currentDirEntries = dir->getFieldV256(sfIndexes);
255 if (!currentDirEntries.empty())
257 jvResult[jss::limit] = limit;
258 jvResult[jss::marker] =
270 auto const& params = context.
params;
271 if (!params.isMember(jss::account))
274 if (!params[jss::account].isString())
279 if (ledger ==
nullptr)
288 auto const accountID{
id.value()};
295 if (params.isMember(jss::deletion_blockers_only) &&
296 params[jss::deletion_blockers_only].asBool())
302 }
static constexpr kDeletionBlockers[] = {
303 {.name = jss::check, .type = ltCHECK},
304 {.name = jss::escrow, .type = ltESCROW},
305 {.name = jss::nft_page, .type = ltNFTOKEN_PAGE},
306 {.name = jss::payment_channel, .type = ltPAYCHAN},
307 {.name = jss::state, .type = ltRIPPLE_STATE},
308 {.name = jss::xchain_owned_claim_id, .type = ltXCHAIN_OWNED_CLAIM_ID},
309 {.name = jss::xchain_owned_create_account_claim_id,
310 .type = ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID},
311 {.name = jss::bridge, .type = ltBRIDGE},
312 {.name = jss::mpt_issuance, .type = ltMPTOKEN_ISSUANCE},
313 {.name = jss::mptoken, .type = ltMPTOKEN},
314 {.name = jss::permissioned_domain, .type = ltPERMISSIONED_DOMAIN},
315 {.name = jss::vault, .type = ltVAULT},
316 {.name = jss::sponsorship, .type = ltSPONSORSHIP},
320 typeFilter->reserve(
std::size(kDeletionBlockers));
322 for (
auto [name, type] : kDeletionBlockers)
324 if (params.isMember(jss::type) && name != params[jss::type])
329 typeFilter->push_back(type);
342 rpcStatus.inject(result);
351 unsigned int limit = 0;
357 if (params.isMember(jss::marker))
359 auto const& marker = params[jss::marker];
360 if (!marker.isString())
363 auto const& markerStr = marker.asString();
364 auto const& idx = markerStr.find(
',');
365 if (idx == std::string::npos)
368 if (!dirIndex.
parseHex(markerStr.substr(0, idx)))
371 if (!entryIndex.
parseHex(markerStr.substr(idx + 1)))
376 if (params.isMember(jss::sponsored))
378 auto const& sponsoredJv = params[jss::sponsored];
379 if (!sponsoredJv.isBool())
382 sponsoredFilter = sponsoredJv.asBool();
386 *ledger, accountID, typeFilter, dirIndex, entryIndex, limit, sponsoredFilter, result))
389 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 child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Keylet page(uint256 const &root, std::uint64_t const index=0) noexcept
A page in a directory.
Keylet nftokenPageMax(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Keylet account(AccountID const &id) noexcept
AccountID root.
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)
std::pair< rpc::Status, LedgerEntryType > chooseLedgerEntryType(json::Value const ¶ms)
Chooses the ledger entry type based on RPC parameters.
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)
json::Value missingFieldError(std::string const &name)
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.
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::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Number root(Number f, unsigned d)
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.
std::string to_string(BaseUInt< Bits, Tag > const &a)
std::optional< AccountID > getLedgerEntryReserveSponsorID(SLE::const_ref sle, SF_ACCOUNT const &field=sfSponsor)
Return the AccountID stored in the given sponsor field of a ledger entry, or nullopt if absent.
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