rippled
STPathSet.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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_PROTOCOL_STPATHSET_H_INCLUDED
21 #define RIPPLE_PROTOCOL_STPATHSET_H_INCLUDED
22 
23 #include <ripple/json/json_value.h>
24 #include <ripple/protocol/SField.h>
25 #include <ripple/protocol/STBase.h>
26 #include <ripple/protocol/UintTypes.h>
27 #include <boost/optional.hpp>
28 #include <cassert>
29 #include <cstddef>
30 
31 namespace ripple {
32 
34 {
35 public:
36  enum Type
37  {
38  typeNone = 0x00,
39  typeAccount = 0x01, // Rippling through an account (vs taking an offer).
40  typeCurrency = 0x10, // Currency follows.
41  typeIssuer = 0x20, // Issuer follows.
42  typeBoundary = 0xFF, // Boundary between alternate paths.
44  // Combination of all types.
45  };
46 
47 private:
48  static
50  get_hash (STPathElement const& element);
51 
52 public:
54  boost::optional<AccountID> const& account,
55  boost::optional<Currency> const& currency,
56  boost::optional<AccountID> const& issuer)
57  : mType (typeNone)
58  {
59  if (! account)
60  {
61  is_offer_ = true;
62  }
63  else
64  {
65  is_offer_ = false;
66  mAccountID = *account;
67  mType |= typeAccount;
68  assert(mAccountID != noAccount());
69  }
70 
71  if (currency)
72  {
73  mCurrencyID = *currency;
75  }
76 
77  if (issuer)
78  {
79  mIssuerID = *issuer;
80  mType |= typeIssuer;
81  assert(mIssuerID != noAccount());
82  }
83 
84  hash_value_ = get_hash (*this);
85  }
86 
88  AccountID const& account, Currency const& currency,
89  AccountID const& issuer, bool forceCurrency = false)
90  : mType (typeNone), mAccountID (account), mCurrencyID (currency)
91  , mIssuerID (issuer), is_offer_ (isXRP(mAccountID))
92  {
93  if (!is_offer_)
94  mType |= typeAccount;
95 
96  if (forceCurrency || !isXRP(currency))
98 
99  if (!isXRP(issuer))
100  mType |= typeIssuer;
101 
102  hash_value_ = get_hash (*this);
103  }
104 
106  unsigned int uType, AccountID const& account, Currency const& currency,
107  AccountID const& issuer)
108  : mType (uType), mAccountID (account), mCurrencyID (currency)
109  , mIssuerID (issuer), is_offer_ (isXRP(mAccountID))
110  {
111  hash_value_ = get_hash (*this);
112  }
113 
115  : mType (typeNone), is_offer_ (true)
116  {
117  hash_value_ = get_hash (*this);
118  }
119 
120  STPathElement(STPathElement const&) = default;
121  STPathElement& operator=(STPathElement const&) = default;
122 
123  auto
124  getNodeType () const
125  {
126  return mType;
127  }
128 
129  bool
130  isOffer () const
131  {
132  return is_offer_;
133  }
134 
135  bool
136  isAccount () const
137  {
138  return !isOffer ();
139  }
140 
141  bool
142  hasIssuer () const
143  {
145  }
146 
147  bool
148  hasCurrency () const
149  {
151  }
152 
153  bool
154  isNone () const
155  {
157  }
158 
159  // Nodes are either an account ID or a offer prefix. Offer prefixs denote a
160  // class of offers.
161  AccountID const&
162  getAccountID () const
163  {
164  return mAccountID;
165  }
166 
167  Currency const&
168  getCurrency () const
169  {
170  return mCurrencyID;
171  }
172 
173  AccountID const&
174  getIssuerID () const
175  {
176  return mIssuerID;
177  }
178 
179  bool
180  operator== (const STPathElement& t) const
181  {
182  return (mType & typeAccount) == (t.mType & typeAccount) &&
183  hash_value_ == t.hash_value_ &&
184  mAccountID == t.mAccountID &&
185  mCurrencyID == t.mCurrencyID &&
186  mIssuerID == t.mIssuerID;
187  }
188 
189  bool
190  operator!= (const STPathElement& t) const
191  {
192  return !operator==(t);
193  }
194 
195 private:
196  unsigned int mType;
200 
201  bool is_offer_;
203 };
204 
205 class STPath
206 {
207 public:
208  STPath () = default;
209 
211  : mPath (std::move(p))
212  { }
213 
215  size () const
216  {
217  return mPath.size ();
218  }
219 
220  bool empty() const
221  {
222  return mPath.empty ();
223  }
224 
225  void
227  {
228  mPath.push_back (e);
229  }
230 
231  template <typename ...Args>
232  void
233  emplace_back (Args&&... args)
234  {
235  mPath.emplace_back (std::forward<Args> (args)...);
236  }
237 
238  bool
239  hasSeen (
240  AccountID const& account, Currency const& currency,
241  AccountID const& issuer) const;
242 
244  getJson (JsonOptions) const;
245 
247  begin () const
248  {
249  return mPath.begin ();
250  }
251 
253  end () const
254  {
255  return mPath.end ();
256  }
257 
258  bool
259  operator== (STPath const& t) const
260  {
261  return mPath == t.mPath;
262  }
263 
265  back () const
266  {
267  return mPath.back ();
268  }
269 
271  front () const
272  {
273  return mPath.front ();
274  }
275 
277  {
278  return mPath[i];
279  }
280 
281  const STPathElement& operator[](int i) const
282  {
283  return mPath[i];
284  }
285 
286  void reserve(size_t s)
287  {
288  mPath.reserve(s);
289  }
290 private:
292 };
293 
294 //------------------------------------------------------------------------------
295 
296 // A set of zero or more payment paths
297 class STPathSet final
298  : public STBase
299 {
300 public:
301  STPathSet () = default;
302 
303  STPathSet (SField const& n)
304  : STBase (n)
305  { }
306 
307  STPathSet (SerialIter& sit, SField const& name);
308 
309  STBase*
310  copy (std::size_t n, void* buf) const override
311  {
312  return emplace(n, buf, *this);
313  }
314 
315  STBase*
316  move (std::size_t n, void* buf) override
317  {
318  return emplace(n, buf, std::move(*this));
319  }
320 
321  void
322  add (Serializer& s) const override;
323 
325  getJson (JsonOptions) const override;
326 
328  getSType () const override
329  {
330  return STI_PATHSET;
331  }
332 
333  bool
334  assembleAdd(STPath const& base, STPathElement const& tail);
335 
336  bool
337  isEquivalent (const STBase& t) const override;
338 
339  bool
340  isDefault () const override
341  {
342  return value.empty ();
343  }
344 
345  // std::vector like interface:
348  {
349  return value[n];
350  }
351 
354  {
355  return value[n];
356  }
357 
359  begin () const
360  {
361  return value.begin ();
362  }
363 
365  end () const
366  {
367  return value.end ();
368  }
369 
371  size () const
372  {
373  return value.size ();
374  }
375 
376  bool
377  empty () const
378  {
379  return value.empty ();
380  }
381 
382  void
383  push_back (STPath const& e)
384  {
385  value.push_back (e);
386  }
387 
388  template <typename... Args>
389  void emplace_back (Args&&... args)
390  {
391  value.emplace_back (std::forward<Args> (args)...);
392  }
393 
394 private:
396 };
397 
398 } // ripple
399 
400 #endif
ripple::STPath::push_back
void push_back(STPathElement const &e)
Definition: STPathSet.h:226
ripple::STPathElement::operator=
STPathElement & operator=(STPathElement const &)=default
ripple::STPathElement::get_hash
static std::size_t get_hash(STPathElement const &element)
Definition: STPathSet.cpp:31
ripple::STPathElement::isOffer
bool isOffer() const
Definition: STPathSet.h:130
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::STPathSet::end
std::vector< STPath >::const_iterator end() const
Definition: STPathSet.h:365
ripple::STPathElement::typeAll
@ typeAll
Definition: STPathSet.h:43
ripple::STPathElement::operator==
bool operator==(const STPathElement &t) const
Definition: STPathSet.h:180
ripple::STPath::mPath
std::vector< STPathElement > mPath
Definition: STPathSet.h:291
ripple::STPathSet::getSType
SerializedTypeID getSType() const override
Definition: STPathSet.h:328
ripple::STPathElement::STPathElement
STPathElement(unsigned int uType, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition: STPathSet.h:105
ripple::STPathElement::STPathElement
STPathElement(boost::optional< AccountID > const &account, boost::optional< Currency > const &currency, boost::optional< AccountID > const &issuer)
Definition: STPathSet.h:53
std::vector
STL class.
ripple::STI_PATHSET
@ STI_PATHSET
Definition: SField.h:75
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STPathElement::STPathElement
STPathElement()
Definition: STPathSet.h:114
ripple::STPathSet::emplace_back
void emplace_back(Args &&... args)
Definition: STPathSet.h:389
ripple::STPathElement::typeBoundary
@ typeBoundary
Definition: STPathSet.h:42
ripple::STPathElement::getCurrency
Currency const & getCurrency() const
Definition: STPathSet.h:168
ripple::STPathSet::STPathSet
STPathSet()=default
ripple::STPathElement::mType
unsigned int mType
Definition: STPathSet.h:196
ripple::STPath::size
std::vector< STPathElement >::size_type size() const
Definition: STPathSet.h:215
ripple::STPath::end
std::vector< STPathElement >::const_iterator end() const
Definition: STPathSet.h:253
ripple::STPath::STPath
STPath()=default
ripple::STPathElement::typeCurrency
@ typeCurrency
Definition: STPathSet.h:40
ripple::STPathSet
Definition: STPathSet.h:297
ripple::STPathSet::empty
bool empty() const
Definition: STPathSet.h:377
ripple::STPathElement::typeIssuer
@ typeIssuer
Definition: STPathSet.h:41
ripple::STPathSet::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STPathSet.cpp:129
ripple::STPath::operator[]
const STPathElement & operator[](int i) const
Definition: STPathSet.h:281
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:161
ripple::base_uint< 160, detail::AccountIDTag >
ripple::STPathSet::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STPathSet.h:310
ripple::STPathElement::getNodeType
auto getNodeType() const
Definition: STPathSet.h:124
ripple::STPath::reserve
void reserve(size_t s)
Definition: STPathSet.h:286
ripple::STPathElement::mAccountID
AccountID mAccountID
Definition: STPathSet.h:197
ripple::STPathSet::move
STBase * move(std::size_t n, void *buf) override
Definition: STPathSet.h:316
cstddef
ripple::STPathSet::STPathSet
STPathSet(SField const &n)
Definition: STPathSet.h:303
ripple::STPathElement::getIssuerID
AccountID const & getIssuerID() const
Definition: STPathSet.h:174
ripple::STPathSet::add
void add(Serializer &s) const override
Definition: STPathSet.cpp:190
ripple::STPathElement::getAccountID
AccountID const & getAccountID() const
Definition: STPathSet.h:162
ripple::STPath::emplace_back
void emplace_back(Args &&... args)
Definition: STPathSet.h:233
ripple::STPathSet::isDefault
bool isDefault() const override
Definition: STPathSet.h:340
ripple::STPath::operator[]
STPathElement & operator[](int i)
Definition: STPathSet.h:276
ripple::STPath::back
std::vector< STPathElement >::const_reference back() const
Definition: STPathSet.h:265
ripple::isXRP
bool isXRP(AccountID const &c)
Definition: AccountID.h:121
ripple::STPathElement::Type
Type
Definition: STPathSet.h:36
ripple::SerialIter
Definition: Serializer.h:311
ripple::STPathElement::hasIssuer
bool hasIssuer() const
Definition: STPathSet.h:142
ripple::STPathElement::operator!=
bool operator!=(const STPathElement &t) const
Definition: STPathSet.h:190
ripple::STPathSet::begin
std::vector< STPath >::const_iterator begin() const
Definition: STPathSet.h:359
ripple::STPathSet::operator[]
std::vector< STPath >::const_reference operator[](std::vector< STPath >::size_type n) const
Definition: STPathSet.h:347
ripple::STPathElement::isNone
bool isNone() const
Definition: STPathSet.h:154
ripple::STPath::begin
std::vector< STPathElement >::const_iterator begin() const
Definition: STPathSet.h:247
ripple::STPath::getJson
Json::Value getJson(JsonOptions) const
Definition: STPathSet.cpp:152
ripple::Serializer
Definition: Serializer.h:43
ripple::STPathElement::hasCurrency
bool hasCurrency() const
Definition: STPathSet.h:148
ripple::STPath::hasSeen
bool hasSeen(AccountID const &account, Currency const &currency, AccountID const &issuer) const
Definition: STPathSet.cpp:136
ripple::STPathElement::mIssuerID
AccountID mIssuerID
Definition: STPathSet.h:199
ripple::STPath::empty
bool empty() const
Definition: STPathSet.h:220
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::STPathSet::value
std::vector< STPath > value
Definition: STPathSet.h:395
ripple::SField
Identifies fields.
Definition: SField.h:112
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:65
ripple::STPathElement
Definition: STPathSet.h:33
std
STL namespace.
cassert
ripple::STPath::front
std::vector< STPathElement >::const_reference front() const
Definition: STPathSet.h:271
ripple::STPathElement::typeNone
@ typeNone
Definition: STPathSet.h:38
ripple::STPathSet::getJson
Json::Value getJson(JsonOptions) const override
Definition: STPathSet.cpp:180
ripple::STPath::operator==
bool operator==(STPath const &t) const
Definition: STPathSet.h:259
ripple::STPathElement::typeAccount
@ typeAccount
Definition: STPathSet.h:39
ripple::STPathSet::size
std::vector< STPath >::size_type size() const
Definition: STPathSet.h:371
ripple::STPath::STPath
STPath(std::vector< STPathElement > p)
Definition: STPathSet.h:210
std::size_t
ripple::STPathElement::hash_value_
std::size_t hash_value_
Definition: STPathSet.h:202
ripple::STPathElement::isAccount
bool isAccount() const
Definition: STPathSet.h:136
ripple::STPathElement::mCurrencyID
Currency mCurrencyID
Definition: STPathSet.h:198
ripple::STPathElement::STPathElement
STPathElement(AccountID const &account, Currency const &currency, AccountID const &issuer, bool forceCurrency=false)
Definition: STPathSet.h:87
ripple::STPathSet::assembleAdd
bool assembleAdd(STPath const &base, STPathElement const &tail)
Definition: STPathSet.cpp:108
ripple::STPath
Definition: STPathSet.h:205
ripple::noAccount
AccountID const & noAccount()
A placeholder for empty accounts.
Definition: AccountID.cpp:156
Json::Value
Represents a JSON value.
Definition: json_value.h:141
ripple::STPathElement::is_offer_
bool is_offer_
Definition: STPathSet.h:201
ripple::STPathSet::push_back
void push_back(STPath const &e)
Definition: STPathSet.h:383