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/basics/CountedObject.h>
24 #include <ripple/json/json_value.h>
25 #include <ripple/protocol/SField.h>
26 #include <ripple/protocol/STBase.h>
27 #include <ripple/protocol/UintTypes.h>
28 #include <cassert>
29 #include <cstddef>
30 #include <optional>
31 
32 namespace ripple {
33 
34 class STPathElement final : public CountedObject<STPathElement>
35 {
36  unsigned int mType;
40 
41  bool is_offer_;
43 
44 public:
45  enum Type {
46  typeNone = 0x00,
48  0x01, // Rippling through an account (vs taking an offer).
49  typeCurrency = 0x10, // Currency follows.
50  typeIssuer = 0x20, // Issuer follows.
51  typeBoundary = 0xFF, // Boundary between alternate paths.
53  // Combination of all types.
54  };
55 
56  STPathElement();
57  STPathElement(STPathElement const&) = default;
59  operator=(STPathElement const&) = default;
60 
62  std::optional<AccountID> const& account,
63  std::optional<Currency> const& currency,
64  std::optional<AccountID> const& issuer);
65 
67  AccountID const& account,
68  Currency const& currency,
69  AccountID const& issuer,
70  bool forceCurrency = false);
71 
73  unsigned int uType,
74  AccountID const& account,
75  Currency const& currency,
76  AccountID const& issuer);
77 
78  auto
79  getNodeType() const;
80 
81  bool
82  isOffer() const;
83 
84  bool
85  isAccount() const;
86 
87  bool
88  hasIssuer() const;
89 
90  bool
91  hasCurrency() const;
92 
93  bool
94  isNone() const;
95 
96  // Nodes are either an account ID or a offer prefix. Offer prefixs denote a
97  // class of offers.
98  AccountID const&
99  getAccountID() const;
100 
101  Currency const&
102  getCurrency() const;
103 
104  AccountID const&
105  getIssuerID() const;
106 
107  bool
108  operator==(const STPathElement& t) const;
109 
110  bool
111  operator!=(const STPathElement& t) const;
112 
113 private:
114  static std::size_t
115  get_hash(STPathElement const& element);
116 };
117 
118 class STPath final : public CountedObject<STPath>
119 {
121 
122 public:
123  STPath() = default;
124 
126 
128  size() const;
129 
130  bool
131  empty() const;
132 
133  void
134  push_back(STPathElement const& e);
135 
136  template <typename... Args>
137  void
138  emplace_back(Args&&... args);
139 
140  bool
141  hasSeen(
142  AccountID const& account,
143  Currency const& currency,
144  AccountID const& issuer) const;
145 
147 
149  begin() const;
150 
152  end() const;
153 
154  bool
155  operator==(STPath const& t) const;
156 
158  back() const;
159 
161  front() const;
162 
164  operator[](int i);
165 
166  const STPathElement&
167  operator[](int i) const;
168 
169  void
170  reserve(size_t s);
171 };
172 
173 //------------------------------------------------------------------------------
174 
175 // A set of zero or more payment paths
176 class STPathSet final : public STBase, public CountedObject<STPathSet>
177 {
179 
180 public:
181  STPathSet() = default;
182 
183  STPathSet(SField const& n);
184  STPathSet(SerialIter& sit, SField const& name);
185 
186  void
187  add(Serializer& s) const override;
188 
189  Json::Value getJson(JsonOptions) const override;
190 
192  getSType() const override;
193 
194  bool
195  assembleAdd(STPath const& base, STPathElement const& tail);
196 
197  bool
198  isEquivalent(const STBase& t) const override;
199 
200  bool
201  isDefault() const override;
202 
203  // std::vector like interface:
206 
209 
211  begin() const;
212 
214  end() const;
215 
217  size() const;
218 
219  bool
220  empty() const;
221 
222  void
223  push_back(STPath const& e);
224 
225  template <typename... Args>
226  void
227  emplace_back(Args&&... args);
228 
229 private:
230  STBase*
231  copy(std::size_t n, void* buf) const override;
232  STBase*
233  move(std::size_t n, void* buf) override;
234 
235  friend class detail::STVar;
236 };
237 
238 // ------------ STPathElement ------------
239 
240 inline STPathElement::STPathElement() : mType(typeNone), is_offer_(true)
241 {
242  hash_value_ = get_hash(*this);
243 }
244 
246  std::optional<AccountID> const& account,
247  std::optional<Currency> const& currency,
248  std::optional<AccountID> const& issuer)
249  : mType(typeNone)
250 {
251  if (!account)
252  {
253  is_offer_ = true;
254  }
255  else
256  {
257  is_offer_ = false;
258  mAccountID = *account;
259  mType |= typeAccount;
260  assert(mAccountID != noAccount());
261  }
262 
263  if (currency)
264  {
265  mCurrencyID = *currency;
266  mType |= typeCurrency;
267  }
268 
269  if (issuer)
270  {
271  mIssuerID = *issuer;
272  mType |= typeIssuer;
273  assert(mIssuerID != noAccount());
274  }
275 
276  hash_value_ = get_hash(*this);
277 }
278 
280  AccountID const& account,
281  Currency const& currency,
282  AccountID const& issuer,
283  bool forceCurrency)
284  : mType(typeNone)
285  , mAccountID(account)
286  , mCurrencyID(currency)
287  , mIssuerID(issuer)
288  , is_offer_(isXRP(mAccountID))
289 {
290  if (!is_offer_)
291  mType |= typeAccount;
292 
293  if (forceCurrency || !isXRP(currency))
294  mType |= typeCurrency;
295 
296  if (!isXRP(issuer))
297  mType |= typeIssuer;
298 
299  hash_value_ = get_hash(*this);
300 }
301 
303  unsigned int uType,
304  AccountID const& account,
305  Currency const& currency,
306  AccountID const& issuer)
307  : mType(uType)
308  , mAccountID(account)
309  , mCurrencyID(currency)
310  , mIssuerID(issuer)
311  , is_offer_(isXRP(mAccountID))
312 {
313  hash_value_ = get_hash(*this);
314 }
315 
316 inline auto
318 {
319  return mType;
320 }
321 
322 inline bool
324 {
325  return is_offer_;
326 }
327 
328 inline bool
330 {
331  return !isOffer();
332 }
333 
334 inline bool
336 {
338 }
339 
340 inline bool
342 {
344 }
345 
346 inline bool
348 {
350 }
351 
352 // Nodes are either an account ID or a offer prefix. Offer prefixs denote a
353 // class of offers.
354 inline AccountID const&
356 {
357  return mAccountID;
358 }
359 
360 inline Currency const&
362 {
363  return mCurrencyID;
364 }
365 
366 inline AccountID const&
368 {
369  return mIssuerID;
370 }
371 
372 inline bool
374 {
375  return (mType & typeAccount) == (t.mType & typeAccount) &&
378 }
379 
380 inline bool
382 {
383  return !operator==(t);
384 }
385 
386 // ------------ STPath ------------
387 
388 inline STPath::STPath(std::vector<STPathElement> p) : mPath(std::move(p))
389 {
390 }
391 
394 {
395  return mPath.size();
396 }
397 
398 inline bool
400 {
401  return mPath.empty();
402 }
403 
404 inline void
406 {
407  mPath.push_back(e);
408 }
409 
410 template <typename... Args>
411 inline void
412 STPath::emplace_back(Args&&... args)
413 {
414  mPath.emplace_back(std::forward<Args>(args)...);
415 }
416 
419 {
420  return mPath.begin();
421 }
422 
424 STPath::end() const
425 {
426  return mPath.end();
427 }
428 
429 inline bool
430 STPath::operator==(STPath const& t) const
431 {
432  return mPath == t.mPath;
433 }
434 
437 {
438  return mPath.back();
439 }
440 
443 {
444  return mPath.front();
445 }
446 
447 inline STPathElement&
449 {
450  return mPath[i];
451 }
452 
453 inline const STPathElement&
454 STPath::operator[](int i) const
455 {
456  return mPath[i];
457 }
458 
459 inline void
461 {
462  mPath.reserve(s);
463 }
464 
465 // ------------ STPathSet ------------
466 
467 inline STPathSet::STPathSet(SField const& n) : STBase(n)
468 {
469 }
470 
471 // std::vector like interface:
474 {
475  return value[n];
476 }
477 
480 {
481  return value[n];
482 }
483 
486 {
487  return value.begin();
488 }
489 
492 {
493  return value.end();
494 }
495 
498 {
499  return value.size();
500 }
501 
502 inline bool
504 {
505  return value.empty();
506 }
507 
508 inline void
510 {
511  value.push_back(e);
512 }
513 
514 template <typename... Args>
515 inline void
516 STPathSet::emplace_back(Args&&... args)
517 {
518  value.emplace_back(std::forward<Args>(args)...);
519 }
520 
521 } // namespace ripple
522 
523 #endif
ripple::STPath::push_back
void push_back(STPathElement const &e)
Definition: STPathSet.h:405
ripple::STPathElement::operator=
STPathElement & operator=(STPathElement const &)=default
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::STPathElement::get_hash
static std::size_t get_hash(STPathElement const &element)
Definition: STPathSet.cpp:30
ripple::STPathElement::isOffer
bool isOffer() const
Definition: STPathSet.h:323
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::STPathElement::typeAll
@ typeAll
Definition: STPathSet.h:52
ripple::STPathElement::operator==
bool operator==(const STPathElement &t) const
Definition: STPathSet.h:373
ripple::STPath::size
std::vector< STPathElement >::size_type size() const
Definition: STPathSet.h:393
ripple::STPath::mPath
std::vector< STPathElement > mPath
Definition: STPathSet.h:120
ripple::STPathSet::getSType
SerializedTypeID getSType() const override
Definition: STPathSet.cpp:203
std::vector
STL class.
ripple::STPathElement::getCurrency
Currency const & getCurrency() const
Definition: STPathSet.h:361
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STPathElement::STPathElement
STPathElement()
Definition: STPathSet.h:240
ripple::STPath::front
std::vector< STPathElement >::const_reference front() const
Definition: STPathSet.h:442
ripple::STPathSet::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STPathSet.cpp:105
ripple::STPathSet::emplace_back
void emplace_back(Args &&... args)
Definition: STPathSet.h:516
ripple::STPathElement::typeBoundary
@ typeBoundary
Definition: STPathSet.h:51
ripple::STPathSet::operator[]
std::vector< STPath >::const_reference operator[](std::vector< STPath >::size_type n) const
Definition: STPathSet.h:473
ripple::STPathSet::STPathSet
STPathSet()=default
ripple::STPathSet::end
std::vector< STPath >::const_iterator end() const
Definition: STPathSet.h:491
ripple::STPathElement::mType
unsigned int mType
Definition: STPathSet.h:36
ripple::STPath::STPath
STPath()=default
ripple::STPathElement::typeCurrency
@ typeCurrency
Definition: STPathSet.h:49
ripple::STPathSet
Definition: STPathSet.h:176
ripple::STPathSet::empty
bool empty() const
Definition: STPathSet.h:503
ripple::STPathElement::typeIssuer
@ typeIssuer
Definition: STPathSet.h:50
ripple::STPathSet::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STPathSet.cpp:138
ripple::base_uint< 160, detail::AccountIDTag >
ripple::STPathElement::getNodeType
auto getNodeType() const
Definition: STPathSet.h:317
ripple::STPathSet::size
std::vector< STPath >::size_type size() const
Definition: STPathSet.h:497
ripple::STPath::reserve
void reserve(size_t s)
Definition: STPathSet.h:460
ripple::STPathElement::mAccountID
AccountID mAccountID
Definition: STPathSet.h:37
cstddef
ripple::STPath::back
std::vector< STPathElement >::const_reference back() const
Definition: STPathSet.h:436
ripple::STPathSet::add
void add(Serializer &s) const override
Definition: STPathSet.cpp:209
ripple::STPath::emplace_back
void emplace_back(Args &&... args)
Definition: STPathSet.h:412
ripple::STPathSet::isDefault
bool isDefault() const override
Definition: STPathSet.cpp:145
ripple::isXRP
bool isXRP(AccountID const &c)
Definition: AccountID.h:89
ripple::STPathElement::Type
Type
Definition: STPathSet.h:45
ripple::SerialIter
Definition: Serializer.h:310
ripple::STPathElement::hasIssuer
bool hasIssuer() const
Definition: STPathSet.h:335
ripple::STPathElement::operator!=
bool operator!=(const STPathElement &t) const
Definition: STPathSet.h:381
ripple::STPath::operator[]
STPathElement & operator[](int i)
Definition: STPathSet.h:448
ripple::STPathElement::isNone
bool isNone() const
Definition: STPathSet.h:347
ripple::STPathElement::getIssuerID
AccountID const & getIssuerID() const
Definition: STPathSet.h:367
ripple::STPath::getJson
Json::Value getJson(JsonOptions) const
Definition: STPathSet.cpp:166
ripple::Serializer
Definition: Serializer.h:39
ripple::STPathElement::hasCurrency
bool hasCurrency() const
Definition: STPathSet.h:341
ripple::STPath::hasSeen
bool hasSeen(AccountID const &account, Currency const &currency, AccountID const &issuer) const
Definition: STPathSet.cpp:151
ripple::STPathElement::mIssuerID
AccountID mIssuerID
Definition: STPathSet.h:39
ripple::STPath::empty
bool empty() const
Definition: STPathSet.h:399
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::STPath::begin
std::vector< STPathElement >::const_iterator begin() const
Definition: STPathSet.h:418
ripple::STPathSet::value
std::vector< STPath > value
Definition: STPathSet.h:178
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:66
ripple::STPathSet::begin
std::vector< STPath >::const_iterator begin() const
Definition: STPathSet.h:485
ripple::STPathElement
Definition: STPathSet.h:34
std
STL namespace.
cassert
ripple::STPath::end
std::vector< STPathElement >::const_iterator end() const
Definition: STPathSet.h:424
ripple::STPathElement::typeNone
@ typeNone
Definition: STPathSet.h:46
ripple::STPathSet::getJson
Json::Value getJson(JsonOptions) const override
Definition: STPathSet.cpp:193
ripple::STPath::operator==
bool operator==(STPath const &t) const
Definition: STPathSet.h:430
ripple::STPathElement::typeAccount
@ typeAccount
Definition: STPathSet.h:47
optional
std::size_t
ripple::STPathElement::hash_value_
std::size_t hash_value_
Definition: STPathSet.h:42
ripple::STPathElement::isAccount
bool isAccount() const
Definition: STPathSet.h:329
ripple::STPathElement::mCurrencyID
Currency mCurrencyID
Definition: STPathSet.h:38
ripple::STPathSet::assembleAdd
bool assembleAdd(STPath const &base, STPathElement const &tail)
Definition: STPathSet.cpp:117
ripple::STPath
Definition: STPathSet.h:118
ripple::noAccount
AccountID const & noAccount()
A placeholder for empty accounts.
Definition: AccountID.cpp:175
ripple::detail::STVar
Definition: STVar.h:49
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STPathSet::move
STBase * move(std::size_t n, void *buf) override
Definition: STPathSet.cpp:111
ripple::STPathElement::is_offer_
bool is_offer_
Definition: STPathSet.h:41
ripple::STPathSet::push_back
void push_back(STPath const &e)
Definition: STPathSet.h:509
ripple::STPathElement::getAccountID
AccountID const & getAccountID() const
Definition: STPathSet.h:355