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 ripple {
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
162bool
164 ReadView const& ledger,
165 AccountID const& account,
167 uint256 dirIndex,
168 uint256 entryIndex,
169 std::uint32_t const limit,
170 Json::Value& jvResult)
171{
172 // check if dirIndex is valid
173 if (!dirIndex.isZero() && !ledger.read({ltDIR_NODE, dirIndex}))
174 return false;
175
176 auto typeMatchesFilter = [](std::vector<LedgerEntryType> const& typeFilter,
177 LedgerEntryType ledgerType) {
178 auto it = std::find(typeFilter.begin(), typeFilter.end(), ledgerType);
179 return it != typeFilter.end();
180 };
181
182 // if dirIndex != 0, then all NFTs have already been returned. only
183 // iterate NFT pages if the filter says so AND dirIndex == 0
184 bool iterateNFTPages =
185 (!typeFilter.has_value() ||
186 typeMatchesFilter(typeFilter.value(), ltNFTOKEN_PAGE)) &&
187 dirIndex == beast::zero;
188
189 Keylet const firstNFTPage = keylet::nftpage_min(account);
190
191 // we need to check the marker to see if it is an NFTTokenPage index.
192 if (iterateNFTPages && entryIndex != beast::zero)
193 {
194 // if it is we will try to iterate the pages up to the limit
195 // and then change over to the owner directory
196
197 if (firstNFTPage.key != (entryIndex & ~nft::pageMask))
198 iterateNFTPages = false;
199 }
200
201 auto& jvObjects = (jvResult[jss::account_objects] = Json::arrayValue);
202
203 // this is a mutable version of limit, used to seamlessly switch
204 // to iterating directory entries when nftokenpages are exhausted
205 uint32_t mlimit = limit;
206
207 // iterate NFTokenPages preferentially
208 if (iterateNFTPages)
209 {
210 Keylet const first = entryIndex == beast::zero
211 ? firstNFTPage
212 : Keylet{ltNFTOKEN_PAGE, entryIndex};
213
214 Keylet const last = keylet::nftpage_max(account);
215
216 // current key
217 uint256 ck = ledger.succ(first.key, last.key.next()).value_or(last.key);
218
219 // current page
220 auto cp = ledger.read(Keylet{ltNFTOKEN_PAGE, ck});
221
222 while (cp)
223 {
224 jvObjects.append(cp->getJson(JsonOptions::none));
225 auto const npm = (*cp)[~sfNextPageMin];
226 if (npm)
227 cp = ledger.read(Keylet(ltNFTOKEN_PAGE, *npm));
228 else
229 cp = nullptr;
230
231 if (--mlimit == 0)
232 {
233 if (cp)
234 {
235 jvResult[jss::limit] = limit;
236 jvResult[jss::marker] = std::string("0,") + to_string(ck);
237 return true;
238 }
239 }
240
241 if (!npm)
242 break;
243
244 ck = *npm;
245 }
246
247 // if execution reaches here then we're about to transition
248 // to iterating the root directory (and the conventional
249 // behaviour of this RPC function.) Therefore we should
250 // zero entryIndex so as not to terribly confuse things.
251 entryIndex = beast::zero;
252 }
253
254 auto const root = keylet::ownerDir(account);
255 auto found = false;
256
257 if (dirIndex.isZero())
258 {
259 dirIndex = root.key;
260 found = true;
261 }
262
263 auto dir = ledger.read({ltDIR_NODE, dirIndex});
264 if (!dir)
265 {
266 // it's possible the user had nftoken pages but no
267 // directory entries. If there's no nftoken page, we will
268 // give empty array for account_objects.
269 if (mlimit >= limit)
270 jvResult[jss::account_objects] = Json::arrayValue;
271
272 // non-zero dirIndex validity was checked in the beginning of this
273 // function; by this point, it should be zero. This function returns
274 // true regardless of nftoken page presence; if absent, account_objects
275 // is already set as an empty array. Notice we will only return false in
276 // this function when entryIndex can not be found, indicating an invalid
277 // marker error.
278 return true;
279 }
280
281 std::uint32_t i = 0;
282 for (;;)
283 {
284 auto const& entries = dir->getFieldV256(sfIndexes);
285 auto iter = entries.begin();
286
287 if (!found)
288 {
289 iter = std::find(iter, entries.end(), entryIndex);
290 if (iter == entries.end())
291 return false;
292
293 found = true;
294 }
295
296 // it's possible that the returned NFTPages exactly filled the
297 // response. Check for that condition.
298 if (i == mlimit && mlimit < limit)
299 {
300 jvResult[jss::limit] = limit;
301 jvResult[jss::marker] =
302 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() ||
311 typeMatchesFilter(typeFilter.value(), sleNode->getType()))
312 {
313 jvObjects.append(sleNode->getJson(JsonOptions::none));
314 }
315
316 if (++i == mlimit)
317 {
318 if (++iter != entries.end())
319 {
320 jvResult[jss::limit] = limit;
321 jvResult[jss::marker] =
322 to_string(dirIndex) + ',' + to_string(*iter);
323 return true;
324 }
325
326 break;
327 }
328 }
329
330 auto const nodeIndex = dir->getFieldU64(sfIndexNext);
331 if (nodeIndex == 0)
332 return true;
333
334 dirIndex = keylet::page(root, nodeIndex).key;
335 dir = ledger.read({ltDIR_NODE, dirIndex});
336 if (!dir)
337 return true;
338
339 if (i == mlimit)
340 {
341 auto const& e = dir->getFieldV256(sfIndexes);
342 if (!e.empty())
343 {
344 jvResult[jss::limit] = limit;
345 jvResult[jss::marker] =
346 to_string(dirIndex) + ',' + to_string(*e.begin());
347 }
348
349 return true;
350 }
351 }
352}
353
356{
357 auto const& params = context.params;
358 if (!params.isMember(jss::account))
359 return RPC::missing_field_error(jss::account);
360
361 if (!params[jss::account].isString())
362 return RPC::invalid_field_error(jss::account);
363
365 auto result = RPC::lookupLedger(ledger, context);
366 if (ledger == nullptr)
367 return result;
368
369 auto const id = parseBase58<AccountID>(params[jss::account].asString());
370 if (!id)
371 {
373 return result;
374 }
375 auto const accountID{id.value()};
376
377 if (!ledger->exists(keylet::account(accountID)))
379
381
382 if (params.isMember(jss::deletion_blockers_only) &&
383 params[jss::deletion_blockers_only].asBool())
384 {
385 struct
386 {
388 LedgerEntryType type;
389 } static constexpr deletionBlockers[] = {
390 {jss::check, ltCHECK},
391 {jss::escrow, ltESCROW},
392 {jss::nft_page, ltNFTOKEN_PAGE},
393 {jss::payment_channel, ltPAYCHAN},
394 {jss::state, ltRIPPLE_STATE},
395 {jss::xchain_owned_claim_id, ltXCHAIN_OWNED_CLAIM_ID},
396 {jss::xchain_owned_create_account_claim_id,
397 ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID},
398 {jss::bridge, ltBRIDGE},
399 {jss::mpt_issuance, ltMPTOKEN_ISSUANCE},
400 {jss::mptoken, ltMPTOKEN},
401 {jss::permissioned_domain, ltPERMISSIONED_DOMAIN},
402 {jss::vault, ltVAULT},
403 };
404
405 typeFilter.emplace();
406 typeFilter->reserve(std::size(deletionBlockers));
407
408 for (auto [name, type] : deletionBlockers)
409 {
410 if (params.isMember(jss::type) && name != params[jss::type])
411 {
412 continue;
413 }
414
415 typeFilter->push_back(type);
416 }
417 }
418 else
419 {
420 auto [rpcStatus, type] = RPC::chooseLedgerEntryType(params);
421
423 return RPC::invalid_field_error(jss::type);
424
425 if (rpcStatus)
426 {
427 result.clear();
428 rpcStatus.inject(result);
429 return result;
430 }
431 else if (type != ltANY)
432 {
433 typeFilter = std::vector<LedgerEntryType>({type});
434 }
435 }
436
437 unsigned int limit;
438 if (auto err = readLimitField(limit, RPC::Tuning::accountObjects, context))
439 return *err;
440
441 uint256 dirIndex;
442 uint256 entryIndex;
443 if (params.isMember(jss::marker))
444 {
445 auto const& marker = params[jss::marker];
446 if (!marker.isString())
447 return RPC::expected_field_error(jss::marker, "string");
448
449 auto const& markerStr = marker.asString();
450 auto const& idx = markerStr.find(',');
451 if (idx == std::string::npos)
452 return RPC::invalid_field_error(jss::marker);
453
454 if (!dirIndex.parseHex(markerStr.substr(0, idx)))
455 return RPC::invalid_field_error(jss::marker);
456
457 if (!entryIndex.parseHex(markerStr.substr(idx + 1)))
458 return RPC::invalid_field_error(jss::marker);
459 }
460
462 *ledger,
463 accountID,
464 typeFilter,
465 dirIndex,
466 entryIndex,
467 limit,
468 result))
469 return RPC::invalid_field_error(jss::marker);
470
471 result[jss::account] = toBase58(accountID);
473 return result;
474}
475
476} // namespace ripple
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::shared_ptr< SLE const > 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.
base_uint next() const
Definition base_uint.h:436
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:484
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:306
bool isAccountObjectsValidType(LedgerEntryType const &type)
Check if the type is a valid filtering type for account_objects method.
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition ErrorCodes.h:214
std::pair< RPC::Status, LedgerEntryType > chooseLedgerEntryType(Json::Value const &params)
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition ErrorCodes.h:330
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext &context, Json::Value &result)
Look up a ledger from a request and fill a Json::Result with the data representing a ledger.
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:264
Charge const feeMediumBurdenRPC
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition Indexes.cpp:171
Keylet nftpage(Keylet const &k, uint256 const &token)
Definition Indexes.cpp:400
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:361
Keylet nftpage_min(AccountID const &owner)
NFT page keylets.
Definition Indexes.cpp:384
Keylet nftpage_max(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Definition Indexes.cpp:392
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:355
std::uint16_t getTransferFee(uint256 const &id)
Definition nft.h:49
std::uint16_t getFlags(uint256 const &id)
Definition nft.h:41
std::uint32_t toUInt32(Taxon t)
Definition nft.h:29
Taxon getTaxon(uint256 const &id)
Definition nft.h:89
AccountID getIssuer(uint256 const &id)
Definition nft.h:101
std::uint32_t getSerial(uint256 const &id)
Definition nft.h:57
uint256 constexpr pageMask(std::string_view("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
@ rpcACT_NOT_FOUND
Definition ErrorCodes.h:51
@ rpcACT_MALFORMED
Definition ErrorCodes.h:71
Json::Value rpcError(int iError)
Definition RPCErr.cpp:12
Json::Value doAccountObjects(RPC::JsonContext &context)
Json::Value doAccountNFTs(RPC::JsonContext &context)
General RPC command that can retrieve objects in the account root.
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)
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
LedgerEntryType
Identifiers for on-ledger objects.
@ ltANY
A special type, matching any ledger entry type.
Number root(Number f, unsigned d)
Definition Number.cpp:617
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