rippled
NodeDirectory.h
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 #ifndef RIPPLE_APP_PATHS_NODEDIRECTORY_H_INCLUDED
21 #define RIPPLE_APP_PATHS_NODEDIRECTORY_H_INCLUDED
22 
23 #include <ripple/ledger/ApplyView.h>
24 #include <ripple/protocol/Indexes.h>
25 #include <ripple/protocol/STLedgerEntry.h>
26 
27 namespace ripple {
28 
29 // VFALCO TODO de-inline these function definitions
30 
32 {
33 public:
34  explicit NodeDirectory() = default;
35 
36  // Current directory - the last 64 bits of this are the quality.
38 
39  // Start of the next order book - one past the worst quality possible
40  // for the current order book.
42 
43  // TODO(tom): directory.current and directory.next should be of type
44  // Directory.
45 
46  bool advanceNeeded; // Need to advance directory.
47  bool restartNeeded; // Need to restart directory.
48 
50 
51  void
52  restart(bool multiQuality)
53  {
54  if (multiQuality)
55  current = beast::zero; // Restart book searching.
56  else
57  restartNeeded = true; // Restart at same quality.
58  }
59 
60  bool
61  initialize(Book const& book, ApplyView& view)
62  {
63  if (current != beast::zero)
64  return false;
65 
66  current = getBookBase(book);
68 
69  // TODO(tom): it seems impossible that any actual offers with
70  // quality == 0 could occur - we should disallow them, and clear
71  // directory.ledgerEntry without the database call in the next line.
73 
74  // Advance, if didn't find it. Normal not to be unable to lookup
75  // firstdirectory. Maybe even skip this lookup.
77  restartNeeded = false;
78 
79  // Associated vars are dirty, if found it.
80  return bool(ledgerEntry);
81  }
82 
84 
86  // VFALCO Consider renaming this to `nextQuality` or something
87  Advance
89  {
90  if (!(advanceNeeded || restartNeeded))
91  return NO_ADVANCE;
92 
93  // Get next quality.
94  // The Merkel radix tree is ordered by key so we can go to the next
95  // quality in O(1).
96  if (advanceNeeded)
97  {
98  auto const opt = view.succ(current, next);
99  current = opt ? *opt : uint256{};
100  }
101  advanceNeeded = false;
102  restartNeeded = false;
103 
104  if (current == beast::zero)
105  return END_ADVANCE;
106 
108  return NEW_QUALITY;
109  }
110 };
111 
112 } // namespace ripple
113 
114 #endif
ripple::NodeDirectory::Advance
Advance
Definition: NodeDirectory.h:83
std::shared_ptr< STLedgerEntry >
ripple::ApplyView::peek
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
ripple::NodeDirectory::current
uint256 current
Definition: NodeDirectory.h:37
ripple::getBookBase
uint256 getBookBase(Book const &book)
Definition: Indexes.cpp:79
ripple::NodeDirectory::restart
void restart(bool multiQuality)
Definition: NodeDirectory.h:52
ripple::NodeDirectory::advance
Advance advance(ApplyView &view)
Advance to the next quality directory in the order book.
Definition: NodeDirectory.h:88
ripple::getQualityNext
uint256 getQualityNext(uint256 const &uBase)
Definition: Indexes.cpp:97
ripple::NodeDirectory::NEW_QUALITY
@ NEW_QUALITY
Definition: NodeDirectory.h:83
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:140
ripple::base_uint< 256 >
ripple::keylet::page
Keylet page(uint256 const &key, std::uint64_t index) noexcept
A page in a directory.
Definition: Indexes.cpp:310
ripple::NodeDirectory::NodeDirectory
NodeDirectory()=default
ripple::NodeDirectory::NO_ADVANCE
@ NO_ADVANCE
Definition: NodeDirectory.h:83
ripple::NodeDirectory::advanceNeeded
bool advanceNeeded
Definition: NodeDirectory.h:46
ripple::NodeDirectory::next
uint256 next
Definition: NodeDirectory.h:41
ripple::NodeDirectory
Definition: NodeDirectory.h:31
ripple::NodeDirectory::restartNeeded
bool restartNeeded
Definition: NodeDirectory.h:47
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeDirectory::END_ADVANCE
@ END_ADVANCE
Definition: NodeDirectory.h:83
ripple::Book
Specifies an order book.
Definition: Book.h:32
ripple::NodeDirectory::ledgerEntry
SLE::pointer ledgerEntry
Definition: NodeDirectory.h:49
ripple::NodeDirectory::initialize
bool initialize(Book const &book, ApplyView &view)
Definition: NodeDirectory.h:61
ripple::ReadView::succ
virtual boost::optional< key_type > succ(key_type const &key, boost::optional< key_type > const &last=boost::none) const =0
Return the key of the next state item.