rippled
Indexes.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/protocol/digest.h>
21 #include <ripple/protocol/Indexes.h>
22 #include <boost/endian/conversion.hpp>
23 #include <cassert>
24 
25 namespace ripple {
26 
27 // get the index of the node that holds the last 256 ledgers
28 uint256
30 {
32 }
33 
34 // Get the index of the node that holds the set of 256 ledgers that includes
35 // this ledger's hash (or the first ledger after it if it's not a multiple
36 // of 256).
37 uint256
38 getLedgerHashIndex (std::uint32_t desiredLedgerIndex)
39 {
40  return sha512Half(
42  std::uint32_t(desiredLedgerIndex >> 16));
43 }
44 // get the index of the node that holds the enabled amendments
45 uint256
47 {
49 }
50 
51 // get the index of the node that holds the fee schedule
52 uint256
54 {
56 }
57 
58 uint256
59 getGeneratorIndex (AccountID const& uGeneratorID)
60 {
61  return sha512Half(
63  uGeneratorID);
64 }
65 
66 uint256
67 getBookBase (Book const& book)
68 {
69  assert (isConsistent (book));
70  // Return with quality 0.
73  book.in.currency,
74  book.out.currency,
75  book.in.account,
76  book.out.account));
77 }
78 
79 uint256
80 getOfferIndex (AccountID const& account, std::uint32_t uSequence)
81 {
82  return sha512Half(
84  account,
85  std::uint32_t(uSequence));
86 }
87 
88 uint256
89 getOwnerDirIndex (AccountID const& account)
90 {
91  return sha512Half(
93  account);
94 }
95 
96 
97 uint256
98 getDirNodeIndex (uint256 const& uDirRoot, const std::uint64_t uNodeIndex)
99 {
100  if (uNodeIndex == 0)
101  return uDirRoot;
102 
103  return sha512Half(
105  uDirRoot,
106  std::uint64_t(uNodeIndex));
107 }
108 
109 uint256
110 getQualityIndex (uint256 const& uBase, const std::uint64_t uNodeDir)
111 {
112  // Indexes are stored in big endian format: they print as hex as stored.
113  // Most significant bytes are first. Least significant bytes represent
114  // adjacent entries. We place uNodeDir in the 8 right most bytes to be
115  // adjacent. Want uNodeDir in big endian format so ++ goes to the next
116  // entry for indexes.
117  uint256 uNode (uBase);
118 
119  // TODO(tom): there must be a better way.
120  // VFALCO [base_uint] This assumes a certain storage format
121  ((std::uint64_t*) uNode.end ())[-1] = boost::endian::native_to_big (uNodeDir);
122 
123  return uNode;
124 }
125 
126 uint256
127 getQualityNext (uint256 const& uBase)
128 {
129  static uint256 const uNext (
130  from_hex_text<uint256>("10000000000000000"));
131  return uBase + uNext;
132 }
133 
135 getQuality (uint256 const& uBase)
136 {
137  // VFALCO [base_uint] This assumes a certain storage format
138  return boost::endian::big_to_native (((std::uint64_t*) uBase.end ())[-1]);
139 }
140 
141 uint256
142 getTicketIndex (AccountID const& account, std::uint32_t uSequence)
143 {
144  return sha512Half(
146  account,
147  std::uint32_t(uSequence));
148 }
149 
150 uint256
151 getRippleStateIndex (AccountID const& a, AccountID const& b, Currency const& currency)
152 {
153  if (a < b)
154  return sha512Half(
156  a,
157  b,
158  currency);
159  return sha512Half(
161  b,
162  a,
163  currency);
164 }
165 
166 uint256
167 getRippleStateIndex (AccountID const& a, Issue const& issue)
168 {
169  return getRippleStateIndex (a, issue.account, issue.currency);
170 }
171 
172 uint256
174 {
175  // We are prepared for there to be multiple SignerLists in the future,
176  // but we don't have them yet. In anticipation of multiple SignerLists
177  // We supply a 32-bit ID to locate the SignerList. Until we actually
178  // *have* multiple signer lists, we can default that ID to zero.
179  return sha512Half(
181  account,
182  std::uint32_t (0)); // 0 == default SignerList ID.
183 }
184 
185 uint256
186 getCheckIndex (AccountID const& account, std::uint32_t uSequence)
187 {
188  return sha512Half(
190  account,
191  std::uint32_t(uSequence));
192 }
193 
194 uint256
195 getDepositPreauthIndex (AccountID const& owner, AccountID const& preauthorized)
196 {
197  return sha512Half(
199  owner,
200  preauthorized);
201 }
202 
203 //------------------------------------------------------------------------------
204 
205 namespace keylet {
206 
208  AccountID const& id) const
209 {
211 }
212 
213 Keylet child (uint256 const& key)
214 {
215  return { ltCHILD, key };
216 }
217 
219 {
220  return { ltLEDGER_HASHES,
221  getLedgerHashIndex() };
222 }
223 
225 {
226  return { ltLEDGER_HASHES,
227  getLedgerHashIndex(ledger) };
228 }
229 
231 {
232  return { ltAMENDMENTS,
234 }
235 
237 {
238  return { ltFEE_SETTINGS,
239  getLedgerFeeIndex() };
240 }
241 
243 {
244  return { ltDIR_NODE,
245  getBookBase(b) };
246 }
247 
249  AccountID const& id1, Currency const& currency) const
250 {
251  return { ltRIPPLE_STATE,
252  getRippleStateIndex(id0, id1, currency) };
253 }
254 
256  Issue const& issue) const
257 {
258  return { ltRIPPLE_STATE,
259  getRippleStateIndex(id, issue) };
260 }
261 
263  std::uint32_t seq) const
264 {
265  return { ltOFFER,
266  getOfferIndex(id, seq) };
267 }
268 
270  std::uint64_t q) const
271 {
272  assert(k.type == ltDIR_NODE);
273  return { ltDIR_NODE,
274  getQualityIndex(k.key, q) };
275 }
276 
278 {
279  assert(k.type == ltDIR_NODE);
280  return { ltDIR_NODE,
281  getQualityNext(k.key) };
282 }
283 
285  std::uint32_t seq) const
286 {
287  return { ltTICKET,
288  getTicketIndex(id, seq) };
289 }
290 
292 {
293  return { ltSIGNER_LIST,
294  getSignerListIndex(id) };
295 }
296 
298  std::uint32_t seq) const
299 {
300  return { ltCHECK,
301  getCheckIndex(id, seq) };
302 }
303 
305  AccountID const& preauthorized) const
306 {
307  return { ltDEPOSIT_PREAUTH,
308  getDepositPreauthIndex(owner, preauthorized) };
309 }
310 
311 //------------------------------------------------------------------------------
312 
314 {
315  return { ltANY, key };
316 }
317 
319 {
320  return { ltDIR_NODE,
321  getOwnerDirIndex(id) };
322 }
323 
324 Keylet page(uint256 const& key,
325  std::uint64_t index)
326 {
327  return { ltDIR_NODE,
328  getDirNodeIndex(key, index) };
329 }
330 
331 Keylet page(Keylet const& root,
332  std::uint64_t index)
333 {
334  assert(root.type == ltDIR_NODE);
335  return page(root.key, index);
336 }
337 
338 Keylet
339 escrow (AccountID const& source, std::uint32_t seq)
340 {
342  using beast::hash_append;
344  hash_append(h, source);
345  hash_append(h, seq);
346  return { ltESCROW, static_cast<uint256>(h) };
347 }
348 
349 Keylet
350 payChan (AccountID const& source, AccountID const& dst, std::uint32_t seq)
351 {
353  using beast::hash_append;
355  hash_append(h, source);
356  hash_append(h, dst);
357  hash_append(h, seq);
358  return { ltPAYCHAN, static_cast<uint256>(h) };
359 }
360 
361 } // keylet
362 
363 } // ripple
ripple::keylet::amendments_t::operator()
Keylet operator()() const
Definition: Indexes.cpp:230
ripple::spaceSignerList
@ spaceSignerList
Definition: LedgerFormats.h:116
ripple::keylet::line_t::operator()
Keylet operator()(AccountID const &id0, AccountID const &id1, Currency const &currency) const
Definition: Indexes.cpp:248
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
beast::hash_append
std::enable_if_t< is_contiguously_hashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
Definition: hash_append.h:236
ripple::spaceRipple
@ spaceRipple
Definition: LedgerFormats.h:106
ripple::keylet::account_t::operator()
Keylet operator()(AccountID const &id) const
Definition: Indexes.cpp:207
ripple::isConsistent
bool isConsistent(Book const &book)
Definition: Book.cpp:25
ripple::detail::basic_sha512_half_hasher
Returns the SHA512-Half digest of a message.
Definition: digest.h:192
ripple::spaceDepositPreauth
@ spaceDepositPreauth
Definition: LedgerFormats.h:119
ripple::Book::out
Issue out
Definition: Book.h:36
ripple::getLedgerAmendmentIndex
uint256 getLedgerAmendmentIndex()
Definition: Indexes.cpp:46
ripple::getBookBase
uint256 getBookBase(Book const &book)
Definition: Indexes.cpp:67
ripple::ltESCROW
@ ltESCROW
Definition: LedgerFormats.h:81
ripple::getQualityIndex
uint256 getQualityIndex(uint256 const &uBase, const std::uint64_t uNodeDir)
Definition: Indexes.cpp:110
ripple::base_uint::end
iterator end()
Definition: base_uint.h:107
ripple::ltANY
@ ltANY
Special type, anything This is used when the type in the Keylet is unknown, such as when building met...
Definition: LedgerFormats.h:42
ripple::getOfferIndex
uint256 getOfferIndex(AccountID const &account, std::uint32_t uSequence)
Definition: Indexes.cpp:80
ripple::keylet::payChan
Keylet payChan(AccountID const &source, AccountID const &dst, std::uint32_t seq)
A PaymentChannel.
Definition: Indexes.cpp:350
ripple::spaceOffer
@ spaceOffer
Definition: LedgerFormats.h:107
ripple::Issue::currency
Currency currency
Definition: Issue.h:37
ripple::getSignerListIndex
uint256 getSignerListIndex(AccountID const &account)
Definition: Indexes.cpp:173
ripple::keylet::child
Keylet child(uint256 const &key)
Any item that can be in an owner dir.
Definition: Indexes.cpp:213
ripple::keylet::fees_t::operator()
Keylet operator()() const
Definition: Indexes.cpp:236
ripple::ltLEDGER_HASHES
@ ltLEDGER_HASHES
Definition: LedgerFormats.h:75
ripple::ltAMENDMENTS
@ ltAMENDMENTS
Definition: LedgerFormats.h:77
ripple::getQualityNext
uint256 getQualityNext(uint256 const &uBase)
Definition: Indexes.cpp:127
ripple::ltSIGNER_LIST
@ ltSIGNER_LIST
Definition: LedgerFormats.h:71
ripple::spaceCheck
@ spaceCheck
Definition: LedgerFormats.h:118
ripple::keylet::depositPreauth_t::operator()
Keylet operator()(AccountID const &owner, AccountID const &preauthorized) const
Definition: Indexes.cpp:304
ripple::spaceSkipList
@ spaceSkipList
Definition: LedgerFormats.h:111
ripple::getLedgerFeeIndex
uint256 getLedgerFeeIndex()
Definition: Indexes.cpp:53
ripple::keylet::skip_t::operator()
Keylet operator()() const
Definition: Indexes.cpp:218
ripple::ltTICKET
@ ltTICKET
Definition: LedgerFormats.h:69
ripple::keylet::next_t::operator()
Keylet operator()(Keylet const &k) const
Definition: Indexes.cpp:277
ripple::ltCHECK
@ ltCHECK
Definition: LedgerFormats.h:86
ripple::spaceEscrow
@ spaceEscrow
Definition: LedgerFormats.h:112
ripple::ltCHILD
@ ltCHILD
Special type, anything not a directory This is used when the type in the Keylet is unknown,...
Definition: LedgerFormats.h:48
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:436
ripple::Keylet::key
uint256 key
Definition: Keylet.h:41
ripple::getGeneratorIndex
uint256 getGeneratorIndex(AccountID const &uGeneratorID)
Definition: Indexes.cpp:59
ripple::base_uint
Definition: base_uint.h:65
ripple::getDirNodeIndex
uint256 getDirNodeIndex(uint256 const &uDirRoot, const std::uint64_t uNodeIndex)
Definition: Indexes.cpp:98
ripple::keylet::ticket_t::operator()
Keylet operator()(AccountID const &id, std::uint32_t seq) const
Definition: Indexes.cpp:284
ripple::getCheckIndex
uint256 getCheckIndex(AccountID const &account, std::uint32_t uSequence)
Definition: Indexes.cpp:186
ripple::ltFEE_SETTINGS
@ ltFEE_SETTINGS
Definition: LedgerFormats.h:79
ripple::keylet::signers_t::operator()
Keylet operator()(AccountID const &id) const
Definition: Indexes.cpp:291
ripple::spaceAccount
@ spaceAccount
Definition: LedgerFormats.h:103
ripple::spaceAmendment
@ spaceAmendment
Definition: LedgerFormats.h:113
ripple::getRippleStateIndex
uint256 getRippleStateIndex(AccountID const &a, AccountID const &b, Currency const &currency)
Definition: Indexes.cpp:151
ripple::keylet::ownerDir
Keylet ownerDir(AccountID const &id)
The root page of an account's directory.
Definition: Indexes.cpp:318
ripple::keylet::escrow
Keylet escrow(AccountID const &source, std::uint32_t seq)
An escrow entry.
Definition: Indexes.cpp:339
ripple::Keylet::type
LedgerEntryType type
Definition: Keylet.h:40
ripple::getOwnerDirIndex
uint256 getOwnerDirIndex(AccountID const &account)
Definition: Indexes.cpp:89
ripple::spaceDirNode
@ spaceDirNode
Definition: LedgerFormats.h:104
ripple::spaceTicket
@ spaceTicket
Definition: LedgerFormats.h:115
std::uint16_t
ripple::ltDEPOSIT_PREAUTH
@ ltDEPOSIT_PREAUTH
Definition: LedgerFormats.h:88
ripple::keylet::unchecked
Keylet unchecked(uint256 const &key)
Any ledger entry.
Definition: Indexes.cpp:313
ripple::spaceBookDir
@ spaceBookDir
Definition: LedgerFormats.h:109
ripple::spaceFee
@ spaceFee
Definition: LedgerFormats.h:114
ripple::spaceOwnerDir
@ spaceOwnerDir
Definition: LedgerFormats.h:108
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::getDepositPreauthIndex
uint256 getDepositPreauthIndex(AccountID const &owner, AccountID const &preauthorized)
Definition: Indexes.cpp:195
ripple::ltRIPPLE_STATE
@ ltRIPPLE_STATE
Definition: LedgerFormats.h:67
ripple::keylet::book_t::operator()
Keylet operator()(Book const &b) const
Definition: Indexes.cpp:242
ripple::getLedgerHashIndex
uint256 getLedgerHashIndex()
Definition: Indexes.cpp:29
ripple::getTicketIndex
uint256 getTicketIndex(AccountID const &account, std::uint32_t uSequence)
Definition: Indexes.cpp:142
ripple::sha512Half
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:268
cassert
ripple::keylet::quality_t::operator()
Keylet operator()(Keylet const &k, std::uint64_t q) const
Definition: Indexes.cpp:269
ripple::hash_append
void hash_append(Hasher &h, Slice const &v)
Definition: Slice.h:151
ripple::ltPAYCHAN
@ ltPAYCHAN
Definition: LedgerFormats.h:84
ripple::Book
Specifies an order book.
Definition: Book.h:32
ripple::ltDIR_NODE
@ ltDIR_NODE
Directory node.
Definition: LedgerFormats.h:65
ripple::ltOFFER
@ ltOFFER
Definition: LedgerFormats.h:73
ripple::ltACCOUNT_ROOT
@ ltACCOUNT_ROOT
Definition: LedgerFormats.h:54
ripple::keylet::offer_t::operator()
Keylet operator()(AccountID const &id, std::uint32_t seq) const
Definition: Indexes.cpp:262
ripple::spaceGenerator
@ spaceGenerator
Definition: LedgerFormats.h:105
ripple::getQuality
std::uint64_t getQuality(uint256 const &uBase)
Definition: Indexes.cpp:135
ripple::Book::in
Issue in
Definition: Book.h:35
ripple::Issue::account
AccountID account
Definition: Issue.h:38
ripple::keylet::page
Keylet page(uint256 const &key, std::uint64_t index)
A page in a directory.
Definition: Indexes.cpp:324
ripple::spaceXRPUChannel
@ spaceXRPUChannel
Definition: LedgerFormats.h:117
ripple::keylet::check_t::operator()
Keylet operator()(AccountID const &id, std::uint32_t seq) const
Definition: Indexes.cpp:297