rippled
STPathSet.cpp
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 #include <ripple/protocol/STPathSet.h>
21 #include <ripple/protocol/jss.h>
22 #include <ripple/basics/contract.h>
23 #include <ripple/basics/Log.h>
24 #include <ripple/basics/strHex.h>
25 #include <ripple/basics/StringUtilities.h>
26 #include <cstddef>
27 
28 namespace ripple {
29 
32 {
33  std::size_t hash_account = 2654435761;
34  std::size_t hash_currency = 2654435761;
35  std::size_t hash_issuer = 2654435761;
36 
37  // NIKB NOTE: This doesn't have to be a secure hash as speed is more
38  // important. We don't even really need to fully hash the whole
39  // base_uint here, as a few bytes would do for our use.
40 
41  for (auto const x : element.getAccountID ())
42  hash_account += (hash_account * 257) ^ x;
43 
44  for (auto const x : element.getCurrency ())
45  hash_currency += (hash_currency * 509) ^ x;
46 
47  for (auto const x : element.getIssuerID ())
48  hash_issuer += (hash_issuer * 911) ^ x;
49 
50  return (hash_account ^ hash_currency ^ hash_issuer);
51 }
52 
54  : STBase(name)
55 {
57  for(;;)
58  {
59  int iType = sit.get8 ();
60 
61  if (iType == STPathElement::typeNone ||
63  {
64  if (path.empty ())
65  {
66  JLOG (debugLog().error())
67  << "Empty path in pathset";
68  Throw<std::runtime_error> ("empty path");
69  }
70 
71  push_back (path);
72  path.clear ();
73 
74  if (iType == STPathElement::typeNone)
75  return;
76  }
77  else if (iType & ~STPathElement::typeAll)
78  {
79  JLOG (debugLog().error())
80  << "Bad path element " << iType << " in pathset";
81  Throw<std::runtime_error> ("bad path element");
82  }
83  else
84  {
85  auto hasAccount = iType & STPathElement::typeAccount;
86  auto hasCurrency = iType & STPathElement::typeCurrency;
87  auto hasIssuer = iType & STPathElement::typeIssuer;
88 
89  AccountID account;
90  Currency currency;
91  AccountID issuer;
92 
93  if (hasAccount)
94  account = sit.get160();
95 
96  if (hasCurrency)
97  currency = sit.get160();
98 
99  if (hasIssuer)
100  issuer = sit.get160();
101 
102  path.emplace_back (account, currency, issuer, hasCurrency);
103  }
104  }
105 }
106 
107 bool
108 STPathSet::assembleAdd(STPath const& base, STPathElement const& tail)
109 { // assemble base+tail and add it to the set if it's not a duplicate
110  value.push_back (base);
111 
113 
114  STPath& newPath = *it;
115  newPath.push_back (tail);
116 
117  while (++it != value.rend ())
118  {
119  if (*it == newPath)
120  {
121  value.pop_back ();
122  return false;
123  }
124  }
125  return true;
126 }
127 
128 bool
130 {
131  const STPathSet* v = dynamic_cast<const STPathSet*> (&t);
132  return v && (value == v->value);
133 }
134 
135 bool
137  AccountID const& account, Currency const& currency,
138  AccountID const& issuer) const
139 {
140  for (auto& p: mPath)
141  {
142  if (p.getAccountID () == account
143  && p.getCurrency () == currency
144  && 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  elem[jss::type_hex] = strHex (iType);
163 
164  if (iType & STPathElement::typeAccount)
165  elem[jss::account] = to_string (it.getAccountID ());
166 
167  if (iType & STPathElement::typeCurrency)
168  elem[jss::currency] = to_string (it.getCurrency ());
169 
170  if (iType & STPathElement::typeIssuer)
171  elem[jss::issuer] = to_string (it.getIssuerID ());
172 
173  ret.append (elem);
174  }
175 
176  return ret;
177 }
178 
181 {
183  for (auto it: value)
184  ret.append (it.getJson (options));
185 
186  return ret;
187 }
188 
189 void
191 {
192  assert (fName->isBinary ());
193  assert (fName->fieldType == STI_PATHSET);
194  bool first = true;
195 
196  for (auto const& spPath : value)
197  {
198  if (!first)
200 
201  for (auto const& speElement : spPath)
202  {
203  int iType = speElement.getNodeType ();
204 
205  s.add8 (iType);
206 
207  if (iType & STPathElement::typeAccount)
208  s.add160 (speElement.getAccountID ());
209 
210  if (iType & STPathElement::typeCurrency)
211  s.add160 (speElement.getCurrency ());
212 
213  if (iType & STPathElement::typeIssuer)
214  s.add160 (speElement.getIssuerID ());
215  }
216 
217  first = false;
218  }
219 
221 }
222 
223 } // ripple
ripple::STPath::push_back
void push_back(STPathElement const &e)
Definition: STPathSet.h:226
ripple::Serializer::add8
int add8(unsigned char byte)
Definition: Serializer.cpp:162
ripple::STPathElement::get_hash
static std::size_t get_hash(STPathElement const &element)
Definition: STPathSet.cpp:31
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::SField::isBinary
bool isBinary() const
Definition: SField.h:199
ripple::STPathElement::typeAll
@ typeAll
Definition: STPathSet.h:43
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:44
ripple::STPath::mPath
std::vector< STPathElement > mPath
Definition: STPathSet.h:291
std::vector
STL class.
ripple::STI_PATHSET
@ STI_PATHSET
Definition: SField.h:75
ripple::STPathElement::typeBoundary
@ typeBoundary
Definition: STPathSet.h:42
ripple::STPathElement::getCurrency
Currency const & getCurrency() const
Definition: STPathSet.h:168
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:41
ripple::STPathSet::STPathSet
STPathSet()=default
ripple::STPathElement::typeCurrency
@ typeCurrency
Definition: STPathSet.h:40
ripple::STPathSet
Definition: STPathSet.h:297
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:417
ripple::STPathElement::typeIssuer
@ typeIssuer
Definition: STPathSet.h:41
ripple::STPathSet::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STPathSet.cpp:129
ripple::SField::fieldType
const SerializedTypeID fieldType
Definition: SField.h:134
ripple::base_uint< 160, detail::AccountIDTag >
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:907
ripple::SerialIter::get8
unsigned char get8()
Definition: Serializer.cpp:430
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:45
ripple::SerialIter::get160
uint160 get160()
Definition: Serializer.h:374
cstddef
ripple::STPathElement::getIssuerID
AccountID const & getIssuerID() const
Definition: STPathSet.h:174
ripple::STPathSet::add
void add(Serializer &s) const override
Definition: STPathSet.cpp:190
ripple::STPathElement::getAccountID
AccountID const & getAccountID() const
Definition: STPathSet.h:162
ripple::SerialIter
Definition: Serializer.h:311
ripple::STPath::getJson
Json::Value getJson(JsonOptions) const
Definition: STPathSet.cpp:152
ripple::Serializer
Definition: Serializer.h:43
ripple::STPath::hasSeen
bool hasSeen(AccountID const &account, Currency const &currency, AccountID const &issuer) const
Definition: STPathSet.cpp:136
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:395
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:65
ripple::STPathElement
Definition: STPathSet.h:33
ripple::STPathElement::typeNone
@ typeNone
Definition: STPathSet.h:38
ripple::STPathSet::getJson
Json::Value getJson(JsonOptions) const override
Definition: STPathSet.cpp:180
ripple::STPathElement::typeAccount
@ typeAccount
Definition: STPathSet.h:39
ripple::STBase::fName
SField const * fName
Definition: STBase.h:156
std::size_t
ripple::Serializer::add160
int add160(base_uint< 160, Tag > const &i)
Definition: Serializer.h:105
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:70
ripple::STPathSet::assembleAdd
bool assembleAdd(STPath const &base, STPathElement const &tail)
Definition: STPathSet.cpp:108
ripple::STPath
Definition: STPathSet.h:205
Json::Value
Represents a JSON value.
Definition: json_value.h:141
ripple::STPathSet::push_back
void push_back(STPath const &e)
Definition: STPathSet.h:383