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