rippled
Loading...
Searching...
No Matches
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 <xrpld/app/tx/detail/BookTip.h>
21#include <xrpl/basics/Log.h>
22
23namespace ripple {
24
25BookTip::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
33bool
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
A generic endpoint for log messages.
Definition: Journal.h:59
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:140
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
uint256 m_book
Definition: BookTip.h:42
ApplyView & view_
Definition: BookTip.h:40
BookTip(ApplyView &view, Book const &book)
Create the iterator.
Definition: BookTip.cpp:25
uint256 m_index
Definition: BookTip.h:45
bool step(beast::Journal j)
Erases the current offer and advance to the next offer.
Definition: BookTip.cpp:34
uint256 m_end
Definition: BookTip.h:43
uint256 const & dir() const noexcept
Definition: BookTip.h:54
std::shared_ptr< SLE > m_entry
Definition: BookTip.h:46
Quality m_quality
Definition: BookTip.h:47
uint256 m_dir
Definition: BookTip.h:44
bool m_valid
Definition: BookTip.h:41
Specifies an order book.
Definition: Book.h:34
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.
Keylet offer(AccountID const &id, std::uint32_t seq) noexcept
An offer from an account.
Definition: Indexes.cpp:250
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::uint64_t getQuality(uint256 const &uBase)
Definition: Indexes.cpp:125
bool dirFirst(ApplyView &view, uint256 const &root, std::shared_ptr< SLE > &page, unsigned int &index, uint256 &entry)
Definition: View.cpp:114
TER offerDelete(ApplyView &view, std::shared_ptr< SLE > const &sle, beast::Journal j)
Delete an offer.
Definition: View.cpp:1092
uint256 getQualityNext(uint256 const &uBase)
Definition: Indexes.cpp:117
uint256 getBookBase(Book const &book)
Definition: Indexes.cpp:98