rippled
Loading...
Searching...
No Matches
STPathSet.cpp
1#include <xrpl/basics/Log.h>
2#include <xrpl/basics/contract.h>
3#include <xrpl/beast/utility/instrumentation.h>
4#include <xrpl/json/json_value.h>
5#include <xrpl/protocol/AccountID.h>
6#include <xrpl/protocol/SField.h>
7#include <xrpl/protocol/STBase.h>
8#include <xrpl/protocol/STPathSet.h>
9#include <xrpl/protocol/Serializer.h>
10#include <xrpl/protocol/UintTypes.h>
11#include <xrpl/protocol/jss.h>
12
13#include <cstddef>
14#include <stdexcept>
15#include <utility>
16#include <vector>
17
18namespace xrpl {
19
22{
23 std::size_t hash_account = 2654435761;
24 std::size_t hash_currency = 2654435761;
25 std::size_t hash_issuer = 2654435761;
26
27 // NIKB NOTE: This doesn't have to be a secure hash as speed is more
28 // important. We don't even really need to fully hash the whole
29 // base_uint here, as a few bytes would do for our use.
30
31 for (auto const x : element.getAccountID())
32 hash_account += (hash_account * 257) ^ x;
33
34 for (auto const x : element.getCurrency())
35 hash_currency += (hash_currency * 509) ^ x;
36
37 for (auto const x : element.getIssuerID())
38 hash_issuer += (hash_issuer * 911) ^ x;
39
40 return (hash_account ^ hash_currency ^ hash_issuer);
41}
42
43STPathSet::STPathSet(SerialIter& sit, SField const& name) : STBase(name)
44{
46 for (;;)
47 {
48 int iType = sit.get8();
49
51 {
52 if (path.empty())
53 {
54 JLOG(debugLog().error()) << "Empty path in pathset";
55 Throw<std::runtime_error>("empty path");
56 }
57
58 push_back(path);
59 path.clear();
60
61 if (iType == STPathElement::typeNone)
62 return;
63 }
64 else if (iType & ~STPathElement::typeAll)
65 {
66 JLOG(debugLog().error()) << "Bad path element " << iType << " in pathset";
67 Throw<std::runtime_error>("bad path element");
68 }
69 else
70 {
71 auto hasAccount = iType & STPathElement::typeAccount;
72 auto hasCurrency = iType & STPathElement::typeCurrency;
73 auto hasIssuer = iType & STPathElement::typeIssuer;
74
75 AccountID account;
76 Currency currency;
77 AccountID issuer;
78
79 if (hasAccount)
80 account = sit.get160();
81
82 if (hasCurrency)
83 currency = sit.get160();
84
85 if (hasIssuer)
86 issuer = sit.get160();
87
88 path.emplace_back(account, currency, issuer, hasCurrency);
89 }
90 }
91}
92
93STBase*
94STPathSet::copy(std::size_t n, void* buf) const
95{
96 return emplace(n, buf, *this);
97}
98
99STBase*
101{
102 return emplace(n, buf, std::move(*this));
103}
104
105bool
107{ // assemble base+tail and add it to the set if it's not a duplicate
108 value.push_back(base);
109
111
112 STPath& newPath = *it;
113 newPath.push_back(tail);
114
115 while (++it != value.rend())
116 {
117 if (*it == newPath)
118 {
119 value.pop_back();
120 return false;
121 }
122 }
123 return true;
124}
125
126bool
128{
129 STPathSet const* v = dynamic_cast<STPathSet const*>(&t);
130 return v && (value == v->value);
131}
132
133bool
135{
136 return value.empty();
137}
138
139bool
140STPath::hasSeen(AccountID const& account, Currency const& currency, AccountID const& issuer) const
141{
142 for (auto& p : mPath)
143 {
144 if (p.getAccountID() == account && p.getCurrency() == currency && p.getIssuerID() == issuer)
145 return true;
146 }
147
148 return false;
149}
150
153{
155
156 for (auto it : mPath)
157 {
159 auto const iType = it.getNodeType();
160
161 elem[jss::type] = iType;
162
163 if (iType & STPathElement::typeAccount)
164 elem[jss::account] = to_string(it.getAccountID());
165
166 if (iType & STPathElement::typeCurrency)
167 elem[jss::currency] = to_string(it.getCurrency());
168
169 if (iType & STPathElement::typeIssuer)
170 elem[jss::issuer] = to_string(it.getIssuerID());
171
172 ret.append(elem);
173 }
174
175 return ret;
176}
177
180{
182 for (auto it : value)
183 ret.append(it.getJson(options));
184
185 return ret;
186}
187
190{
191 return STI_PATHSET;
192}
193
194void
196{
197 XRPL_ASSERT(getFName().isBinary(), "xrpl::STPathSet::add : field is binary");
198 XRPL_ASSERT(getFName().fieldType == STI_PATHSET, "xrpl::STPathSet::add : valid field type");
199 bool first = true;
200
201 for (auto const& spPath : value)
202 {
203 if (!first)
205
206 for (auto const& speElement : spPath)
207 {
208 int iType = speElement.getNodeType();
209
210 s.add8(iType);
211
212 if (iType & STPathElement::typeAccount)
213 s.addBitString(speElement.getAccountID());
214
215 if (iType & STPathElement::typeCurrency)
216 s.addBitString(speElement.getCurrency());
217
218 if (iType & STPathElement::typeIssuer)
219 s.addBitString(speElement.getIssuerID());
220 }
221
222 first = false;
223 }
224
226}
227
228} // namespace xrpl
Represents a JSON value.
Definition json_value.h:131
Value & append(Value const &value)
Append value to array at the end.
Identifies fields.
Definition SField.h:127
A type which can be exported to a well known binary format.
Definition STBase.h:116
SField const & getFName() const
Definition STBase.cpp:122
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:214
AccountID const & getAccountID() const
Definition STPathSet.h:321
static std::size_t get_hash(STPathElement const &element)
Definition STPathSet.cpp:21
Currency const & getCurrency() const
Definition STPathSet.h:327
AccountID const & getIssuerID() const
Definition STPathSet.h:333
void add(Serializer &s) const override
STPathSet()=default
bool assembleAdd(STPath const &base, STPathElement const &tail)
void push_back(STPath const &e)
Definition STPathSet.h:474
std::vector< STPath > value
Definition STPathSet.h:152
STBase * copy(std::size_t n, void *buf) const override
Definition STPathSet.cpp:94
STBase * move(std::size_t n, void *buf) override
bool empty() const
Definition STPathSet.h:468
Json::Value getJson(JsonOptions) const override
bool isEquivalent(STBase const &t) const override
bool isDefault() const override
SerializedTypeID getSType() const override
Json::Value getJson(JsonOptions) const
void push_back(STPathElement const &e)
Definition STPathSet.h:370
bool hasSeen(AccountID const &account, Currency const &currency, AccountID const &issuer) const
std::vector< STPathElement > mPath
Definition STPathSet.h:97
uint160 get160()
Definition Serializer.h:383
unsigned char get8()
int addBitString(base_uint< Bits, Tag > const &v)
Definition Serializer.h:106
int add8(unsigned char i)
T clear(T... args)
T emplace_back(T... args)
T empty(T... args)
@ arrayValue
array value (ordered list)
Definition json_value.h:26
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:27
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:445
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
SerializedTypeID
Definition SField.h:91
T rbegin(T... args)
T rend(T... args)
Note, should be treated as flags that can be | and &.
Definition STBase.h:18