rippled
BookTip.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2014 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/app/tx/impl/BookTip.h>
21 #include <ripple/basics/Log.h>
22 
23 namespace ripple {
24 
25 BookTip::BookTip(ApplyView& view, Book const& book)
26  : view_(view)
27  , m_valid(false)
28  , m_book(getBookBase(book))
29  , m_end(getQualityNext(m_book))
30 {
31 }
32 
33 bool
35 {
36  if (m_valid)
37  {
38  if (m_entry)
39  {
41  m_entry = nullptr;
42  }
43  }
44 
45  for (;;)
46  {
47  // See if there's an entry at or worse than current quality. Notice
48  // that the quality is encoded only in the index of the first page
49  // of a directory.
50  auto const first_page = view_.succ(m_book, m_end);
51 
52  if (!first_page)
53  return false;
54 
55  unsigned int di = 0;
57 
58  if (dirFirst(view_, *first_page, dir, di, m_index))
59  {
60  m_dir = dir->key();
62  m_quality = Quality(getQuality(*first_page));
63  m_valid = true;
64 
65  // Next query should start before this directory
66  m_book = *first_page;
67 
68  // The quality immediately before the next quality
69  --m_book;
70 
71  break;
72  }
73 
74  // There should never be an empty directory but just in case,
75  // we handle that case by advancing to the next directory.
76  m_book = *first_page;
77  }
78 
79  return true;
80 }
81 
82 } // namespace ripple
std::shared_ptr
STL class.
ripple::ApplyView::peek
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
ripple::getBookBase
uint256 getBookBase(Book const &book)
Definition: Indexes.cpp:82
ripple::keylet::offer
Keylet offer(AccountID const &id, std::uint32_t seq) noexcept
An offer from an account.
Definition: Indexes.cpp:222
ripple::getQualityNext
uint256 getQualityNext(uint256 const &uBase)
Definition: Indexes.cpp:100
ripple::BookTip::m_end
uint256 m_end
Definition: BookTip.h:43
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:134
ripple::BookTip::BookTip
BookTip(ApplyView &view, Book const &book)
Create the iterator.
Definition: BookTip.cpp:25
ripple::offerDelete
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition: View.cpp:893
ripple::BookTip::view_
ApplyView & view_
Definition: BookTip.h:40
ripple::BookTip::m_book
uint256 m_book
Definition: BookTip.h:42
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::ReadView::succ
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.
ripple::BookTip::m_quality
Quality m_quality
Definition: BookTip.h:47
ripple::BookTip::m_index
uint256 m_index
Definition: BookTip.h:45
ripple::BookTip::m_valid
bool m_valid
Definition: BookTip.h:41
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::dirFirst
bool dirFirst(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
Definition: View.cpp:112
ripple::BookTip::dir
uint256 const & dir() const noexcept
Definition: BookTip.h:54
ripple::BookTip::step
bool step(beast::Journal j)
Erases the current offer and advance to the next offer.
Definition: BookTip.cpp:34
ripple::BookTip::m_entry
std::shared_ptr< SLE > m_entry
Definition: BookTip.h:46
ripple::Book
Specifies an order book.
Definition: Book.h:33
ripple::getQuality
std::uint64_t getQuality(uint256 const &uBase)
Definition: Indexes.cpp:108
ripple::BookTip::m_dir
uint256 m_dir
Definition: BookTip.h:44