rippled
PathSet.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2015 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_LEDGER_TESTS_PATHSET_H_INCLUDED
21 #define RIPPLE_LEDGER_TESTS_PATHSET_H_INCLUDED
22 
23 #include <ripple/basics/Log.h>
24 #include <ripple/protocol/TxFlags.h>
25 #include <test/jtx.h>
26 
27 namespace ripple {
28 namespace test {
29 
32 inline std::size_t
34  jtx::Env& env,
35  jtx::Account const& account,
36  Issue const& takerPays,
37  Issue const& takerGets)
38 {
39  size_t count = 0;
41  *env.current(), account, [&](std::shared_ptr<SLE const> const& sle) {
42  if (sle->getType() == ltOFFER &&
43  sle->getFieldAmount(sfTakerPays).issue() == takerPays &&
44  sle->getFieldAmount(sfTakerGets).issue() == takerGets)
45  ++count;
46  });
47  return count;
48 }
49 
50 inline std::size_t
52  jtx::Env& env,
53  jtx::Account const& account,
54  STAmount const& takerPays,
55  STAmount const& takerGets)
56 {
57  size_t count = 0;
59  *env.current(), account, [&](std::shared_ptr<SLE const> const& sle) {
60  if (sle->getType() == ltOFFER &&
61  sle->getFieldAmount(sfTakerPays) == takerPays &&
62  sle->getFieldAmount(sfTakerGets) == takerGets)
63  ++count;
64  });
65  return count;
66 }
67 
70 inline bool
72  jtx::Env& env,
73  jtx::Account const& account,
74  STAmount const& takerPays,
75  STAmount const& takerGets)
76 {
77  return countOffers(env, account, takerPays, takerGets) > 0;
78 }
79 
82 inline bool
84  jtx::Env& env,
85  jtx::Account const& account,
86  Issue const& takerPays,
87  Issue const& takerGets)
88 {
89  return countOffers(env, account, takerPays, takerGets) > 0;
90 }
91 
92 class Path
93 {
94 public:
96 
97  Path() = default;
98  Path(Path const&) = default;
99  Path&
100  operator=(Path const&) = default;
101  Path(Path&&) = default;
102  Path&
103  operator=(Path&&) = default;
104 
105  template <class First, class... Rest>
106  explicit Path(First&& first, Rest&&... rest)
107  {
108  addHelper(std::forward<First>(first), std::forward<Rest>(rest)...);
109  }
110  Path&
111  push_back(Issue const& iss);
112  Path&
113  push_back(jtx::Account const& acc);
114  Path&
115  push_back(STPathElement const& pe);
117  json() const;
118 
119 private:
120  template <class First, class... Rest>
121  void
122  addHelper(First&& first, Rest&&... rest);
123 };
124 
125 inline Path&
127 {
128  path.emplace_back(pe);
129  return *this;
130 }
131 
132 inline Path&
134 {
135  path.emplace_back(
137  beast::zero,
138  iss.currency,
139  iss.account);
140  return *this;
141 }
142 
143 inline Path&
145 {
146  path.emplace_back(account.id(), beast::zero, beast::zero);
147  return *this;
148 }
149 
150 template <class First, class... Rest>
151 void
152 Path::addHelper(First&& first, Rest&&... rest)
153 {
154  push_back(std::forward<First>(first));
155  if constexpr (sizeof...(rest) > 0)
156  addHelper(std::forward<Rest>(rest)...);
157 }
158 
159 inline Json::Value
160 Path::json() const
161 {
162  return path.getJson(JsonOptions::none);
163 }
164 
165 class PathSet
166 {
167 public:
169 
170  PathSet() = default;
171  PathSet(PathSet const&) = default;
172  PathSet&
173  operator=(PathSet const&) = default;
174  PathSet(PathSet&&) = default;
175  PathSet&
176  operator=(PathSet&&) = default;
177 
178  template <class First, class... Rest>
179  explicit PathSet(First&& first, Rest&&... rest)
180  {
181  addHelper(std::forward<First>(first), std::forward<Rest>(rest)...);
182  }
184  json() const
185  {
186  Json::Value v;
187  v["Paths"] = paths.getJson(JsonOptions::none);
188  return v;
189  }
190 
191 private:
192  template <class First, class... Rest>
193  void
194  addHelper(First first, Rest... rest)
195  {
196  paths.emplace_back(std::move(first.path));
197  if constexpr (sizeof...(rest) > 0)
198  addHelper(std::move(rest)...);
199  }
200 };
201 
202 } // namespace test
203 } // namespace ripple
204 
205 #endif
ripple::test::PathSet::operator=
PathSet & operator=(PathSet const &)=default
ripple::Issue
A currency issued by an account.
Definition: Issue.h:34
std::shared_ptr
STL class.
ripple::test::Path::json
Json::Value json() const
Definition: PathSet.h:160
ripple::Issue::currency
Currency currency
Definition: Issue.h:37
ripple::test::Path::push_back
Path & push_back(Issue const &iss)
Definition: PathSet.h:133
ripple::test::Path
Definition: PathSet.h:92
ripple::test::Path::Path
Path()=default
ripple::STPathElement::typeCurrency
@ typeCurrency
Definition: STPathSet.h:49
ripple::STPathSet
Definition: STPathSet.h:176
ripple::STPathElement::typeIssuer
@ typeIssuer
Definition: STPathSet.h:50
ripple::test::PathSet::PathSet
PathSet()=default
ripple::test::Path::addHelper
void addHelper(First &&first, Rest &&... rest)
Definition: PathSet.h:152
ripple::test::PathSet::PathSet
PathSet(First &&first, Rest &&... rest)
Definition: PathSet.h:179
ripple::JsonOptions::none
@ none
ripple::test::PathSet
Definition: PathSet.h:165
ripple::test::jtx::paths
Set Paths, SendMax on a JTx.
Definition: paths.h:32
ripple::STAmount
Definition: STAmount.h:45
ripple::test::Path::Path
Path(First &&first, Rest &&... rest)
Definition: PathSet.h:106
ripple::test::isOffer
bool isOffer(jtx::Env &env, jtx::Account const &account, STAmount const &takerPays, STAmount const &takerGets)
An offer exists.
Definition: PathSet.h:71
ripple::test::jtx::path
Add a path.
Definition: paths.h:55
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::PathSet::addHelper
void addHelper(First first, Rest... rest)
Definition: PathSet.h:194
ripple::test::countOffers
std::size_t countOffers(jtx::Env &env, jtx::Account const &account, Issue const &takerPays, Issue const &takerGets)
Count offer.
Definition: PathSet.h:33
ripple::STPathElement
Definition: STPathSet.h:34
ripple::test::PathSet::paths
STPathSet paths
Definition: PathSet.h:168
ripple::test::PathSet::json
Json::Value json() const
Definition: PathSet.h:184
ripple::forEachItem
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items in the given directory.
Definition: View.cpp:367
std::size_t
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
ripple::STPath
Definition: STPathSet.h:118
ripple::test::jtx::Env::current
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition: Env.h:300
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:116
ripple::Issue::account
AccountID account
Definition: Issue.h:38
ripple::test::Path::operator=
Path & operator=(Path const &)=default
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::test::Path::path
STPath path
Definition: PathSet.h:95