rippled
Loading...
Searching...
No Matches
AccountObjects.cpp
1#include <xrpld/app/tx/detail/NFTokenUtils.h>
2#include <xrpld/rpc/Context.h>
3#include <xrpld/rpc/detail/RPCHelpers.h>
4#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
5#include <xrpld/rpc/detail/Tuning.h>
6
7#include <xrpl/ledger/ReadView.h>
8#include <xrpl/protocol/ErrorCodes.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/LedgerFormats.h>
11#include <xrpl/protocol/RPCErr.h>
12#include <xrpl/protocol/jss.h>
13#include <xrpl/protocol/nftPageMask.h>
14#include <xrpl/resource/Fees.h>
15
16#include <string>
17
18namespace xrpl {
19
33{
34 auto const& params = context.params;
35 if (!params.isMember(jss::account))
36 return RPC::missing_field_error(jss::account);
37
38 if (!params[jss::account].isString())
39 return RPC::invalid_field_error(jss::account);
40
41 auto id = parseBase58<AccountID>(params[jss::account].asString());
42 if (!id)
43 {
45 }
46
48 auto result = RPC::lookupLedger(ledger, context);
49 if (ledger == nullptr)
50 return result;
51 auto const accountID{id.value()};
52
53 if (!ledger->exists(keylet::account(accountID)))
55
56 unsigned int limit;
57 if (auto err = readLimitField(limit, RPC::Tuning::accountNFTokens, context))
58 return *err;
59
60 uint256 marker;
61 bool const markerSet = params.isMember(jss::marker);
62
63 if (markerSet)
64 {
65 auto const& m = params[jss::marker];
66 if (!m.isString())
67 return RPC::expected_field_error(jss::marker, "string");
68
69 if (!marker.parseHex(m.asString()))
70 return RPC::invalid_field_error(jss::marker);
71 }
72
73 auto const first = keylet::nftpage(keylet::nftpage_min(accountID), marker);
74 auto const last = keylet::nftpage_max(accountID);
75
76 auto cp = ledger->read(Keylet(
77 ltNFTOKEN_PAGE,
78 ledger->succ(first.key, last.key.next()).value_or(last.key)));
79
80 std::uint32_t cnt = 0;
81 auto& nfts = (result[jss::account_nfts] = Json::arrayValue);
82
83 // Continue iteration from the current page:
84 bool pastMarker = marker.isZero();
85 bool markerFound = false;
86 uint256 const maskedMarker = marker & nft::pageMask;
87 while (cp)
88 {
89 auto arr = cp->getFieldArray(sfNFTokens);
90
91 for (auto const& o : arr)
92 {
93 // Scrolling past the marker gets weird. We need to look at
94 // a couple of conditions.
95 //
96 // 1. If the low 96-bits don't match, then we compare only
97 // against the low 96-bits, since that's what determines
98 // the sort order of the pages.
99 //
100 // 2. However, within one page there can be a number of
101 // NFTokenIDs that all have the same low 96 bits. If we're
102 // in that case then we need to compare against the full
103 // 256 bits.
104 uint256 const nftokenID = o[sfNFTokenID];
105 uint256 const maskedNftokenID = nftokenID & nft::pageMask;
106
107 if (!pastMarker)
108 {
109 if (maskedNftokenID < maskedMarker)
110 continue;
111
112 if (maskedNftokenID == maskedMarker && nftokenID < marker)
113 continue;
114
115 if (nftokenID == marker)
116 {
117 markerFound = true;
118 continue;
119 }
120 }
121
122 if (markerSet && !markerFound)
123 return RPC::invalid_field_error(jss::marker);
124
125 pastMarker = true;
126
127 {
128 Json::Value& obj = nfts.append(o.getJson(JsonOptions::none));
129
130 // Pull out the components of the nft ID.
131 obj[sfFlags.jsonName] = nft::getFlags(nftokenID);
132 obj[sfIssuer.jsonName] = to_string(nft::getIssuer(nftokenID));
133 obj[sfNFTokenTaxon.jsonName] =
134 nft::toUInt32(nft::getTaxon(nftokenID));
135 obj[jss::nft_serial] = nft::getSerial(nftokenID);
136 if (std::uint16_t xferFee = {nft::getTransferFee(nftokenID)})
137 obj[sfTransferFee.jsonName] = xferFee;
138 }
139
140 if (++cnt == limit)
141 {
142 result[jss::limit] = limit;
143 result[jss::marker] = to_string(o.getFieldH256(sfNFTokenID));
144 return result;
145 }
146 }
147
148 if (auto npm = (*cp)[~sfNextPageMin])
149 cp = ledger->read(Keylet(ltNFTOKEN_PAGE, *npm));
150 else
151 cp = nullptr;
152 }
153
154 if (markerSet && !markerFound)
155 return RPC::invalid_field_error(jss::marker);
156
157 result[jss::account] = toBase58(accountID);
159 return result;
160}
161
171bool
173 ReadView const& ledger,
174 AccountID const& account,
176 uint256 dirIndex,
177 uint256 entryIndex,
178 std::uint32_t const limit,
179 Json::Value& jvResult)
180{
181 // check if dirIndex is valid
182 if (!dirIndex.isZero() && !ledger.read({ltDIR_NODE, dirIndex}))
183 return false;
184
185 auto typeMatchesFilter = [](std::vector<LedgerEntryType> const& typeFilter,
186 LedgerEntryType ledgerType) {
187 auto it = std::find(typeFilter.begin(), typeFilter.end(), ledgerType);
188 return it != typeFilter.end();
189 };
190
191 // if dirIndex != 0, then all NFTs have already been returned. only
192 // iterate NFT pages if the filter says so AND dirIndex == 0
193 bool iterateNFTPages =
194 (!typeFilter.has_value() ||
195 typeMatchesFilter(typeFilter.value(), ltNFTOKEN_PAGE)) &&
196 dirIndex == beast::zero;
197
198 Keylet const firstNFTPage = keylet::nftpage_min(account);
199
200 // we need to check the marker to see if it is an NFTTokenPage index.
201 if (iterateNFTPages && entryIndex != beast::zero)
202 {
203 // if it is we will try to iterate the pages up to the limit
204 // and then change over to the owner directory
205
206 if (firstNFTPage.key != (entryIndex & ~nft::pageMask))
207 iterateNFTPages = false;
208 }
209
210 auto& jvObjects = (jvResult[jss::account_objects] = Json::arrayValue);
211
212 // this is a mutable version of limit, used to seamlessly switch
213 // to iterating directory entries when nftokenpages are exhausted
214 uint32_t mlimit = limit;
215
216 // iterate NFTokenPages preferentially
217 if (iterateNFTPages)
218 {
219 Keylet const first = entryIndex == beast::zero
220 ? firstNFTPage
221 : Keylet{ltNFTOKEN_PAGE, entryIndex};
222
223 Keylet const last = keylet::nftpage_max(account);
224
225 // current key
226 uint256 ck = ledger.succ(first.key, last.key.next()).value_or(last.key);
227
228 // current page
229 auto cp = ledger.read(Keylet{ltNFTOKEN_PAGE, ck});
230
231 while (cp)
232 {
233 jvObjects.append(cp->getJson(JsonOptions::none));
234 auto const npm = (*cp)[~sfNextPageMin];
235 if (npm)
236 cp = ledger.read(Keylet(ltNFTOKEN_PAGE, *npm));
237 else
238 cp = nullptr;
239
240 if (--mlimit == 0)
241 {
242 if (cp)
243 {
244 jvResult[jss::limit] = limit;
245 jvResult[jss::marker] = std::string("0,") + to_string(ck);
246 return true;
247 }
248 }
249
250 if (!npm)
251 break;
252
253 ck = *npm;
254 }
255
256 // if execution reaches here then we're about to transition
257 // to iterating the root directory (and the conventional
258 // behaviour of this RPC function.) Therefore we should
259 // zero entryIndex so as not to terribly confuse things.
260 entryIndex = beast::zero;
261 }
262
263 auto const root = keylet::ownerDir(account);
264 auto found = false;
265
266 if (dirIndex.isZero())
267 {
268 dirIndex = root.key;
269 found = true;
270 }
271
272 auto dir = ledger.read({ltDIR_NODE, dirIndex});
273 if (!dir)
274 {
275 // it's possible the user had nftoken pages but no
276 // directory entries. If there's no nftoken page, we will
277 // give empty array for account_objects.
278 if (mlimit >= limit)
279 jvResult[jss::account_objects] = Json::arrayValue;
280
281 // non-zero dirIndex validity was checked in the beginning of this
282 // function; by this point, it should be zero. This function returns
283 // true regardless of nftoken page presence; if absent, account_objects
284 // is already set as an empty array. Notice we will only return false in
285 // this function when entryIndex can not be found, indicating an invalid
286 // marker error.
287 return true;
288 }
289
290 std::uint32_t i = 0;
291 for (;;)
292 {
293 auto const& entries = dir->getFieldV256(sfIndexes);
294 auto iter = entries.begin();
295
296 if (!found)
297 {
298 iter = std::find(iter, entries.end(), entryIndex);
299 if (iter == entries.end())
300 return false;
301
302 found = true;
303 }
304
305 // it's possible that the returned NFTPages exactly filled the
306 // response. Check for that condition.
307 if (i == mlimit && mlimit < limit)
308 {
309 jvResult[jss::limit] = limit;
310 jvResult[jss::marker] =
311 to_string(dirIndex) + ',' + to_string(*iter);
312 return true;
313 }
314
315 for (; iter != entries.end(); ++iter)
316 {
317 auto const sleNode = ledger.read(keylet::child(*iter));
318
319 if (!typeFilter.has_value() ||
320 typeMatchesFilter(typeFilter.value(), sleNode->getType()))
321 {
322 jvObjects.append(sleNode->getJson(JsonOptions::none));
323 }
324
325 if (++i == mlimit)
326 {
327 if (++iter != entries.end())
328 {
329 jvResult[jss::limit] = limit;
330 jvResult[jss::marker] =
331 to_string(dirIndex) + ',' + to_string(*iter);
332 return true;
333 }
334
335 break;
336 }
337 }
338
339 auto const nodeIndex = dir->getFieldU64(sfIndexNext);
340 if (nodeIndex == 0)
341 return true;
342
343 dirIndex = keylet::page(root, nodeIndex).key;
344 dir = ledger.read({ltDIR_NODE, dirIndex});
345 if (!dir)
346 return true;
347
348 if (i == mlimit)
349 {
350 auto const& e = dir->getFieldV256(sfIndexes);
351 if (!e.empty())
352 {
353 jvResult[jss::limit] = limit;
354 jvResult[jss::marker] =
355 to_string(dirIndex) + ',' + to_string(*e.begin());
356 }
357
358 return true;
359 }
360 }
361}
362
365{
366 auto const& params = context.params;
367 if (!params.isMember(jss::account))
368 return RPC::missing_field_error(jss::account);
369
370 if (!params[jss::account].isString())
371 return RPC::invalid_field_error(jss::account);
372
374 auto result = RPC::lookupLedger(ledger, context);
375 if (ledger == nullptr)
376 return result;
377
378 auto const id = parseBase58<AccountID>(params[jss::account].asString());
379 if (!id)
380 {
382 return result;
383 }
384 auto const accountID{id.value()};
385
386 if (!ledger->exists(keylet::account(accountID)))
388
390
391 if (params.isMember(jss::deletion_blockers_only) &&
392 params[jss::deletion_blockers_only].asBool())
393 {
394 struct
395 {
397 LedgerEntryType type;
398 } static constexpr deletionBlockers[] = {
399 {jss::check, ltCHECK},
400 {jss::escrow, ltESCROW},
401 {jss::nft_page, ltNFTOKEN_PAGE},
402 {jss::payment_channel, ltPAYCHAN},
403 {jss::state, ltRIPPLE_STATE},
404 {jss::xchain_owned_claim_id, ltXCHAIN_OWNED_CLAIM_ID},
405 {jss::xchain_owned_create_account_claim_id,
406 ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID},
407 {jss::bridge, ltBRIDGE},
408 {jss::mpt_issuance, ltMPTOKEN_ISSUANCE},
409 {jss::mptoken, ltMPTOKEN},
410 {jss::permissioned_domain, ltPERMISSIONED_DOMAIN},
411 {jss::vault, ltVAULT},
412 };
413
414 typeFilter.emplace();
415 typeFilter->reserve(std::size(deletionBlockers));
416
417 for (auto [name, type] : deletionBlockers)
418 {
419 if (params.isMember(jss::type) && name != params[jss::type])
420 {
421 continue;
422 }
423
424 typeFilter->push_back(type);
425 }
426 }
427 else
428 {
429 auto [rpcStatus, type] = RPC::chooseLedgerEntryType(params);
430
432 return RPC::invalid_field_error(jss::type);
433
434 if (rpcStatus)
435 {
436 result.clear();
437 rpcStatus.inject(result);
438 return result;
439 }
440 else if (type != ltANY)
441 {
442 typeFilter = std::vector<LedgerEntryType>({type});
443 }
444 }
445
446 unsigned int limit;
447 if (auto err = readLimitField(limit, RPC::Tuning::accountObjects, context))
448 return *err;
449
450 uint256 dirIndex;
451 uint256 entryIndex;
452 if (params.isMember(jss::marker))
453 {
454 auto const& marker = params[jss::marker];
455 if (!marker.isString())
456 return RPC::expected_field_error(jss::marker, "string");
457
458 auto const& markerStr = marker.asString();
459 auto const& idx = markerStr.find(',');
460 if (idx == std::string::npos)
461 return RPC::invalid_field_error(jss::marker);
462
463 if (!dirIndex.parseHex(markerStr.substr(0, idx)))
464 return RPC::invalid_field_error(jss::marker);
465
466 if (!entryIndex.parseHex(markerStr.substr(idx + 1)))
467 return RPC::invalid_field_error(jss::marker);
468 }
469
471 *ledger,
472 accountID,
473 typeFilter,
474 dirIndex,
475 entryIndex,
476 limit,
477 result))
478 return RPC::invalid_field_error(jss::marker);
479
480 result[jss::account] = toBase58(accountID);
482 return result;
483}
484
485} // namespace xrpl
T begin(T... args)
Lightweight wrapper to tag static string.
Definition json_value.h:45
Represents a JSON value.
Definition json_value.h:131
Value & append(Value const &value)
Append value to array at the end.
A view into a ledger.
Definition ReadView.h:32
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.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:484
base_uint next() const
Definition base_uint.h:436
bool isZero() const
Definition base_uint.h:521
T emplace(T... args)
T end(T... args)
T find(T... args)
T is_same_v
@ arrayValue
array value (ordered list)
Definition json_value.h:26
static LimitRange constexpr accountNFTokens
Limits for the account_nftokens command, in pages.
static LimitRange constexpr accountObjects
Limits for the account_objects command.
Json::Value invalid_field_error(std::string const &name)
Definition ErrorCodes.h:284
std::pair< RPC::Status, LedgerEntryType > chooseLedgerEntryType(Json::Value const &params)
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.
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition ErrorCodes.h:308
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:242
void inject_error(error_code_i code, Json::Value &json)
Add or update the json update to reflect 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.
Charge const feeMediumBurdenRPC
Keylet nftpage_max(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Definition Indexes.cpp:393
Keylet nftpage(Keylet const &k, uint256 const &token)
Definition Indexes.cpp:401
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:356
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition Indexes.cpp:172
Keylet nftpage_min(AccountID const &owner)
NFT page keylets.
Definition Indexes.cpp:385
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:166
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:362
std::uint32_t toUInt32(Taxon t)
Definition nft.h:29
Taxon getTaxon(uint256 const &id)
Definition nft.h:89
uint256 constexpr pageMask(std::string_view("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))
AccountID getIssuer(uint256 const &id)
Definition nft.h:101
std::uint16_t getTransferFee(uint256 const &id)
Definition nft.h:49
std::uint32_t getSerial(uint256 const &id)
Definition nft.h:57
std::uint16_t getFlags(uint256 const &id)
Definition nft.h:41
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
Json::Value doAccountNFTs(RPC::JsonContext &context)
General RPC command that can retrieve objects in the account root.
Number root(Number f, unsigned d)
Definition Number.cpp:644
bool getAccountObjects(ReadView const &ledger, AccountID const &account, std::optional< std::vector< LedgerEntryType > > const &typeFilter, uint256 dirIndex, uint256 entryIndex, std::uint32_t const limit, Json::Value &jvResult)
Gathers all objects for an account in a ledger.
Json::Value doAccountObjects(RPC::JsonContext &context)
Json::Value rpcError(error_code_i iError)
Definition RPCErr.cpp:12
LedgerEntryType
Identifiers for on-ledger objects.
@ ltANY
A special type, matching any ledger entry type.
@ rpcACT_NOT_FOUND
Definition ErrorCodes.h:51
@ rpcACT_MALFORMED
Definition ErrorCodes.h:71
T size(T... args)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
uint256 key
Definition Keylet.h:21
Resource::Charge & loadType
Definition Context.h:23
Json::Value params
Definition Context.h:44