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