xrpld
Loading...
Searching...
No Matches
AccountObjects.cpp
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>
5
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/beast/utility/Zero.h>
8#include <xrpl/json/json_value.h>
9#include <xrpl/ledger/ReadView.h>
10#include <xrpl/protocol/AccountID.h>
11#include <xrpl/protocol/ErrorCodes.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/LedgerFormats.h>
14#include <xrpl/protocol/RPCErr.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/jss.h>
17#include <xrpl/protocol/nftPageMask.h>
18#include <xrpl/resource/Fees.h>
19
20#include <algorithm>
21#include <cstdint>
22#include <memory>
23#include <optional>
24#include <string>
25#include <vector>
26
27namespace xrpl {
28
38bool
40 ReadView const& ledger,
41 AccountID const& account,
43 uint256 dirIndex,
44 uint256 entryIndex,
45 std::uint32_t const limit,
46 json::Value& jvResult)
47{
48 // check if dirIndex is valid
49 if (!dirIndex.isZero() && !ledger.read({ltDIR_NODE, dirIndex}))
50 return false;
51
52 auto typeMatchesFilter = [](std::vector<LedgerEntryType> const& typeFilter,
53 LedgerEntryType ledgerType) {
54 auto it = std::ranges::find(typeFilter, ledgerType);
55 return it != typeFilter.end();
56 };
57
58 // if dirIndex != 0, then all NFTs have already been returned. only
59 // iterate NFT pages if the filter says so AND dirIndex == 0
60 bool iterateNFTPages =
61 (!typeFilter.has_value() || typeMatchesFilter(typeFilter.value(), ltNFTOKEN_PAGE)) &&
62 dirIndex.isZero();
63
64 Keylet const firstNFTPage = keylet::nftokenPageMin(account);
65
66 // we need to check the marker to see if it is an NFTTokenPage index.
67 if (iterateNFTPages && entryIndex.isNonZero())
68 {
69 // if it is we will try to iterate the pages up to the limit
70 // and then change over to the owner directory
71
72 if (firstNFTPage.key != (entryIndex & ~nft::kPageMask))
73 iterateNFTPages = false;
74 }
75
76 auto& jvObjects = (jvResult[jss::account_objects] = json::ValueType::Array);
77
78 // this is a mutable version of limit, used to seamlessly switch
79 // to iterating directory entries when nftokenpages are exhausted
80 uint32_t limitLeft = limit;
81
82 // iterate NFTokenPages preferentially
83 if (iterateNFTPages)
84 {
85 Keylet const first =
86 entryIndex.isZero() ? firstNFTPage : Keylet{ltNFTOKEN_PAGE, entryIndex};
87
88 Keylet const last = keylet::nftokenPageMax(account);
89
90 auto currentKey = ledger.succ(first.key, last.key.next()).value_or(last.key);
91
92 auto currentPage = ledger.read(Keylet{ltNFTOKEN_PAGE, currentKey});
93
94 while (currentPage)
95 {
96 jvObjects.append(currentPage->getJson(JsonOptions::Values::None));
97 auto const npm = (*currentPage)[~sfNextPageMin];
98 if (npm)
99 {
100 currentPage = ledger.read(Keylet(ltNFTOKEN_PAGE, *npm));
101 }
102 else
103 {
104 currentPage = nullptr;
105 }
106
107 if (--limitLeft == 0 && currentPage)
108 {
109 jvResult[jss::limit] = limit;
110 jvResult[jss::marker] = std::string("0,") + to_string(currentKey);
111 return true;
112 }
113
114 if (!npm)
115 break;
116
117 currentKey = *npm;
118 }
119
120 // if execution reaches here then we're about to transition
121 // to iterating the root directory (and the conventional
122 // behaviour of this RPC function.) Therefore we should
123 // zero entryIndex so as not to terribly confuse things.
124 entryIndex = beast::kZero;
125 }
126
127 auto const root = keylet::ownerDir(account);
128 auto startEntryFound = false;
129
130 if (dirIndex.isZero())
131 {
132 dirIndex = root.key;
133 startEntryFound = true;
134 }
135
136 auto dir = ledger.read({ltDIR_NODE, dirIndex});
137 if (!dir)
138 {
139 // it's possible the user had nftoken pages but no
140 // directory entries. If there's no nftoken page, we will
141 // give empty array for account_objects.
142 if (limitLeft >= limit)
143 jvResult[jss::account_objects] = json::ValueType::Array;
144
145 // non-zero dirIndex validity was checked in the beginning of this
146 // function; by this point, it should be zero. This function returns
147 // true regardless of nftoken page presence; if absent, account_objects
148 // is already set as an empty array. Notice we will only return false in
149 // this function when entryIndex can not be found, indicating an invalid
150 // marker error.
151 return true;
152 }
153
154 std::uint32_t itemsAdded = 0;
155 for (;;)
156 {
157 auto const& dirEntries = dir->getFieldV256(sfIndexes);
158 auto entryIter = dirEntries.begin();
159
160 if (!startEntryFound)
161 {
162 entryIter = std::find(entryIter, dirEntries.end(), entryIndex);
163 if (entryIter == dirEntries.end())
164 return false;
165
166 startEntryFound = true;
167 }
168
169 // it's possible that the returned NFTPages exactly filled the
170 // response. Check for that condition.
171 if (itemsAdded == limitLeft && limitLeft < limit && entryIter != dirEntries.end())
172 {
173 jvResult[jss::limit] = limit;
174 jvResult[jss::marker] = to_string(dirIndex) + ',' + to_string(*entryIter);
175 return true;
176 }
177
178 for (; entryIter != dirEntries.end(); ++entryIter)
179 {
180 auto const sleNode = ledger.read(keylet::child(*entryIter));
181
182 if (!typeFilter.has_value() ||
183 typeMatchesFilter(typeFilter.value(), sleNode->getType()))
184 {
185 jvObjects.append(sleNode->getJson(JsonOptions::Values::None));
186 }
187
188 if (++itemsAdded == limitLeft)
189 {
190 if (++entryIter != dirEntries.end())
191 {
192 jvResult[jss::limit] = limit;
193 jvResult[jss::marker] = to_string(dirIndex) + ',' + to_string(*entryIter);
194 return true;
195 }
196
197 break;
198 }
199 }
200
201 auto const nodeIndex = dir->getFieldU64(sfIndexNext);
202 if (nodeIndex == 0)
203 return true;
204
205 dirIndex = keylet::page(root, nodeIndex).key;
206 dir = ledger.read({ltDIR_NODE, dirIndex});
207 if (!dir)
208 return true;
209
210 if (itemsAdded == limitLeft)
211 {
212 auto const& currentDirEntries = dir->getFieldV256(sfIndexes);
213 if (!currentDirEntries.empty())
214 {
215 jvResult[jss::limit] = limit;
216 jvResult[jss::marker] =
217 to_string(dirIndex) + ',' + to_string(*currentDirEntries.begin());
218 }
219
220 return true;
221 }
222 }
223}
224
227{
228 auto const& params = context.params;
229 if (!params.isMember(jss::account))
230 return RPC::missingFieldError(jss::account);
231
232 if (!params[jss::account].isString())
233 return RPC::invalidFieldError(jss::account);
234
236 auto result = RPC::lookupLedger(ledger, context);
237 if (ledger == nullptr)
238 return result;
239
240 auto const id = parseBase58<AccountID>(params[jss::account].asString());
241 if (!id)
242 {
244 return result;
245 }
246 auto const accountID{id.value()};
247
248 if (!ledger->exists(keylet::account(accountID)))
249 return rpcError(RpcActNotFound);
250
252
253 if (params.isMember(jss::deletion_blockers_only) &&
254 params[jss::deletion_blockers_only].asBool())
255 {
256 struct
257 {
259 LedgerEntryType type;
260 } static constexpr kDeletionBlockers[] = {
261 {.name = jss::check, .type = ltCHECK},
262 {.name = jss::escrow, .type = ltESCROW},
263 {.name = jss::nft_page, .type = ltNFTOKEN_PAGE},
264 {.name = jss::payment_channel, .type = ltPAYCHAN},
265 {.name = jss::state, .type = ltRIPPLE_STATE},
266 {.name = jss::xchain_owned_claim_id, .type = ltXCHAIN_OWNED_CLAIM_ID},
267 {.name = jss::xchain_owned_create_account_claim_id,
268 .type = ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID},
269 {.name = jss::bridge, .type = ltBRIDGE},
270 {.name = jss::mpt_issuance, .type = ltMPTOKEN_ISSUANCE},
271 {.name = jss::mptoken, .type = ltMPTOKEN},
272 {.name = jss::permissioned_domain, .type = ltPERMISSIONED_DOMAIN},
273 {.name = jss::vault, .type = ltVAULT},
274 };
275
276 typeFilter.emplace();
277 typeFilter->reserve(std::size(kDeletionBlockers));
278
279 for (auto [name, type] : kDeletionBlockers)
280 {
281 if (params.isMember(jss::type) && name != params[jss::type])
282 {
283 continue;
284 }
285
286 typeFilter->push_back(type);
287 }
288 }
289 else
290 {
291 auto [rpcStatus, type] = RPC::chooseLedgerEntryType(params);
292
294 return RPC::invalidFieldError(jss::type);
295
296 if (rpcStatus)
297 {
298 result.clear();
299 rpcStatus.inject(result);
300 return result;
301 }
302 if (type != ltANY)
303 {
304 typeFilter = std::vector<LedgerEntryType>({type});
305 }
306 }
307
308 unsigned int limit = 0;
309 if (auto err = readLimitField(limit, RPC::Tuning::kAccountObjects, context))
310 return *err;
311
312 uint256 dirIndex;
313 uint256 entryIndex;
314 if (params.isMember(jss::marker))
315 {
316 auto const& marker = params[jss::marker];
317 if (!marker.isString())
318 return RPC::expectedFieldError(jss::marker, "string");
319
320 auto const& markerStr = marker.asString();
321 auto const& idx = markerStr.find(',');
322 if (idx == std::string::npos)
323 return RPC::invalidFieldError(jss::marker);
324
325 if (!dirIndex.parseHex(markerStr.substr(0, idx)))
326 return RPC::invalidFieldError(jss::marker);
327
328 if (!entryIndex.parseHex(markerStr.substr(idx + 1)))
329 return RPC::invalidFieldError(jss::marker);
330 }
331
332 if (!getAccountObjects(*ledger, accountID, typeFilter, dirIndex, entryIndex, limit, result))
333 return RPC::invalidFieldError(jss::marker);
334
335 result[jss::account] = toBase58(accountID);
337 return result;
338}
339
340} // namespace xrpl
Lightweight wrapper to tag static string.
Definition json_value.h:44
Represents a JSON value.
Definition json_value.h:130
bool isZero() const
Definition base_uint.h:544
bool isNonZero() const
Definition base_uint.h:549
BaseUInt next() const
Definition base_uint.h:460
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:507
A view into a ledger.
Definition ReadView.h:31
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.
T emplace(T... args)
T end(T... args)
T find(T... args)
@ Array
array value (ordered list)
Definition json_value.h:25
static constexpr LimitRange kAccountObjects
Limits for the account_objects command.
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.
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)
Definition ErrorCodes.h:273
json::Value missingFieldError(std::string const &name)
Definition ErrorCodes.h:231
std::pair< RPC::Status, LedgerEntryType > chooseLedgerEntryType(json::Value const &params)
Chooses the ledger entry type based on RPC parameters.
json::Value expectedFieldError(std::string const &name, std::string const &type)
Definition ErrorCodes.h:297
Charge const kFeeMediumBurdenRpc
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:357
Keylet nftokenPageMin(AccountID const &owner)
NFT page keylets.
Definition Indexes.cpp:384
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition Indexes.cpp:192
Keylet nftokenPageMax(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Definition Indexes.cpp:392
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:363
constexpr uint256 kPageMask(std::string_view("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcActNotFound
Definition ErrorCodes.h:52
@ RpcActMalformed
Definition ErrorCodes.h:72
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.
Definition AccountID.cpp:93
Number root(Number f, unsigned d)
Definition Number.cpp:1201
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
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.
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
json::Value doAccountObjects(RPC::JsonContext &context)
LedgerEntryType
Identifiers for on-ledger objects.
@ ltANY
A special type, matching any ledger entry type.
BaseUInt< 256 > uint256
Definition base_uint.h:562
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