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 ripple {
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(
101 isConsistent(book), "ripple::getBookBase : input is consistent");
102
103 auto const index = book.domain ? indexHash(
105 book.in.currency,
106 book.out.currency,
107 book.in.account,
108 book.out.account,
109 *(book.domain))
110 : indexHash(
112 book.in.currency,
113 book.out.currency,
114 book.in.account,
115 book.out.account);
116
117 // Return with quality 0.
118 auto k = keylet::quality({ltDIR_NODE, index}, 0);
119
120 return k.key;
121}
122
125{
126 static constexpr uint256 nextq(
127 "0000000000000000000000000000000000000000000000010000000000000000");
128 return uBase + nextq;
129}
130
132getQuality(uint256 const& uBase)
133{
134 // VFALCO [base_uint] This assumes a certain storage format
135 return boost::endian::big_to_native(((std::uint64_t*)uBase.end())[-1]);
136}
137
139getTicketIndex(AccountID const& account, std::uint32_t ticketSeq)
140{
141 return indexHash(
142 LedgerNameSpace::TICKET, account, std::uint32_t(ticketSeq));
143}
144
146getTicketIndex(AccountID const& account, SeqProxy ticketSeq)
147{
148 XRPL_ASSERT(ticketSeq.isTicket(), "ripple::getTicketIndex : valid input");
149 return getTicketIndex(account, ticketSeq.value());
150}
151
152MPTID
153makeMptID(std::uint32_t sequence, AccountID const& account)
154{
155 MPTID u;
156 sequence = boost::endian::native_to_big(sequence);
157 memcpy(u.data(), &sequence, sizeof(sequence));
158 memcpy(u.data() + sizeof(sequence), account.data(), sizeof(account));
159 return u;
160}
161
162//------------------------------------------------------------------------------
163
164namespace keylet {
165
166Keylet
167account(AccountID const& id) noexcept
168{
169 return Keylet{ltACCOUNT_ROOT, indexHash(LedgerNameSpace::ACCOUNT, id)};
170}
171
172Keylet
173child(uint256 const& key) noexcept
174{
175 return {ltCHILD, key};
176}
177
178Keylet const&
179skip() noexcept
180{
181 static Keylet const ret{
182 ltLEDGER_HASHES, indexHash(LedgerNameSpace::SKIP_LIST)};
183 return ret;
184}
185
186Keylet
187skip(LedgerIndex ledger) noexcept
188{
189 return {
190 ltLEDGER_HASHES,
191 indexHash(
193 std::uint32_t(static_cast<std::uint32_t>(ledger) >> 16))};
194}
195
196Keylet const&
197amendments() noexcept
198{
199 static Keylet const ret{
201 return ret;
202}
203
204Keylet const&
205fees() noexcept
206{
207 static Keylet const ret{
209 return ret;
210}
211
212Keylet const&
213negativeUNL() noexcept
214{
215 static Keylet const ret{
217 return ret;
218}
219
220Keylet
222{
223 return {ltDIR_NODE, getBookBase(b)};
224}
225
226Keylet
228 AccountID const& id0,
229 AccountID const& id1,
230 Currency const& currency) noexcept
231{
232 // There is code in SetTrust that calls us with id0 == id1, to allow users
233 // to locate and delete such "weird" trustlines. If we remove that code, we
234 // could enable this assert:
235 // XRPL_ASSERT(id0 != id1, "ripple::keylet::line : accounts must be
236 // different");
237
238 // A trust line is shared between two accounts; while we typically think
239 // of this as an "issuer" and a "holder" the relationship is actually fully
240 // bidirectional.
241 //
242 // So that we can generate a unique ID for a trust line, regardess of which
243 // side of the line we're looking at, we define a "canonical" order for the
244 // two accounts (smallest then largest) and hash them in that order:
245 auto const accounts = std::minmax(id0, id1);
246
247 return {
248 ltRIPPLE_STATE,
249 indexHash(
251 accounts.first,
252 accounts.second,
253 currency)};
254}
255
256Keylet
257offer(AccountID const& id, std::uint32_t seq) noexcept
258{
259 return {ltOFFER, indexHash(LedgerNameSpace::OFFER, id, seq)};
260}
261
262Keylet
263quality(Keylet const& k, std::uint64_t q) noexcept
264{
265 XRPL_ASSERT(
266 k.type == ltDIR_NODE, "ripple::keylet::quality : valid input type");
267
268 // Indexes are stored in big endian format: they print as hex as stored.
269 // Most significant bytes are first and the least significant bytes
270 // represent adjacent entries. We place the quality, in big endian format,
271 // in the 8 right most bytes; this way, incrementing goes to the next entry
272 // for indexes.
273 uint256 x = k.key;
274
275 // FIXME This is ugly and we can and should do better...
276 ((std::uint64_t*)x.end())[-1] = boost::endian::native_to_big(q);
277
278 return {ltDIR_NODE, x};
279}
280
281Keylet
283{
284 XRPL_ASSERT(
285 k.type == ltDIR_NODE,
286 "ripple::keylet::next_t::operator() : valid input type");
287 return {ltDIR_NODE, getQualityNext(k.key)};
288}
289
290Keylet
292{
293 return {ltTICKET, getTicketIndex(id, ticketSeq)};
294}
295
296Keylet
297ticket_t::operator()(AccountID const& id, SeqProxy ticketSeq) const
298{
299 return {ltTICKET, getTicketIndex(id, ticketSeq)};
300}
301
302// This function is presently static, since it's never accessed from anywhere
303// else. If we ever support multiple pages of signer lists, this would be the
304// keylet used to locate them.
305static Keylet
306signers(AccountID const& account, std::uint32_t page) noexcept
307{
308 return {
309 ltSIGNER_LIST, indexHash(LedgerNameSpace::SIGNER_LIST, account, page)};
310}
311
312Keylet
313signers(AccountID const& account) noexcept
314{
315 return signers(account, 0);
316}
317
318Keylet
319check(AccountID const& id, std::uint32_t seq) noexcept
320{
321 return {ltCHECK, indexHash(LedgerNameSpace::CHECK, id, seq)};
322}
323
324Keylet
325depositPreauth(AccountID const& owner, AccountID const& preauthorized) noexcept
326{
327 return {
328 ltDEPOSIT_PREAUTH,
329 indexHash(LedgerNameSpace::DEPOSIT_PREAUTH, owner, preauthorized)};
330}
331
332// Credentials should be sorted here, use credentials::makeSorted
333Keylet
335 AccountID const& owner,
336 std::set<std::pair<AccountID, Slice>> const& authCreds) noexcept
337{
339 hashes.reserve(authCreds.size());
340 for (auto const& o : authCreds)
341 hashes.emplace_back(sha512Half(o.first, o.second));
342
343 return {
344 ltDEPOSIT_PREAUTH,
346}
347
348//------------------------------------------------------------------------------
349
350Keylet
351unchecked(uint256 const& key) noexcept
352{
353 return {ltANY, key};
354}
355
356Keylet
357ownerDir(AccountID const& id) noexcept
358{
359 return {ltDIR_NODE, indexHash(LedgerNameSpace::OWNER_DIR, id)};
360}
361
362Keylet
363page(uint256 const& key, std::uint64_t index) noexcept
364{
365 if (index == 0)
366 return {ltDIR_NODE, key};
367
368 return {ltDIR_NODE, indexHash(LedgerNameSpace::DIR_NODE, key, index)};
369}
370
371Keylet
372escrow(AccountID const& src, std::uint32_t seq) noexcept
373{
374 return {ltESCROW, indexHash(LedgerNameSpace::ESCROW, src, seq)};
375}
376
377Keylet
378payChan(AccountID const& src, AccountID const& dst, std::uint32_t seq) noexcept
379{
380 return {
381 ltPAYCHAN,
383}
384
385Keylet
387{
389 std::memcpy(buf.data(), owner.data(), owner.size());
390 return {ltNFTOKEN_PAGE, uint256{buf}};
391}
392
393Keylet
395{
397 std::memcpy(id.data(), owner.data(), owner.size());
398 return {ltNFTOKEN_PAGE, id};
399}
400
401Keylet
402nftpage(Keylet const& k, uint256 const& token)
403{
404 XRPL_ASSERT(
405 k.type == ltNFTOKEN_PAGE, "ripple::keylet::nftpage : valid input type");
406 return {ltNFTOKEN_PAGE, (k.key & ~nft::pageMask) + (token & nft::pageMask)};
407}
408
409Keylet
411{
412 return {
413 ltNFTOKEN_OFFER, indexHash(LedgerNameSpace::NFTOKEN_OFFER, owner, seq)};
414}
415
416Keylet
417nft_buys(uint256 const& id) noexcept
418{
419 return {ltDIR_NODE, indexHash(LedgerNameSpace::NFTOKEN_BUY_OFFERS, id)};
420}
421
422Keylet
423nft_sells(uint256 const& id) noexcept
424{
425 return {ltDIR_NODE, indexHash(LedgerNameSpace::NFTOKEN_SELL_OFFERS, id)};
426}
427
428Keylet
429amm(Asset const& issue1, Asset const& issue2) noexcept
430{
431 auto const& [minI, maxI] =
432 std::minmax(issue1.get<Issue>(), issue2.get<Issue>());
433 return amm(indexHash(
435 minI.account,
436 minI.currency,
437 maxI.account,
438 maxI.currency));
439}
440
441Keylet
442amm(uint256 const& id) noexcept
443{
444 return {ltAMM, id};
445}
446
447Keylet
448delegate(AccountID const& account, AccountID const& authorizedAccount) noexcept
449{
450 return {
451 ltDELEGATE,
452 indexHash(LedgerNameSpace::DELEGATE, account, authorizedAccount)};
453}
454
455Keylet
457{
458 // A door account can support multiple bridges. On the locking chain
459 // there can only be one bridge per lockingChainCurrency. On the issuing
460 // chain there can only be one bridge per issuingChainCurrency.
461 auto const& issue = bridge.issue(chainType);
462 return {
463 ltBRIDGE,
464 indexHash(
465 LedgerNameSpace::BRIDGE, bridge.door(chainType), issue.currency)};
466}
467
468Keylet
470{
471 return {
472 ltXCHAIN_OWNED_CLAIM_ID,
473 indexHash(
475 bridge.lockingChainDoor(),
476 bridge.lockingChainIssue(),
477 bridge.issuingChainDoor(),
478 bridge.issuingChainIssue(),
479 seq)};
480}
481
482Keylet
484{
485 return {
486 ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID,
487 indexHash(
489 bridge.lockingChainDoor(),
490 bridge.lockingChainIssue(),
491 bridge.issuingChainDoor(),
492 bridge.issuingChainIssue(),
493 seq)};
494}
495
496Keylet
497did(AccountID const& account) noexcept
498{
499 return {ltDID, indexHash(LedgerNameSpace::DID, account)};
500}
501
502Keylet
503oracle(AccountID const& account, std::uint32_t const& documentID) noexcept
504{
505 return {ltORACLE, indexHash(LedgerNameSpace::ORACLE, account, documentID)};
506}
507
508Keylet
509mptIssuance(std::uint32_t seq, AccountID const& issuer) noexcept
510{
511 return mptIssuance(makeMptID(seq, issuer));
512}
513
514Keylet
515mptIssuance(MPTID const& issuanceID) noexcept
516{
517 return {
518 ltMPTOKEN_ISSUANCE,
520}
521
522Keylet
523mptoken(MPTID const& issuanceID, AccountID const& holder) noexcept
524{
525 return mptoken(mptIssuance(issuanceID).key, holder);
526}
527
528Keylet
529mptoken(uint256 const& issuanceKey, AccountID const& holder) noexcept
530{
531 return {
532 ltMPTOKEN, indexHash(LedgerNameSpace::MPTOKEN, issuanceKey, holder)};
533}
534
535Keylet
537 AccountID const& subject,
538 AccountID const& issuer,
539 Slice const& credType) noexcept
540{
541 return {
542 ltCREDENTIAL,
543 indexHash(LedgerNameSpace::CREDENTIAL, subject, issuer, credType)};
544}
545
546Keylet
547vault(AccountID const& owner, std::uint32_t seq) noexcept
548{
549 return vault(indexHash(LedgerNameSpace::VAULT, owner, seq));
550}
551
552Keylet
553loanbroker(AccountID const& owner, std::uint32_t seq) noexcept
554{
556}
557
558Keylet
559loan(uint256 const& loanBrokerID, std::uint32_t loanSeq) noexcept
560{
561 return loan(indexHash(LedgerNameSpace::LOAN, loanBrokerID, loanSeq));
562}
563
564Keylet
565permissionedDomain(AccountID const& account, std::uint32_t seq) noexcept
566{
567 return {
568 ltPERMISSIONED_DOMAIN,
570}
571
572Keylet
573permissionedDomain(uint256 const& domainID) noexcept
574{
575 return {ltPERMISSIONED_DOMAIN, domainID};
576}
577
578} // namespace keylet
579
580} // namespace ripple
Specifies an order book.
Definition Book.h:17
Issue in
Definition Book.h:19
Issue out
Definition Book.h:20
std::optional< uint256 > domain
Definition Book.h:21
A currency issued by an account.
Definition Issue.h:14
AccountID account
Definition Issue.h:17
Currency currency
Definition Issue.h:16
A type that represents either a sequence value or a ticket value.
Definition SeqProxy.h:37
constexpr std::uint32_t value() const
Definition SeqProxy.h:63
constexpr bool isTicket() const
Definition SeqProxy.h:75
An immutable linear range of bytes.
Definition Slice.h:27
static constexpr std::size_t size()
Definition base_uint.h:507
iterator end()
Definition base_uint.h:122
T emplace_back(T... args)
T memcpy(T... args)
T minmax(T... args)
Keylet quality(Keylet const &k, std::uint64_t q) noexcept
The initial directory page for a specific quality.
Definition Indexes.cpp:263
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:523
Keylet oracle(AccountID const &account, std::uint32_t const &documentID) noexcept
Definition Indexes.cpp:503
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
Definition Indexes.cpp:448
Keylet child(uint256 const &key) noexcept
Any item that can be in an owner dir.
Definition Indexes.cpp:173
Keylet permissionedDomain(AccountID const &account, std::uint32_t seq) noexcept
Definition Indexes.cpp:565
Keylet const & negativeUNL() noexcept
The (fixed) index of the object containing the ledger negativeUNL.
Definition Indexes.cpp:213
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Definition Indexes.cpp:429
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:227
Keylet const & amendments() noexcept
The index of the amendment table.
Definition Indexes.cpp:197
Keylet nftpage(Keylet const &k, uint256 const &token)
Definition Indexes.cpp:402
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:509
Keylet xChainClaimID(STXChainBridge const &bridge, std::uint64_t seq)
Definition Indexes.cpp:469
Keylet did(AccountID const &account) noexcept
Definition Indexes.cpp:497
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:547
Keylet loan(uint256 const &loanBrokerID, std::uint32_t loanSeq) noexcept
Definition Indexes.cpp:559
Keylet page(uint256 const &root, std::uint64_t index=0) noexcept
A page in a directory.
Definition Indexes.cpp:363
Keylet unchecked(uint256 const &key) noexcept
Any ledger entry.
Definition Indexes.cpp:351
Keylet escrow(AccountID const &src, std::uint32_t seq) noexcept
An escrow entry.
Definition Indexes.cpp:372
Keylet nftpage_min(AccountID const &owner)
NFT page keylets.
Definition Indexes.cpp:386
Keylet bridge(STXChainBridge const &bridge, STXChainBridge::ChainType chainType)
Definition Indexes.cpp:456
Keylet const & fees() noexcept
The (fixed) index of the object containing the ledger fees.
Definition Indexes.cpp:205
Keylet nftpage_max(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Definition Indexes.cpp:394
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:357
Keylet nft_buys(uint256 const &id) noexcept
The directory of buy offers for the specified NFT.
Definition Indexes.cpp:417
Keylet const & skip() noexcept
The index of the "short" skip list.
Definition Indexes.cpp:179
Keylet nft_sells(uint256 const &id) noexcept
The directory of sell offers for the specified NFT.
Definition Indexes.cpp:423
Keylet signers(AccountID const &account) noexcept
A SignerList.
Definition Indexes.cpp:313
Keylet xChainCreateAccountClaimID(STXChainBridge const &bridge, std::uint64_t seq)
Definition Indexes.cpp:483
Keylet nftoffer(AccountID const &owner, std::uint32_t seq)
An offer from an account to buy or sell an NFT.
Definition Indexes.cpp:410
Keylet check(AccountID const &id, std::uint32_t seq) noexcept
A Check.
Definition Indexes.cpp:319
Keylet offer(AccountID const &id, std::uint32_t seq) noexcept
An offer from an account.
Definition Indexes.cpp:257
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Definition Indexes.cpp:325
Keylet loanbroker(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:553
Keylet payChan(AccountID const &src, AccountID const &dst, std::uint32_t seq) noexcept
A PaymentChannel.
Definition Indexes.cpp:378
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
bool isConsistent(Book const &book)
Definition Book.cpp:10
LedgerNameSpace
Type-specific prefix for calculating ledger indices.
Definition Indexes.cpp:47
std::uint64_t getQuality(uint256 const &uBase)
Definition Indexes.cpp:132
base_uint< 256 > uint256
Definition base_uint.h:539
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:45
static uint256 indexHash(LedgerNameSpace space, Args const &... args)
Definition Indexes.cpp:92
uint256 getQualityNext(uint256 const &uBase)
Definition Indexes.cpp:124
uint256 getTicketIndex(AccountID const &account, std::uint32_t uSequence)
Definition Indexes.cpp:139
@ ltCHILD
A special type, matching any ledger type except directory nodes.
@ ltANY
A special type, matching any ledger entry type.
@ credential
Credentials signature.
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)
Definition Indexes.cpp:153
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition digest.h:205
uint256 getBookBase(Book const &book)
Definition Indexes.cpp:98
T reserve(T... args)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
LedgerEntryType type
Definition Keylet.h:22
uint256 key
Definition Keylet.h:21
Keylet operator()(Book const &b) const
Definition Indexes.cpp:221
Keylet operator()(Keylet const &k) const
Definition Indexes.cpp:282
Keylet operator()(AccountID const &id, std::uint32_t ticketSeq) const
Definition Indexes.cpp:291