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_;
79  boost::optional<TOut> ownerFunds_;
80  StepCounter& counter_;
81 
82  void
83  erase(ApplyView& view);
84 
85  virtual void
86  permRmOffer(uint256 const& offerIndex) = 0;
87 
88 public:
90  ApplyView& view,
91  ApplyView& cancelView,
92  Book const& book,
94  StepCounter& counter,
95  beast::Journal journal);
96 
97  virtual ~TOfferStreamBase() = default;
98 
104  tip() const
105  {
106  return const_cast<TOfferStreamBase*>(this)->offer_;
107  }
108 
116  bool
117  step();
118 
119  TOut
120  ownerFunds() const
121  {
122  return *ownerFunds_;
123  }
124 };
125 
143 class OfferStream : public TOfferStreamBase<STAmount, STAmount>
144 {
145 protected:
146  void
147  permRmOffer(uint256 const& offerIndex) override;
148 
149 public:
151 };
152 
170 template <class TIn, class TOut>
171 class FlowOfferStream : public TOfferStreamBase<TIn, TOut>
172 {
173 private:
174  boost::container::flat_set<uint256> permToRemove_;
175 
176 public:
178 
179  // The following interface allows offer crossing to permanently
180  // remove self crossed offers. The motivation is somewhat
181  // unintuitive. See the discussion in the comments for
182  // BookOfferCrossingStep::limitSelfCrossQuality().
183  void
184  permRmOffer(uint256 const& offerIndex) override;
185 
186  boost::container::flat_set<uint256> const&
187  permToRemove() const
188  {
189  return permToRemove_;
190  }
191 };
192 } // namespace ripple
193 
194 #endif
ripple::TOfferStreamBase::counter_
StepCounter & counter_
Definition: OfferStream.h:80
ripple::TOfferStreamBase::StepCounter
Definition: OfferStream.h:39
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:171
ripple::TOfferStreamBase::book_
Book book_
Definition: OfferStream.h:74
ripple::OfferStream
Presents and consumes the offers in an order book.
Definition: OfferStream.h:143
ripple::FlowOfferStream::permRmOffer
void permRmOffer(uint256 const &offerIndex) override
Definition: OfferStream.cpp:242
ripple::TOfferStreamBase::ownerFunds_
boost::optional< TOut > ownerFunds_
Definition: OfferStream.h:79
ripple::TOfferStreamBase::erase
void erase(ApplyView &view)
Definition: OfferStream.cpp:60
ripple::ApplyView
Writeable view to a ledger, for applying a transaction.
Definition: ApplyView.h:140
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:235
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:187
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:37
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:120
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:104
ripple::TOfferStreamBase::validBook_
bool validBook_
Definition: OfferStream.h:75
ripple::TOfferStreamBase::view_
ApplyView & view_
Definition: OfferStream.h:72
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:32
ripple::TOfferStreamBase::step
bool step()
Advance to the next valid offer.
Definition: OfferStream.cpp:137
ripple::TOfferStreamBase::permRmOffer
virtual void permRmOffer(uint256 const &offerIndex)=0
ripple::FlowOfferStream::permToRemove_
boost::container::flat_set< uint256 > permToRemove_
Definition: OfferStream.h:174
ripple::TOffer
Definition: Offer.h:49