xrpld
Loading...
Searching...
No Matches
OfferStream.h
1#pragma once
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/basics/chrono.h>
6#include <xrpl/beast/utility/Journal.h>
7#include <xrpl/ledger/ApplyView.h>
8#include <xrpl/protocol/Book.h>
9#include <xrpl/protocol/Concepts.h>
10#include <xrpl/tx/paths/BookTip.h>
11#include <xrpl/tx/paths/Offer.h>
12
13#include <boost/container/flat_set.hpp>
14
15#include <cstdint>
16#include <optional>
17
18namespace xrpl {
19
20template <StepAmount TIn, StepAmount TOut>
22{
23public:
25 {
26 private:
30
31 public:
33 {
34 }
35
36 bool
38 {
39 if (count_ >= limit_)
40 {
41 JLOG(j_.debug()) << "Exceeded " << limit_ << " step limit.";
42 return false;
43 }
44 count_++;
45 return true;
46 }
47 [[nodiscard]] std::uint32_t
48 count() const
49 {
50 return count_;
51 }
52 };
53
54protected:
65
66 void
67 erase(ApplyView& view);
68
69 virtual void
70 permRmOffer(uint256 const& offerIndex) = 0;
71
72 template <class TTakerPays, class TTakerGets>
74 [[nodiscard]] bool
76
77public:
79 ApplyView& view,
80 ApplyView& cancelView,
81 Book const& book,
83 StepCounter& counter,
84 beast::Journal journal);
85
86 virtual ~TOfferStreamBase() = default;
87
93 [[nodiscard]] TOffer<TIn, TOut>&
94 tip() const
95 {
96 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
97 return const_cast<TOfferStreamBase*>(this)->offer_;
98 }
99
108 bool
109 step();
110
111 [[nodiscard]] TOut
113 {
114 return *ownerFunds_; // NOLINT(bugprone-unchecked-optional-access) always set after step()
115 // is called
116 }
117};
118
137template <StepAmount TIn, StepAmount TOut>
138class FlowOfferStream : public TOfferStreamBase<TIn, TOut>
139{
140private:
141 boost::container::flat_set<uint256> permToRemove_;
142
143public:
144 using TOfferStreamBase<TIn, TOut>::TOfferStreamBase;
145
146 // The following interface allows offer crossing to permanently
147 // remove self crossed offers. The motivation is somewhat
148 // unintuitive. See the discussion in the comments for
149 // BookOfferCrossingStep::limitSelfCrossQuality().
150 void
151 permRmOffer(uint256 const& offerIndex) override;
152
153 [[nodiscard]] boost::container::flat_set<uint256> const&
155 {
156 return permToRemove_;
157 }
158};
159} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:134
Iterates and consumes raw offers in an order book.
Definition BookTip.h:20
Specifies an order book.
Definition Book.h:28
Presents and consumes the offers in an order book.
boost::container::flat_set< uint256 > permToRemove_
void permRmOffer(uint256 const &offerIndex) override
boost::container::flat_set< uint256 > const & permToRemove() const
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
StepCounter(std::uint32_t limit, beast::Journal j)
Definition OfferStream.h:32
StepCounter & counter_
Definition OfferStream.h:64
std::optional< TOut > ownerFunds_
Definition OfferStream.h:63
TOfferStreamBase(ApplyView &view, ApplyView &cancelView, Book const &book, NetClock::time_point when, StepCounter &counter, beast::Journal journal)
TOffer< TIn, TOut > & tip() const
Returns the offer at the tip of the order book.
Definition OfferStream.h:94
bool shouldRmSmallIncreasedQOffer() const
ApplyView & cancelView_
Definition OfferStream.h:57
TOut ownerFunds() const
NetClock::time_point const expire_
Definition OfferStream.h:60
TOffer< TIn, TOut > offer_
Definition OfferStream.h:62
virtual void permRmOffer(uint256 const &offerIndex)=0
virtual ~TOfferStreamBase()=default
beast::Journal const j_
Definition OfferStream.h:55
bool step()
Advance to the next valid offer.
void erase(ApplyView &view)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BaseUInt< 256 > uint256
Definition base_uint.h:580