rippled
Loading...
Searching...
No Matches
Indexes.cpp
1#include <xrpl/basics/Slice.h>
2#include <xrpl/basics/base_uint.h>
3#include <xrpl/basics/safe_cast.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/protocol/AccountID.h>
6#include <xrpl/protocol/Asset.h>
7#include <xrpl/protocol/Book.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/LedgerFormats.h>
10#include <xrpl/protocol/Protocol.h>
11#include <xrpl/protocol/SField.h>
12#include <xrpl/protocol/STXChainBridge.h>
13#include <xrpl/protocol/SeqProxy.h>
14#include <xrpl/protocol/UintTypes.h>
15#include <xrpl/protocol/digest.h>
16#include <xrpl/protocol/nftPageMask.h>
17
18#include <boost/endian/conversion.hpp>
19
20#include <algorithm>
21#include <array>
22#include <cstdint>
23#include <cstring>
24#include <set>
25#include <utility>
26#include <vector>
27
28namespace xrpl {
29
48 ACCOUNT = 'a',
49 DIR_NODE = 'd',
50 TRUST_LINE = 'r',
51 OFFER = 'o',
52 OWNER_DIR = 'O',
53 BOOK_DIR = 'B',
54 SKIP_LIST = 's',
55 ESCROW = 'u',
56 AMENDMENTS = 'f',
57 FEE_SETTINGS = 'e',
58 TICKET = 'T',
59 SIGNER_LIST = 'S',
61 CHECK = 'C',
62 DEPOSIT_PREAUTH = 'p',
64 NEGATIVE_UNL = 'N',
65 NFTOKEN_OFFER = 'q',
68 AMM = 'A',
69 BRIDGE = 'H',
70 XCHAIN_CLAIM_ID = 'Q',
72 DID = 'I',
73 ORACLE = 'R',
74 MPTOKEN_ISSUANCE = '~',
75 MPTOKEN = 't',
76 CREDENTIAL = 'D',
78 DELEGATE = 'E',
79 VAULT = 'V',
80 LOAN_BROKER = 'l', // lower-case L
81 LOAN = 'L',
82
83 // No longer used or supported. Left here to reserve the space
84 // to avoid accidental reuse.
85 CONTRACT [[deprecated]] = 'c',
86 GENERATOR [[deprecated]] = 'g',
87 NICKNAME [[deprecated]] = 'n',
88};
89
90template <class... Args>
91static uint256
92indexHash(LedgerNameSpace space, Args const&... args)
93{
94 return sha512Half(safe_cast<std::uint16_t>(space), args...);
95}
96
98getBookBase(Book const& book)
99{
100 XRPL_ASSERT(isConsistent(book), "xrpl::getBookBase : input is consistent");
101
102 auto const index = book.domain ? indexHash(
104 book.in.currency,
105 book.out.currency,
106 book.in.account,
107 book.out.account,
108 *(book.domain))
109 : indexHash(
111 book.in.currency,
112 book.out.currency,
113 book.in.account,
114 book.out.account);
115
116 // Return with quality 0.
117 auto k = keylet::quality({ltDIR_NODE, index}, 0);
118
119 return k.key;
120}
121
124{
125 static constexpr uint256 nextQuality(
126 "0000000000000000000000000000000000000000000000010000000000000000");
127 return uBase + nextQuality;
128}
129
131getQuality(uint256 const& uBase)
132{
133 // VFALCO [base_uint] This assumes a certain storage format
134 return boost::endian::big_to_native(((std::uint64_t*)uBase.end())[-1]);
135}
136
138getTicketIndex(AccountID const& account, std::uint32_t ticketSeq)
139{
140 return indexHash(LedgerNameSpace::TICKET, account, ticketSeq);
141}
142
144getTicketIndex(AccountID const& account, SeqProxy ticketSeq)
145{
146 XRPL_ASSERT(ticketSeq.isTicket(), "xrpl::getTicketIndex : valid input");
147 return getTicketIndex(account, ticketSeq.value());
148}
149
150MPTID
151makeMptID(std::uint32_t sequence, AccountID const& account)
152{
153 MPTID u;
154 sequence = boost::endian::native_to_big(sequence);
155 memcpy(u.data(), &sequence, sizeof(sequence));
156 memcpy(u.data() + sizeof(sequence), account.data(), sizeof(account));
157 return u;
158}
159
160//------------------------------------------------------------------------------
161
162namespace keylet {
163
164Keylet
165account(AccountID const& id) noexcept
166{
167 return Keylet{ltACCOUNT_ROOT, indexHash(LedgerNameSpace::ACCOUNT, id)};
168}
169
170Keylet
171child(uint256 const& key) noexcept
172{
173 return {ltCHILD, key};
174}
175
176Keylet const&
177skip() noexcept
178{
179 static Keylet const ret{ltLEDGER_HASHES, indexHash(LedgerNameSpace::SKIP_LIST)};
180 return ret;
181}
182
183Keylet
184skip(LedgerIndex ledger) noexcept
185{
186 return {
187 ltLEDGER_HASHES,
188 indexHash(
189 LedgerNameSpace::SKIP_LIST, std::uint32_t(static_cast<std::uint32_t>(ledger) >> 16))};
190}
191
192Keylet const&
193amendments() noexcept
194{
195 static Keylet const ret{ltAMENDMENTS, indexHash(LedgerNameSpace::AMENDMENTS)};
196 return ret;
197}
198
199Keylet const&
200fees() noexcept
201{
202 static Keylet const ret{ltFEE_SETTINGS, indexHash(LedgerNameSpace::FEE_SETTINGS)};
203 return ret;
204}
205
206Keylet const&
207negativeUNL() noexcept
208{
209 static Keylet const ret{ltNEGATIVE_UNL, indexHash(LedgerNameSpace::NEGATIVE_UNL)};
210 return ret;
211}
212
213Keylet
215{
216 return {ltDIR_NODE, getBookBase(b)};
217}
218
219Keylet
220line(AccountID const& id0, AccountID const& id1, Currency const& currency) noexcept
221{
222 // There is code in TrustSet that calls us with id0 == id1, to allow users
223 // to locate and delete such "weird" trustlines. If we remove that code, we
224 // could enable this assert:
225 // XRPL_ASSERT(id0 != id1, "xrpl::keylet::line : accounts must be
226 // different");
227
228 // A trust line is shared between two accounts; while we typically think
229 // of this as an "issuer" and a "holder" the relationship is actually fully
230 // bidirectional.
231 //
232 // So that we can generate a unique ID for a trust line, regardess of which
233 // side of the line we're looking at, we define a "canonical" order for the
234 // two accounts (smallest then largest) and hash them in that order:
235 auto const accounts = std::minmax(id0, id1);
236
237 return {
238 ltRIPPLE_STATE,
239 indexHash(LedgerNameSpace::TRUST_LINE, accounts.first, accounts.second, currency)};
240}
241
242Keylet
243offer(AccountID const& id, std::uint32_t seq) noexcept
244{
245 return {ltOFFER, indexHash(LedgerNameSpace::OFFER, id, seq)};
246}
247
248Keylet
249quality(Keylet const& k, std::uint64_t q) noexcept
250{
251 XRPL_ASSERT(k.type == ltDIR_NODE, "xrpl::keylet::quality : valid input type");
252
253 // Indexes are stored in big endian format: they print as hex as stored.
254 // Most significant bytes are first and the least significant bytes
255 // represent adjacent entries. We place the quality, in big endian format,
256 // in the 8 right most bytes; this way, incrementing goes to the next entry
257 // for indexes.
258 uint256 x = k.key;
259
260 // FIXME This is ugly and we can and should do better...
261 ((std::uint64_t*)x.end())[-1] = boost::endian::native_to_big(q);
262
263 return {ltDIR_NODE, x};
264}
265
266Keylet
268{
269 XRPL_ASSERT(k.type == ltDIR_NODE, "xrpl::keylet::next_t::operator() : valid input type");
270 return {ltDIR_NODE, getQualityNext(k.key)};
271}
272
273Keylet
275{
276 return {ltTICKET, getTicketIndex(id, ticketSeq)};
277}
278
279Keylet
280ticket_t::operator()(AccountID const& id, SeqProxy ticketSeq) const
281{
282 return {ltTICKET, getTicketIndex(id, ticketSeq)};
283}
284
285// This function is presently static, since it's never accessed from anywhere
286// else. If we ever support multiple pages of signer lists, this would be the
287// keylet used to locate them.
288static Keylet
289signers(AccountID const& account, std::uint32_t page) noexcept
290{
291 return {ltSIGNER_LIST, indexHash(LedgerNameSpace::SIGNER_LIST, account, page)};
292}
293
294Keylet
295signers(AccountID const& account) noexcept
296{
297 return signers(account, 0);
298}
299
300Keylet
301check(AccountID const& id, std::uint32_t seq) noexcept
302{
303 return {ltCHECK, indexHash(LedgerNameSpace::CHECK, id, seq)};
304}
305
306Keylet
307depositPreauth(AccountID const& owner, AccountID const& preauthorized) noexcept
308{
309 return {ltDEPOSIT_PREAUTH, indexHash(LedgerNameSpace::DEPOSIT_PREAUTH, owner, preauthorized)};
310}
311
312// Credentials should be sorted here, use credentials::makeSorted
313Keylet
315 AccountID const& owner,
316 std::set<std::pair<AccountID, Slice>> const& authCreds) noexcept
317{
319 hashes.reserve(authCreds.size());
320 for (auto const& o : authCreds)
321 hashes.emplace_back(sha512Half(o.first, o.second));
322
323 return {
324 ltDEPOSIT_PREAUTH, indexHash(LedgerNameSpace::DEPOSIT_PREAUTH_CREDENTIALS, owner, hashes)};
325}
326
327//------------------------------------------------------------------------------
328
329Keylet
330unchecked(uint256 const& key) noexcept
331{
332 return {ltANY, key};
333}
334
335Keylet
336ownerDir(AccountID const& id) noexcept
337{
338 return {ltDIR_NODE, indexHash(LedgerNameSpace::OWNER_DIR, id)};
339}
340
341Keylet
342page(uint256 const& key, std::uint64_t index) noexcept
343{
344 if (index == 0)
345 return {ltDIR_NODE, key};
346
347 return {ltDIR_NODE, indexHash(LedgerNameSpace::DIR_NODE, key, index)};
348}
349
350Keylet
351escrow(AccountID const& src, std::uint32_t seq) noexcept
352{
353 return {ltESCROW, indexHash(LedgerNameSpace::ESCROW, src, seq)};
354}
355
356Keylet
357payChan(AccountID const& src, AccountID const& dst, std::uint32_t seq) noexcept
358{
359 return {ltPAYCHAN, indexHash(LedgerNameSpace::XRP_PAYMENT_CHANNEL, src, dst, seq)};
360}
361
362Keylet
364{
366 std::memcpy(buf.data(), owner.data(), owner.size());
367 return {ltNFTOKEN_PAGE, uint256{buf}};
368}
369
370Keylet
372{
374 std::memcpy(id.data(), owner.data(), owner.size());
375 return {ltNFTOKEN_PAGE, id};
376}
377
378Keylet
379nftpage(Keylet const& k, uint256 const& token)
380{
381 XRPL_ASSERT(k.type == ltNFTOKEN_PAGE, "xrpl::keylet::nftpage : valid input type");
382 return {ltNFTOKEN_PAGE, (k.key & ~nft::pageMask) + (token & nft::pageMask)};
383}
384
385Keylet
387{
388 return {ltNFTOKEN_OFFER, indexHash(LedgerNameSpace::NFTOKEN_OFFER, owner, seq)};
389}
390
391Keylet
392nft_buys(uint256 const& id) noexcept
393{
394 return {ltDIR_NODE, indexHash(LedgerNameSpace::NFTOKEN_BUY_OFFERS, id)};
395}
396
397Keylet
398nft_sells(uint256 const& id) noexcept
399{
400 return {ltDIR_NODE, indexHash(LedgerNameSpace::NFTOKEN_SELL_OFFERS, id)};
401}
402
403Keylet
404amm(Asset const& issue1, Asset const& issue2) noexcept
405{
406 auto const& [minI, maxI] = std::minmax(issue1.get<Issue>(), issue2.get<Issue>());
407 return amm(
408 indexHash(LedgerNameSpace::AMM, minI.account, minI.currency, maxI.account, maxI.currency));
409}
410
411Keylet
412amm(uint256 const& id) noexcept
413{
414 return {ltAMM, id};
415}
416
417Keylet
418delegate(AccountID const& account, AccountID const& authorizedAccount) noexcept
419{
420 return {ltDELEGATE, indexHash(LedgerNameSpace::DELEGATE, account, authorizedAccount)};
421}
422
423Keylet
425{
426 // A door account can support multiple bridges. On the locking chain
427 // there can only be one bridge per lockingChainCurrency. On the issuing
428 // chain there can only be one bridge per issuingChainCurrency.
429 auto const& issue = bridge.issue(chainType);
430 return {ltBRIDGE, indexHash(LedgerNameSpace::BRIDGE, bridge.door(chainType), issue.currency)};
431}
432
433Keylet
435{
436 return {
437 ltXCHAIN_OWNED_CLAIM_ID,
438 indexHash(
440 bridge.lockingChainDoor(),
441 bridge.lockingChainIssue(),
442 bridge.issuingChainDoor(),
443 bridge.issuingChainIssue(),
444 seq)};
445}
446
447Keylet
449{
450 return {
451 ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID,
452 indexHash(
454 bridge.lockingChainDoor(),
455 bridge.lockingChainIssue(),
456 bridge.issuingChainDoor(),
457 bridge.issuingChainIssue(),
458 seq)};
459}
460
461Keylet
462did(AccountID const& account) noexcept
463{
464 return {ltDID, indexHash(LedgerNameSpace::DID, account)};
465}
466
467Keylet
468oracle(AccountID const& account, std::uint32_t const& documentID) noexcept
469{
470 return {ltORACLE, indexHash(LedgerNameSpace::ORACLE, account, documentID)};
471}
472
473Keylet
474mptIssuance(std::uint32_t seq, AccountID const& issuer) noexcept
475{
476 return mptIssuance(makeMptID(seq, issuer));
477}
478
479Keylet
480mptIssuance(MPTID const& issuanceID) noexcept
481{
482 return {ltMPTOKEN_ISSUANCE, indexHash(LedgerNameSpace::MPTOKEN_ISSUANCE, issuanceID)};
483}
484
485Keylet
486mptoken(MPTID const& issuanceID, AccountID const& holder) noexcept
487{
488 return mptoken(mptIssuance(issuanceID).key, holder);
489}
490
491Keylet
492mptoken(uint256 const& issuanceKey, AccountID const& holder) noexcept
493{
494 return {ltMPTOKEN, indexHash(LedgerNameSpace::MPTOKEN, issuanceKey, holder)};
495}
496
497Keylet
498credential(AccountID const& subject, AccountID const& issuer, Slice const& credType) noexcept
499{
500 return {ltCREDENTIAL, indexHash(LedgerNameSpace::CREDENTIAL, subject, issuer, credType)};
501}
502
503Keylet
504vault(AccountID const& owner, std::uint32_t seq) noexcept
505{
506 return vault(indexHash(LedgerNameSpace::VAULT, owner, seq));
507}
508
509Keylet
510loanbroker(AccountID const& owner, std::uint32_t seq) noexcept
511{
513}
514
515Keylet
516loan(uint256 const& loanBrokerID, std::uint32_t loanSeq) noexcept
517{
518 return loan(indexHash(LedgerNameSpace::LOAN, loanBrokerID, loanSeq));
519}
520
521Keylet
522permissionedDomain(AccountID const& account, std::uint32_t seq) noexcept
523{
524 return {ltPERMISSIONED_DOMAIN, indexHash(LedgerNameSpace::PERMISSIONED_DOMAIN, account, seq)};
525}
526
527Keylet
528permissionedDomain(uint256 const& domainID) noexcept
529{
530 return {ltPERMISSIONED_DOMAIN, domainID};
531}
532
533} // namespace keylet
534
535} // namespace xrpl
Specifies an order book.
Definition Book.h:16
Issue in
Definition Book.h:18
std::optional< uint256 > domain
Definition Book.h:20
Issue out
Definition Book.h:19
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
AccountID account
Definition Issue.h:16
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:36
constexpr bool isTicket() const
Definition SeqProxy.h:74
constexpr std::uint32_t value() const
Definition SeqProxy.h:62
An immutable linear range of bytes.
Definition Slice.h:26
iterator end()
Definition base_uint.h:117
pointer data()
Definition base_uint.h:101
static constexpr std::size_t size()
Definition base_uint.h:499
T emplace_back(T... args)
T memcpy(T... args)
T minmax(T... args)
Keylet const & skip() noexcept
The index of the "short" skip list.
Definition Indexes.cpp:177
Keylet quality(Keylet const &k, std::uint64_t q) noexcept
The initial directory page for a specific quality.
Definition Indexes.cpp:249
Keylet signers(AccountID const &account) noexcept
A SignerList.
Definition Indexes.cpp:295
Keylet oracle(AccountID const &account, std::uint32_t const &documentID) noexcept
Definition Indexes.cpp:468
Keylet const & negativeUNL() noexcept
The (fixed) index of the object containing the ledger negativeUNL.
Definition Indexes.cpp:207
Keylet did(AccountID const &account) noexcept
Definition Indexes.cpp:462
Keylet nftpage_max(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Definition Indexes.cpp:371
Keylet nftoffer(AccountID const &owner, std::uint32_t seq)
An offer from an account to buy or sell an NFT.
Definition Indexes.cpp:386
Keylet unchecked(uint256 const &key) noexcept
Any ledger entry.
Definition Indexes.cpp:330
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Definition Indexes.cpp:307
Keylet nftpage(Keylet const &k, uint256 const &token)
Definition Indexes.cpp:379
Keylet loanbroker(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:510
Keylet escrow(AccountID const &src, std::uint32_t seq) noexcept
An escrow entry.
Definition Indexes.cpp:351
Keylet bridge(STXChainBridge const &bridge, STXChainBridge::ChainType chainType)
Definition Indexes.cpp:424
Keylet const & amendments() noexcept
The index of the amendment table.
Definition Indexes.cpp:193
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:336
Keylet nft_buys(uint256 const &id) noexcept
The directory of buy offers for the specified NFT.
Definition Indexes.cpp:392
Keylet payChan(AccountID const &src, AccountID const &dst, std::uint32_t seq) noexcept
A PaymentChannel.
Definition Indexes.cpp:357
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:474
Keylet check(AccountID const &id, std::uint32_t seq) noexcept
A Check.
Definition Indexes.cpp:301
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Definition Indexes.cpp:404
Keylet loan(uint256 const &loanBrokerID, std::uint32_t loanSeq) noexcept
Definition Indexes.cpp:516
Keylet offer(AccountID const &id, std::uint32_t seq) noexcept
An offer from an account.
Definition Indexes.cpp:243
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition Indexes.cpp:171
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:504
Keylet line(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:220
Keylet nftpage_min(AccountID const &owner)
NFT page keylets.
Definition Indexes.cpp:363
Keylet xChainCreateAccountClaimID(STXChainBridge const &bridge, std::uint64_t seq)
Definition Indexes.cpp:448
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:486
Keylet nft_sells(uint256 const &id) noexcept
The directory of sell offers for the specified NFT.
Definition Indexes.cpp:398
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
Definition Indexes.cpp:418
Keylet const & fees() noexcept
The (fixed) index of the object containing the ledger fees.
Definition Indexes.cpp:200
Keylet permissionedDomain(AccountID const &account, std::uint32_t seq) noexcept
Definition Indexes.cpp:522
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:342
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Definition Indexes.cpp:498
Keylet xChainClaimID(STXChainBridge const &bridge, std::uint64_t seq)
Definition Indexes.cpp:434
uint256 constexpr pageMask(std::string_view("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isConsistent(Book const &book)
Definition Book.cpp:10
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:204
uint256 getTicketIndex(AccountID const &account, std::uint32_t uSequence)
Definition Indexes.cpp:138
std::uint64_t getQuality(uint256 const &uBase)
Definition Indexes.cpp:131
base_uint< 256 > uint256
Definition base_uint.h:531
static uint256 indexHash(LedgerNameSpace space, Args const &... args)
Definition Indexes.cpp:92
base_uint< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
Definition UintTypes.h:44
uint256 getQualityNext(uint256 const &uBase)
Definition Indexes.cpp:123
LedgerNameSpace
Type-specific prefix for calculating ledger indices.
Definition Indexes.cpp:47
uint256 getBookBase(Book const &book)
Definition Indexes.cpp:98
@ ltANY
A special type, matching any ledger entry type.
@ ltCHILD
A special type, matching any ledger type except directory nodes.
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)
Definition Indexes.cpp:151
T reserve(T... args)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
uint256 key
Definition Keylet.h:20
LedgerEntryType type
Definition Keylet.h:21
Keylet operator()(Book const &b) const
Definition Indexes.cpp:214
Keylet operator()(Keylet const &k) const
Definition Indexes.cpp:267
Keylet operator()(AccountID const &id, std::uint32_t ticketSeq) const
Definition Indexes.cpp:274