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  typeNone = 0x00,
39  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 std::size_t
49  get_hash(STPathElement const& element);
50 
51 public:
53  boost::optional<AccountID> const& account,
54  boost::optional<Currency> const& currency,
55  boost::optional<AccountID> const& issuer)
56  : mType(typeNone)
57  {
58  if (!account)
59  {
60  is_offer_ = true;
61  }
62  else
63  {
64  is_offer_ = false;
65  mAccountID = *account;
66  mType |= typeAccount;
67  assert(mAccountID != noAccount());
68  }
69 
70  if (currency)
71  {
72  mCurrencyID = *currency;
74  }
75 
76  if (issuer)
77  {
78  mIssuerID = *issuer;
79  mType |= typeIssuer;
80  assert(mIssuerID != noAccount());
81  }
82 
83  hash_value_ = get_hash(*this);
84  }
85 
87  AccountID const& account,
88  Currency const& currency,
89  AccountID const& issuer,
90  bool forceCurrency = false)
91  : mType(typeNone)
92  , mAccountID(account)
93  , mCurrencyID(currency)
94  , mIssuerID(issuer)
96  {
97  if (!is_offer_)
98  mType |= typeAccount;
99 
100  if (forceCurrency || !isXRP(currency))
101  mType |= typeCurrency;
102 
103  if (!isXRP(issuer))
104  mType |= typeIssuer;
105 
106  hash_value_ = get_hash(*this);
107  }
108 
110  unsigned int uType,
111  AccountID const& account,
112  Currency const& currency,
113  AccountID const& issuer)
114  : mType(uType)
115  , mAccountID(account)
116  , mCurrencyID(currency)
117  , mIssuerID(issuer)
119  {
120  hash_value_ = get_hash(*this);
121  }
122 
124  {
125  hash_value_ = get_hash(*this);
126  }
127 
128  STPathElement(STPathElement const&) = default;
130  operator=(STPathElement const&) = default;
131 
132  auto
133  getNodeType() const
134  {
135  return mType;
136  }
137 
138  bool
139  isOffer() const
140  {
141  return is_offer_;
142  }
143 
144  bool
145  isAccount() const
146  {
147  return !isOffer();
148  }
149 
150  bool
151  hasIssuer() const
152  {
154  }
155 
156  bool
157  hasCurrency() const
158  {
160  }
161 
162  bool
163  isNone() const
164  {
166  }
167 
168  // Nodes are either an account ID or a offer prefix. Offer prefixs denote a
169  // class of offers.
170  AccountID const&
171  getAccountID() const
172  {
173  return mAccountID;
174  }
175 
176  Currency const&
177  getCurrency() const
178  {
179  return mCurrencyID;
180  }
181 
182  AccountID const&
183  getIssuerID() const
184  {
185  return mIssuerID;
186  }
187 
188  bool
189  operator==(const STPathElement& t) const
190  {
191  return (mType & typeAccount) == (t.mType & typeAccount) &&
194  }
195 
196  bool
197  operator!=(const STPathElement& t) const
198  {
199  return !operator==(t);
200  }
201 
202 private:
203  unsigned int mType;
207 
208  bool is_offer_;
210 };
211 
212 class STPath
213 {
214 public:
215  STPath() = default;
216 
218  {
219  }
220 
222  size() const
223  {
224  return mPath.size();
225  }
226 
227  bool
228  empty() const
229  {
230  return mPath.empty();
231  }
232 
233  void
235  {
236  mPath.push_back(e);
237  }
238 
239  template <typename... Args>
240  void
241  emplace_back(Args&&... args)
242  {
243  mPath.emplace_back(std::forward<Args>(args)...);
244  }
245 
246  bool
247  hasSeen(
248  AccountID const& account,
249  Currency const& currency,
250  AccountID const& issuer) const;
251 
253 
255  begin() const
256  {
257  return mPath.begin();
258  }
259 
261  end() const
262  {
263  return mPath.end();
264  }
265 
266  bool
267  operator==(STPath const& t) const
268  {
269  return mPath == t.mPath;
270  }
271 
273  back() const
274  {
275  return mPath.back();
276  }
277 
279  front() const
280  {
281  return mPath.front();
282  }
283 
285  operator[](int i)
286  {
287  return mPath[i];
288  }
289 
290  const STPathElement&
291  operator[](int i) const
292  {
293  return mPath[i];
294  }
295 
296  void
297  reserve(size_t s)
298  {
299  mPath.reserve(s);
300  }
301 
302 private:
304 };
305 
306 //------------------------------------------------------------------------------
307 
308 // A set of zero or more payment paths
309 class STPathSet final : public STBase
310 {
311 public:
312  STPathSet() = default;
313 
314  STPathSet(SField const& n) : STBase(n)
315  {
316  }
317 
318  STPathSet(SerialIter& sit, SField const& name);
319 
320  STBase*
321  copy(std::size_t n, void* buf) const override
322  {
323  return emplace(n, buf, *this);
324  }
325 
326  STBase*
327  move(std::size_t n, void* buf) override
328  {
329  return emplace(n, buf, std::move(*this));
330  }
331 
332  void
333  add(Serializer& s) const override;
334 
335  Json::Value getJson(JsonOptions) const override;
336 
338  getSType() const override
339  {
340  return STI_PATHSET;
341  }
342 
343  bool
344  assembleAdd(STPath const& base, STPathElement const& tail);
345 
346  bool
347  isEquivalent(const STBase& t) const override;
348 
349  bool
350  isDefault() const override
351  {
352  return value.empty();
353  }
354 
355  // std::vector like interface:
358  {
359  return value[n];
360  }
361 
364  {
365  return value[n];
366  }
367 
369  begin() const
370  {
371  return value.begin();
372  }
373 
375  end() const
376  {
377  return value.end();
378  }
379 
381  size() const
382  {
383  return value.size();
384  }
385 
386  bool
387  empty() const
388  {
389  return value.empty();
390  }
391 
392  void
393  push_back(STPath const& e)
394  {
395  value.push_back(e);
396  }
397 
398  template <typename... Args>
399  void
400  emplace_back(Args&&... args)
401  {
402  value.emplace_back(std::forward<Args>(args)...);
403  }
404 
405 private:
407 };
408 
409 } // namespace ripple
410 
411 #endif
ripple::STPathSet::operator[]
std::vector< STPath >::reference operator[](std::vector< STPath >::size_type n)
Definition: STPathSet.h:363
ripple::STPath::push_back
void push_back(STPathElement const &e)
Definition: STPathSet.h:234
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:139
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::STPathSet::end
std::vector< STPath >::const_iterator end() const
Definition: STPathSet.h:375
ripple::STPathElement::typeAll
@ typeAll
Definition: STPathSet.h:43
ripple::STPathElement::operator==
bool operator==(const STPathElement &t) const
Definition: STPathSet.h:189
ripple::STPath::mPath
std::vector< STPathElement > mPath
Definition: STPathSet.h:303
ripple::STPathSet::getSType
SerializedTypeID getSType() const override
Definition: STPathSet.h:338
ripple::STPathElement::STPathElement
STPathElement(unsigned int uType, AccountID const &account, Currency const &currency, AccountID const &issuer)
Definition: STPathSet.h:109
ripple::STPathElement::STPathElement
STPathElement(boost::optional< AccountID > const &account, boost::optional< Currency > const &currency, boost::optional< AccountID > const &issuer)
Definition: STPathSet.h:52
std::vector
STL class.
ripple::STI_PATHSET
@ STI_PATHSET
Definition: SField.h:74
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STPathElement::STPathElement
STPathElement()
Definition: STPathSet.h:123
ripple::STPathSet::emplace_back
void emplace_back(Args &&... args)
Definition: STPathSet.h:400
ripple::STPathElement::typeBoundary
@ typeBoundary
Definition: STPathSet.h:42
ripple::STPathElement::getCurrency
Currency const & getCurrency() const
Definition: STPathSet.h:177
ripple::STPathSet::STPathSet
STPathSet()=default
ripple::STPathElement::mType
unsigned int mType
Definition: STPathSet.h:203
ripple::STPath::size
std::vector< STPathElement >::size_type size() const
Definition: STPathSet.h:222
ripple::STPath::end
std::vector< STPathElement >::const_iterator end() const
Definition: STPathSet.h:261
ripple::STPath::STPath
STPath()=default
ripple::STPathElement::typeCurrency
@ typeCurrency
Definition: STPathSet.h:40
ripple::STPathSet
Definition: STPathSet.h:309
ripple::STPathSet::empty
bool empty() const
Definition: STPathSet.h:387
ripple::STPathElement::typeIssuer
@ typeIssuer
Definition: STPathSet.h:41
ripple::STPathSet::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STPathSet.cpp:127
ripple::STPath::operator[]
const STPathElement & operator[](int i) const
Definition: STPathSet.h:291
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:149
ripple::base_uint< 160, detail::AccountIDTag >
ripple::STPathSet::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STPathSet.h:321
ripple::STPathElement::getNodeType
auto getNodeType() const
Definition: STPathSet.h:133
ripple::STPath::reserve
void reserve(size_t s)
Definition: STPathSet.h:297
ripple::STPathElement::mAccountID
AccountID mAccountID
Definition: STPathSet.h:204
ripple::STPathSet::move
STBase * move(std::size_t n, void *buf) override
Definition: STPathSet.h:327
cstddef
ripple::STPathSet::STPathSet
STPathSet(SField const &n)
Definition: STPathSet.h:314
ripple::STPathElement::getIssuerID
AccountID const & getIssuerID() const
Definition: STPathSet.h:183
ripple::STPathSet::add
void add(Serializer &s) const override
Definition: STPathSet.cpp:186
ripple::STPathElement::getAccountID
AccountID const & getAccountID() const
Definition: STPathSet.h:171
ripple::STPath::emplace_back
void emplace_back(Args &&... args)
Definition: STPathSet.h:241
ripple::STPathSet::isDefault
bool isDefault() const override
Definition: STPathSet.h:350
ripple::STPath::operator[]
STPathElement & operator[](int i)
Definition: STPathSet.h:285
ripple::STPath::back
std::vector< STPathElement >::const_reference back() const
Definition: STPathSet.h:273
ripple::isXRP
bool isXRP(AccountID const &c)
Definition: AccountID.h:89
ripple::STPathElement::Type
Type
Definition: STPathSet.h:36
ripple::SerialIter
Definition: Serializer.h:308
ripple::STPathElement::hasIssuer
bool hasIssuer() const
Definition: STPathSet.h:151
ripple::STPathElement::operator!=
bool operator!=(const STPathElement &t) const
Definition: STPathSet.h:197
ripple::STPathSet::begin
std::vector< STPath >::const_iterator begin() const
Definition: STPathSet.h:369
ripple::STPathSet::operator[]
std::vector< STPath >::const_reference operator[](std::vector< STPath >::size_type n) const
Definition: STPathSet.h:357
ripple::STPathElement::isNone
bool isNone() const
Definition: STPathSet.h:163
ripple::STPath::begin
std::vector< STPathElement >::const_iterator begin() const
Definition: STPathSet.h:255
ripple::STPath::getJson
Json::Value getJson(JsonOptions) const
Definition: STPathSet.cpp:149
ripple::Serializer
Definition: Serializer.h:39
ripple::STPathElement::hasCurrency
bool hasCurrency() const
Definition: STPathSet.h:157
ripple::STPath::hasSeen
bool hasSeen(AccountID const &account, Currency const &currency, AccountID const &issuer) const
Definition: STPathSet.cpp:134
ripple::STPathElement::mIssuerID
AccountID mIssuerID
Definition: STPathSet.h:206
ripple::STPath::empty
bool empty() const
Definition: STPathSet.h:228
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:406
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:62
ripple::STPathElement
Definition: STPathSet.h:33
std
STL namespace.
cassert
ripple::STPath::front
std::vector< STPathElement >::const_reference front() const
Definition: STPathSet.h:279
ripple::STPathElement::typeNone
@ typeNone
Definition: STPathSet.h:37
ripple::STPathSet::getJson
Json::Value getJson(JsonOptions) const override
Definition: STPathSet.cpp:176
ripple::STPath::operator==
bool operator==(STPath const &t) const
Definition: STPathSet.h:267
ripple::STPathElement::typeAccount
@ typeAccount
Definition: STPathSet.h:38
ripple::STPathSet::size
std::vector< STPath >::size_type size() const
Definition: STPathSet.h:381
ripple::STPath::STPath
STPath(std::vector< STPathElement > p)
Definition: STPathSet.h:217
std::size_t
ripple::STPathElement::hash_value_
std::size_t hash_value_
Definition: STPathSet.h:209
ripple::STPathElement::isAccount
bool isAccount() const
Definition: STPathSet.h:145
ripple::STPathElement::mCurrencyID
Currency mCurrencyID
Definition: STPathSet.h:205
ripple::STPathElement::STPathElement
STPathElement(AccountID const &account, Currency const &currency, AccountID const &issuer, bool forceCurrency=false)
Definition: STPathSet.h:86
ripple::STPathSet::assembleAdd
bool assembleAdd(STPath const &base, STPathElement const &tail)
Definition: STPathSet.cpp:106
ripple::STPath
Definition: STPathSet.h:212
ripple::noAccount
AccountID const & noAccount()
A placeholder for empty accounts.
Definition: AccountID.cpp:97
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STPathElement::is_offer_
bool is_offer_
Definition: STPathSet.h:208
ripple::STPathSet::push_back
void push_back(STPath const &e)
Definition: STPathSet.h:393