xrpld
Loading...
Searching...
No Matches
STPathSet.h
1#pragma once
2
3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/UnorderedContainers.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/json/json_value.h>
7#include <xrpl/protocol/AccountID.h>
8#include <xrpl/protocol/PathAsset.h>
9#include <xrpl/protocol/SField.h>
10#include <xrpl/protocol/STBase.h>
11#include <xrpl/protocol/Serializer.h>
12#include <xrpl/protocol/UintTypes.h>
13
14#include <cstddef>
15#include <optional>
16#include <utility>
17#include <vector>
18
19namespace xrpl {
20
21class STPathElement final : public CountedObject<STPathElement>
22{
23 unsigned int type_;
27
30
31public:
32 // Bitwise values (typeCurrency | typeMPT)
33 // NOLINTNEXTLINE(cppcoreguidelines-use-enum-class)
34 enum Type {
35 TypeNone = 0x00,
36 TypeAccount = 0x01, // Rippling through an account (vs taking an offer).
37 TypeCurrency = 0x10, // Currency follows.
38 TypeIssuer = 0x20, // Issuer follows.
39 TypeMpt = 0x40, // MPT follows.
40 TypeBoundary = 0xFF, // Boundary between alternate paths.
43 // Combination of all types.
44 };
45
47 STPathElement(STPathElement const&) = default;
49 operator=(STPathElement const&) = default;
50
52 std::optional<AccountID> const& account,
53 std::optional<PathAsset> const& asset,
54 std::optional<AccountID> const& issuer);
55
57 AccountID const& account,
58 PathAsset const& asset,
59 AccountID const& issuer,
60 bool forceAsset = false);
61
63 unsigned int uType,
64 AccountID const& account,
65 PathAsset const& asset,
66 AccountID const& issuer);
67
68 [[nodiscard]] auto
69 getNodeType() const;
70
71 [[nodiscard]] bool
72 isOffer() const;
73
74 [[nodiscard]] bool
75 isAccount() const;
76
77 [[nodiscard]] bool
78 hasIssuer() const;
79
80 [[nodiscard]] bool
81 hasCurrency() const;
82
83 [[nodiscard]] bool
84 hasMPT() const;
85
86 [[nodiscard]] bool
87 hasAsset() const;
88
89 [[nodiscard]] bool
90 isNone() const;
91
92 // Nodes are either an account ID or a offer prefix. Offer prefixs denote a
93 // class of offers.
94 [[nodiscard]] AccountID const&
95 getAccountID() const;
96
97 [[nodiscard]] PathAsset const&
98 getPathAsset() const;
99
100 [[nodiscard]] Currency const&
101 getCurrency() const;
102
103 [[nodiscard]] MPTID const&
104 getMPTID() const;
105
106 [[nodiscard]] AccountID const&
107 getIssuerID() const;
108
109 [[nodiscard]] bool
110 isType(Type const& pe) const;
111
112 [[nodiscard]] size_t
113 getHash() const;
114
115 bool
116 operator==(STPathElement const& t) const;
117
118private:
119 static std::size_t
120 getHash(STPathElement const& element);
121};
122
123class STPath final : public CountedObject<STPath>
124{
126
127public:
128 STPath() = default;
129
131
133 size() const;
134
135 [[nodiscard]] bool
136 empty() const;
137
138 void
139 pushBack(STPathElement const& e);
140
141 template <typename... Args>
142 void
143 emplaceBack(Args&&... args);
144
145 [[nodiscard]] bool
146 hasSeen(AccountID const& account, PathAsset const& asset, AccountID const& issuer) const;
147
148 [[nodiscard]] json::Value getJson(JsonOptions) const;
149
150 [[nodiscard]] std::vector<STPathElement>::const_iterator
151 begin() const;
152
153 [[nodiscard]] std::vector<STPathElement>::const_iterator
154 end() const;
155
156 bool
157 operator==(STPath const& t) const;
158
160 back() const;
161
163 front() const;
164
166 operator[](int i);
167
168 STPathElement const&
169 operator[](int i) const;
170
171 void
172 reserve(size_t s);
173};
174
175template <class Hasher>
176void
177hash_append(Hasher& h, STPath const& p) noexcept
178{
179 for (auto const& e : p)
180 {
181 beast::hash_append(h, e.getHash());
182 }
183}
184
185//------------------------------------------------------------------------------
186
187// A set of zero or more payment paths
188class STPathSet final : public STBase, public CountedObject<STPathSet>
189{
192
193public:
194 STPathSet() = default;
195
196 STPathSet(SField const& n);
197 STPathSet(SerialIter& sit, SField const& name);
198
199 void
200 add(Serializer& s) const override;
201
202 [[nodiscard]] json::Value getJson(JsonOptions) const override;
203
204 [[nodiscard]] SerializedTypeID
205 getSType() const override;
206
207 bool
208 assembleAdd(STPath const& base, STPathElement const& tail);
209
210 [[nodiscard]] bool
211 isEquivalent(STBase const& t) const override;
212
213 [[nodiscard]] bool
214 isDefault() const override;
215
216 // std::vector like interface:
219
220 [[nodiscard]] std::vector<STPath>::const_iterator
221 begin() const;
222
223 [[nodiscard]] std::vector<STPath>::const_iterator
224 end() const;
225
227 size() const;
228
229 [[nodiscard]] bool
230 empty() const;
231
232 void
233 pushBack(STPath const& e);
234
235 template <typename... Args>
236 void
237 emplaceBack(Args&&... args);
238
239 [[nodiscard]] bool
240 contains(STPath const& path) const;
241
242private:
243 STBase*
244 copy(std::size_t n, void* buf) const override;
245 STBase*
246 move(std::size_t n, void* buf) override;
247
248 friend class detail::STVar;
249};
250
251// ------------ STPathElement ------------
252
254{
255 // hashValue_ is derived from the whole object, so it is computed in the body
256 // once every other member is initialized (as in the other constructors).
257 // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
258 hashValue_ = getHash(*this);
259}
260
262 std::optional<AccountID> const& account,
263 std::optional<PathAsset> const& asset,
264 std::optional<AccountID> const& issuer)
265 : type_(TypeNone)
266{
267 if (!account)
268 {
269 isOffer_ = true;
270 }
271 else
272 {
273 isOffer_ = false;
274 accountID_ = *account;
276 XRPL_ASSERT(
277 accountID_ != noAccount(), "xrpl::STPathElement::STPathElement : account is set");
278 }
279
280 if (asset)
281 {
282 assetID_ = *asset;
284 }
285
286 if (issuer)
287 {
288 issuerID_ = *issuer;
289 type_ |= TypeIssuer;
290 XRPL_ASSERT(issuerID_ != noAccount(), "xrpl::STPathElement::STPathElement : issuer is set");
291 }
292
293 hashValue_ = getHash(*this);
294}
295
297 AccountID const& account,
298 PathAsset const& asset,
299 AccountID const& issuer,
300 bool forceAsset)
301 : type_(TypeNone)
302 , accountID_(account)
303 , assetID_(asset)
304 , issuerID_(issuer)
306{
307 if (!isOffer_)
309
310 if (forceAsset || !isXRP(assetID_))
311 type_ |= asset.holds<Currency>() ? TypeCurrency : TypeMpt;
312
313 if (!isXRP(issuer))
314 type_ |= TypeIssuer;
315
316 hashValue_ = getHash(*this);
317}
318
320 unsigned int uType,
321 AccountID const& account,
322 PathAsset const& asset,
323 AccountID const& issuer)
324 : type_(uType)
325 , accountID_(account)
326 , assetID_(asset)
327 , issuerID_(issuer)
329{
330 assetID_.visit(
331 [&](Currency const&) { type_ = type_ & (~Type::TypeMpt); },
332 [&](MPTID const&) { type_ = type_ & (~Type::TypeCurrency); });
333 // hashValue_ must be computed after type_ is adjusted above, so this cannot
334 // be a member initializer.
335 // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
336 hashValue_ = getHash(*this);
337}
338
339inline auto
341{
342 return type_;
343}
344
345inline bool
347{
348 return isOffer_;
349}
350
351inline bool
353{
354 return !isOffer();
355}
356
357inline bool
359{
360 return (type_ & pe) != 0u;
361}
362
363inline bool
368
369inline bool
374
375inline bool
380
381inline bool
386
387inline bool
392
393// Nodes are either an account ID or a offer prefix. Offer prefixs denote a
394// class of offers.
395inline AccountID const&
397{
398 return accountID_;
399}
400
401inline PathAsset const&
403{
404 return assetID_;
405}
406
407inline Currency const&
409{
410 return assetID_.get<Currency>();
411}
412
413inline MPTID const&
415{
416 return assetID_.get<MPTID>();
417}
418
419inline AccountID const&
421{
422 return issuerID_;
423}
424
425inline bool
427{
428 return (type_ & TypeAccount) == (t.type_ & TypeAccount) && hashValue_ == t.hashValue_ &&
430}
431
432// ------------ STPath ------------
433
435{
436}
437
440{
441 return path_.size();
442}
443
444inline bool
446{
447 return path_.empty();
448}
449
450inline void
452{
453 path_.push_back(e);
454}
455
456template <typename... Args>
457inline void
458STPath::emplaceBack(Args&&... args)
459{
460 path_.emplace_back(std::forward<Args>(args)...);
461}
462
463inline std::vector<STPathElement>::const_iterator
465{
466 return path_.begin();
467}
468
469inline std::vector<STPathElement>::const_iterator
471{
472 return path_.end();
473}
474
475inline bool
477{
478 return path_ == t.path_;
479}
480
483{
484 return path_.back();
485}
486
489{
490 return path_.front();
491}
492
493inline STPathElement&
495{
496 return path_[i];
497}
498
499inline STPathElement const&
501{
502 return path_[i];
503}
504
505inline void
507{
508 path_.reserve(s);
509}
510
511// ------------ STPathSet ------------
512
513inline STPathSet::STPathSet(SField const& n) : STBase(n)
514{
515}
516
517// std::vector like interface:
523
524inline std::vector<STPath>::const_iterator
526{
527 return value_.begin();
528}
529
530inline std::vector<STPath>::const_iterator
532{
533 return value_.end();
534}
535
538{
539 return value_.size();
540}
541
542inline bool
544{
545 return value_.empty();
546}
547
548inline void
550{
551 value_.push_back(e);
552 seenHashes_.emplace(value_.back());
553}
554
555template <typename... Args>
556inline void
558{
559 value_.emplace_back(std::forward<Args>(args)...);
560 seenHashes_.emplace(value_.back());
561}
562
563inline bool
565{
566 return seenHashes_.contains(path);
567}
568
569} // namespace xrpl
Represents a JSON value.
Definition json_value.h:117
constexpr bool holds() const
Definition PathAsset.h:76
Identifies fields.
Definition SField.h:132
auto getNodeType() const
Definition STPathSet.h:340
std::size_t hashValue_
Definition STPathSet.h:29
AccountID const & getAccountID() const
Definition STPathSet.h:396
bool isOffer() const
Definition STPathSet.h:346
AccountID issuerID_
Definition STPathSet.h:26
unsigned int type_
Definition STPathSet.h:23
bool isNone() const
Definition STPathSet.h:388
PathAsset const & getPathAsset() const
Definition STPathSet.h:402
AccountID accountID_
Definition STPathSet.h:24
MPTID const & getMPTID() const
Definition STPathSet.h:414
STPathElement(STPathElement const &)=default
bool hasAsset() const
Definition STPathSet.h:382
size_t getHash() const
Definition STPathSet.cpp:55
Currency const & getCurrency() const
Definition STPathSet.h:408
bool operator==(STPathElement const &t) const
Definition STPathSet.h:426
STPathElement & operator=(STPathElement const &)=default
bool hasMPT() const
Definition STPathSet.h:376
AccountID const & getIssuerID() const
Definition STPathSet.h:420
bool isAccount() const
Definition STPathSet.h:352
bool hasIssuer() const
Definition STPathSet.h:364
PathAsset assetID_
Definition STPathSet.h:25
bool isType(Type const &pe) const
Definition STPathSet.h:358
bool hasCurrency() const
Definition STPathSet.h:370
void add(Serializer &s) const override
std::vector< STPath >::const_iterator begin() const
Definition STPathSet.h:525
STPathSet()=default
bool assembleAdd(STPath const &base, STPathElement const &tail)
void emplaceBack(Args &&... args)
Definition STPathSet.h:557
std::vector< STPath >::const_reference operator[](std::vector< STPath >::size_type n) const
Definition STPathSet.h:519
STBase * copy(std::size_t n, void *buf) const override
std::vector< STPath >::size_type size() const
Definition STPathSet.h:537
json::Value getJson(JsonOptions) const override
void pushBack(STPath const &e)
Definition STPathSet.h:549
std::vector< STPath > value_
Definition STPathSet.h:190
STBase * move(std::size_t n, void *buf) override
std::vector< STPath >::const_iterator end() const
Definition STPathSet.h:531
bool empty() const
Definition STPathSet.h:543
xrpl::hardened_hash_set< STPath > seenHashes_
Definition STPathSet.h:191
bool isEquivalent(STBase const &t) const override
bool isDefault() const override
SerializedTypeID getSType() const override
bool contains(STPath const &path) const
Definition STPathSet.h:564
std::vector< STPathElement > path_
Definition STPathSet.h:125
std::vector< STPathElement >::size_type size() const
Definition STPathSet.h:439
bool hasSeen(AccountID const &account, PathAsset const &asset, AccountID const &issuer) const
bool empty() const
Definition STPathSet.h:445
bool operator==(STPath const &t) const
Definition STPathSet.h:476
STPathElement & operator[](int i)
Definition STPathSet.h:494
void pushBack(STPathElement const &e)
Definition STPathSet.h:451
std::vector< STPathElement >::const_iterator end() const
Definition STPathSet.h:470
std::vector< STPathElement >::const_reference front() const
Definition STPathSet.h:488
void reserve(size_t s)
Definition STPathSet.h:506
void emplaceBack(Args &&... args)
Definition STPathSet.h:458
std::vector< STPathElement >::const_reference back() const
Definition STPathSet.h:482
STPath()=default
std::vector< STPathElement >::const_iterator begin() const
Definition STPathSet.h:464
json::Value getJson(JsonOptions) const
T forward(T... args)
void hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isXRP(AccountID const &c)
Definition AccountID.h:84
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
std::unordered_set< Value, Hash, Pred, Allocator > hardened_hash_set
SerializedTypeID
Definition SField.h:94
BaseUInt< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
Definition UintTypes.h:54
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
AccountID const & noAccount()
A placeholder for empty accounts.
void hash_append(Hasher &h, Slice const &v)
Definition Slice.h:194
Note, should be treated as flags that can be | and &.
Definition STBase.h:22