rippled
OfferStream.h
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 #ifndef RIPPLE_APP_BOOK_OFFERSTREAM_H_INCLUDED
21 #define RIPPLE_APP_BOOK_OFFERSTREAM_H_INCLUDED
22 
23 #include <ripple/app/tx/impl/BookTip.h>
24 #include <ripple/app/tx/impl/Offer.h>
25 #include <ripple/basics/Log.h>
26 #include <ripple/basics/chrono.h>
27 #include <ripple/beast/utility/Journal.h>
28 #include <ripple/ledger/View.h>
29 #include <ripple/protocol/Quality.h>
30 
31 #include <boost/container/flat_set.hpp>
32 
33 namespace ripple {
34 
35 template <class TIn, class TOut>
37 {
38 public:
40  {
41  private:
45 
46  public:
48  : limit_(limit), count_(0), j_(j)
49  {
50  }
51 
52  bool
53  step()
54  {
55  if (count_ >= limit_)
56  {
57  JLOG(j_.debug()) << "Exceeded " << limit_ << " step limit.";
58  return false;
59  }
60  count_++;
61  return true;
62  }
64  count() const
65  {
66  return count_;
67  }
68  };
69 
70 protected:
75  bool validBook_;
80  StepCounter& counter_;
81 
82  void
83  erase(ApplyView& view);
84 
85  virtual void
86  permRmOffer(uint256 const& offerIndex) = 0;
87 
88  template <class TTakerPays, class TTakerGets>
89  bool
91 
92 public:
94  ApplyView& view,
95  ApplyView& cancelView,
96  Book const& book,
98  StepCounter& counter,
99  beast::Journal journal);
100 
101  virtual ~TOfferStreamBase() = default;
102 
108  tip() const
109  {
110  return const_cast<TOfferStreamBase*>(this)->offer_;
111  }
112 
120  bool
121  step();
122 
123  TOut
124  ownerFunds() const
125  {
126  return *ownerFunds_;
127  }
128 };
129 
147 class OfferStream : public TOfferStreamBase<STAmount, STAmount>
148 {
149 protected:
150  void
151  permRmOffer(uint256 const& offerIndex) override;
152 
153 public:
155 };
156 
174 template <class TIn, class TOut>
175 class FlowOfferStream : public TOfferStreamBase<TIn, TOut>
176 {
177 private:
178  boost::container::flat_set<uint256> permToRemove_;
179 
180 public:
182 
183  // The following interface allows offer crossing to permanently
184  // remove self crossed offers. The motivation is somewhat
185  // unintuitive. See the discussion in the comments for
186  // BookOfferCrossingStep::limitSelfCrossQuality().
187  void
188  permRmOffer(uint256 const& offerIndex) override;
189 
190  boost::container::flat_set<uint256> const&
191  permToRemove() const
192  {
193  return permToRemove_;
194  }
195 };
196 } // namespace ripple
197 
198 #endif
ripple::TOfferStreamBase::counter_
StepCounter & counter_
Definition: OfferStream.h:80
ripple::TOfferStreamBase::StepCounter
Definition: OfferStream.h:39
ripple::TOfferStreamBase::shouldRmSmallIncreasedQOffer
bool shouldRmSmallIncreasedQOffer() const
Definition: OfferStream.cpp:139
ripple::TOfferStreamBase::offer_
TOffer< TIn, TOut > offer_
Definition: OfferStream.h:78
ripple::FlowOfferStream
Presents and consumes the offers in an order book.
Definition: OfferStream.h:175
ripple::TOfferStreamBase::book_
Book book_
Definition: OfferStream.h:74
ripple::TOfferStreamBase::ownerFunds_
std::optional< TOut > ownerFunds_
Definition: OfferStream.h:79
ripple::OfferStream
Presents and consumes the offers in an order book.
Definition: OfferStream.h:147
ripple::FlowOfferStream::permRmOffer
void permRmOffer(uint256 const &offerIndex) override
Definition: OfferStream.cpp:372
ripple::TOfferStreamBase::erase
void erase(ApplyView &view)
Definition: OfferStream.cpp:61
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:134
ripple::TOfferStreamBase::StepCounter::limit_
const std::uint32_t limit_
Definition: OfferStream.h:42
ripple::OfferStream::permRmOffer
void permRmOffer(uint256 const &offerIndex) override
Definition: OfferStream.cpp:365
ripple::base_uint< 256 >
ripple::TOfferStreamBase::StepCounter::step
bool step()
Definition: OfferStream.h:53
ripple::FlowOfferStream::permToRemove
boost::container::flat_set< uint256 > const & permToRemove() const
Definition: OfferStream.h:191
ripple::TOfferStreamBase::cancelView_
ApplyView & cancelView_
Definition: OfferStream.h:73
ripple::TOfferStreamBase::TOfferStreamBase
TOfferStreamBase(ApplyView &view, ApplyView &cancelView, Book const &book, NetClock::time_point when, StepCounter &counter, beast::Journal journal)
Definition: OfferStream.cpp:38
ripple::BookTip
Iterates and consumes raw offers in an order book.
Definition: BookTip.h:37
std::chrono::time_point
ripple::TOfferStreamBase::~TOfferStreamBase
virtual ~TOfferStreamBase()=default
ripple::TOfferStreamBase::tip_
BookTip tip_
Definition: OfferStream.h:77
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
ripple::TOfferStreamBase::expire_
const NetClock::time_point expire_
Definition: OfferStream.h:76
ripple::TOfferStreamBase::StepCounter::j_
beast::Journal j_
Definition: OfferStream.h:44
ripple::TOfferStreamBase::StepCounter::count_
std::uint32_t count_
Definition: OfferStream.h:43
ripple::TOfferStreamBase
Definition: OfferStream.h:36
ripple::TOfferStreamBase::ownerFunds
TOut ownerFunds() const
Definition: OfferStream.h:124
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TOfferStreamBase::j_
const beast::Journal j_
Definition: OfferStream.h:71
ripple::TOfferStreamBase::StepCounter::StepCounter
StepCounter(std::uint32_t limit, beast::Journal j)
Definition: OfferStream.h:47
ripple::TOfferStreamBase::tip
TOffer< TIn, TOut > & tip() const
Returns the offer at the tip of the order book.
Definition: OfferStream.h:108
ripple::TOfferStreamBase::validBook_
bool validBook_
Definition: OfferStream.h:75
ripple::TOfferStreamBase::view_
ApplyView & view_
Definition: OfferStream.h:72
std::optional< TOut >
beast::Journal::debug
Stream debug() const
Definition: Journal.h:315
ripple::TOfferStreamBase::StepCounter::count
std::uint32_t count() const
Definition: OfferStream.h:64
ripple::Book
Specifies an order book.
Definition: Book.h:33
ripple::TOfferStreamBase::step
bool step()
Advance to the next valid offer.
Definition: OfferStream.cpp:205
ripple::TOfferStreamBase::permRmOffer
virtual void permRmOffer(uint256 const &offerIndex)=0
ripple::FlowOfferStream::permToRemove_
boost::container::flat_set< uint256 > permToRemove_
Definition: OfferStream.h:178
ripple::TOffer
Definition: Offer.h:49