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/chrono.h>
26 #include <ripple/basics/Log.h>
27 #include <ripple/ledger/View.h>
28 #include <ripple/protocol/Quality.h>
29 #include <ripple/beast/utility/Journal.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)
49  , count_ (0)
50  , j_ (j)
51  {
52  }
53 
54  bool
55  step ()
56  {
57  if (count_ >= limit_)
58  {
59  JLOG(j_.debug()) << "Exceeded " << limit_ << " step limit.";
60  return false;
61  }
62  count_++;
63  return true;
64  }
66  {
67  return count_;
68  }
69  };
70 
71 protected:
76  bool validBook_;
80  boost::optional<TOut> ownerFunds_;
81  StepCounter& counter_;
82 
83  void
84  erase (ApplyView& view);
85 
86  virtual
87  void
88  permRmOffer (uint256 const& offerIndex) = 0;
89 
90 public:
91  TOfferStreamBase (ApplyView& view, ApplyView& cancelView,
92  Book const& book, NetClock::time_point when,
93  StepCounter& counter, beast::Journal journal);
94 
95  virtual ~TOfferStreamBase() = default;
96 
102  tip () const
103  {
104  return const_cast<TOfferStreamBase*>(this)->offer_;
105  }
106 
114  bool
115  step ();
116 
117  TOut ownerFunds () const
118  {
119  return *ownerFunds_;
120  }
121 };
122 
140 class OfferStream : public TOfferStreamBase<STAmount, STAmount>
141 {
142 protected:
143  void
144  permRmOffer (uint256 const& offerIndex) override;
145 public:
147 };
148 
166 template <class TIn, class TOut>
167 class FlowOfferStream : public TOfferStreamBase<TIn, TOut>
168 {
169 private:
170  boost::container::flat_set<uint256> permToRemove_;
171 
172 public:
174 
175  // The following interface allows offer crossing to permanently
176  // remove self crossed offers. The motivation is somewhat
177  // unintuitive. See the discussion in the comments for
178  // BookOfferCrossingStep::limitSelfCrossQuality().
179  void
180  permRmOffer (uint256 const& offerIndex) override;
181 
182  boost::container::flat_set<uint256> const& permToRemove () const
183  {
184  return permToRemove_;
185  }
186 };
187 }
188 
189 #endif
190 
ripple::TOfferStreamBase::counter_
StepCounter & counter_
Definition: OfferStream.h:81
ripple::TOfferStreamBase::StepCounter
Definition: OfferStream.h:39
ripple::TOfferStreamBase::offer_
TOffer< TIn, TOut > offer_
Definition: OfferStream.h:79
ripple::FlowOfferStream
Presents and consumes the offers in an order book.
Definition: OfferStream.h:167
ripple::TOfferStreamBase::book_
Book book_
Definition: OfferStream.h:75
ripple::OfferStream
Presents and consumes the offers in an order book.
Definition: OfferStream.h:140
ripple::FlowOfferStream::permRmOffer
void permRmOffer(uint256 const &offerIndex) override
Definition: OfferStream.cpp:236
ripple::TOfferStreamBase::ownerFunds_
boost::optional< TOut > ownerFunds_
Definition: OfferStream.h:80
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:150
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:229
ripple::base_uint< 256 >
ripple::TOfferStreamBase::StepCounter::step
bool step()
Definition: OfferStream.h:55
ripple::FlowOfferStream::permToRemove
boost::container::flat_set< uint256 > const & permToRemove() const
Definition: OfferStream.h:182
ripple::TOfferStreamBase::cancelView_
ApplyView & cancelView_
Definition: OfferStream.h:74
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:78
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
std::uint32_t
ripple::TOfferStreamBase::expire_
const NetClock::time_point expire_
Definition: OfferStream.h:77
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:117
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:72
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:102
ripple::TOfferStreamBase::validBook_
bool validBook_
Definition: OfferStream.h:76
ripple::TOfferStreamBase::view_
ApplyView & view_
Definition: OfferStream.h:73
beast::Journal::debug
Stream debug() const
Definition: Journal.h:292
ripple::TOfferStreamBase::StepCounter::count
std::uint32_t count() const
Definition: OfferStream.h:65
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:170
ripple::TOffer
Definition: Offer.h:50