rippled
Loading...
Searching...
No Matches
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 <test/jtx.h>
24
25#include <xrpl/basics/Log.h>
26#include <xrpl/protocol/TxFlags.h>
27
28namespace ripple {
29namespace test {
30
33inline std::size_t
35 jtx::Env& env,
36 jtx::Account const& account,
37 Issue const& takerPays,
38 Issue const& takerGets)
39{
40 size_t count = 0;
42 *env.current(), account, [&](std::shared_ptr<SLE const> const& sle) {
43 if (sle->getType() == ltOFFER &&
44 sle->getFieldAmount(sfTakerPays).issue() == takerPays &&
45 sle->getFieldAmount(sfTakerGets).issue() == takerGets)
46 ++count;
47 });
48 return count;
49}
50
51inline std::size_t
53 jtx::Env& env,
54 jtx::Account const& account,
55 STAmount const& takerPays,
56 STAmount const& takerGets)
57{
58 size_t count = 0;
60 *env.current(), account, [&](std::shared_ptr<SLE const> const& sle) {
61 if (sle->getType() == ltOFFER &&
62 sle->getFieldAmount(sfTakerPays) == takerPays &&
63 sle->getFieldAmount(sfTakerGets) == takerGets)
64 ++count;
65 });
66 return count;
67}
68
71inline bool
73 jtx::Env& env,
74 jtx::Account const& account,
75 STAmount const& takerPays,
76 STAmount const& takerGets)
77{
78 return countOffers(env, account, takerPays, takerGets) > 0;
79}
80
83inline bool
85 jtx::Env& env,
86 jtx::Account const& account,
87 Issue const& takerPays,
88 Issue const& takerGets)
89{
90 return countOffers(env, account, takerPays, takerGets) > 0;
91}
92
93class Path
94{
95public:
97
98 Path() = default;
99 Path(Path const&) = default;
100 Path&
101 operator=(Path const&) = default;
102 Path(Path&&) = default;
103 Path&
104 operator=(Path&&) = default;
105
106 template <class First, class... Rest>
107 explicit Path(First&& first, Rest&&... rest)
108 {
109 addHelper(std::forward<First>(first), std::forward<Rest>(rest)...);
110 }
111 Path&
112 push_back(Issue const& iss);
113 Path&
114 push_back(jtx::Account const& acc);
115 Path&
116 push_back(STPathElement const& pe);
118 json() const;
119
120private:
121 template <class First, class... Rest>
122 void
123 addHelper(First&& first, Rest&&... rest);
124};
125
126inline Path&
128{
129 path.emplace_back(pe);
130 return *this;
131}
132
133inline Path&
135{
136 path.emplace_back(
138 beast::zero,
139 iss.currency,
140 iss.account);
141 return *this;
142}
143
144inline Path&
146{
147 path.emplace_back(account.id(), beast::zero, beast::zero);
148 return *this;
149}
150
151template <class First, class... Rest>
152void
153Path::addHelper(First&& first, Rest&&... rest)
154{
155 push_back(std::forward<First>(first));
156 if constexpr (sizeof...(rest) > 0)
157 addHelper(std::forward<Rest>(rest)...);
158}
159
160inline Json::Value
162{
163 return path.getJson(JsonOptions::none);
164}
165
167{
168public:
170
171 PathSet() = default;
172 PathSet(PathSet const&) = default;
173 PathSet&
174 operator=(PathSet const&) = default;
175 PathSet(PathSet&&) = default;
176 PathSet&
177 operator=(PathSet&&) = default;
178
179 template <class First, class... Rest>
180 explicit PathSet(First&& first, Rest&&... rest)
181 {
182 addHelper(std::forward<First>(first), std::forward<Rest>(rest)...);
183 }
185 json() const
186 {
187 Json::Value v;
188 v["Paths"] = paths.getJson(JsonOptions::none);
189 return v;
190 }
191
192private:
193 template <class First, class... Rest>
194 void
195 addHelper(First first, Rest... rest)
196 {
197 paths.emplace_back(std::move(first.path));
198 if constexpr (sizeof...(rest) > 0)
199 addHelper(std::move(rest)...);
200 }
201};
202
203} // namespace test
204} // namespace ripple
205
206#endif
Represents a JSON value.
Definition: json_value.h:150
A currency issued by an account.
Definition: Issue.h:36
AccountID account
Definition: Issue.h:39
Currency currency
Definition: Issue.h:38
PathSet & operator=(PathSet const &)=default
PathSet(First &&first, Rest &&... rest)
Definition: PathSet.h:180
PathSet(PathSet &&)=default
Json::Value json() const
Definition: PathSet.h:185
PathSet(PathSet const &)=default
void addHelper(First first, Rest... rest)
Definition: PathSet.h:195
PathSet & operator=(PathSet &&)=default
Json::Value json() const
Definition: PathSet.h:161
Path & operator=(Path &&)=default
Path(First &&first, Rest &&... rest)
Definition: PathSet.h:107
void addHelper(First &&first, Rest &&... rest)
Definition: PathSet.h:153
Path & push_back(Issue const &iss)
Definition: PathSet.h:134
Path(Path &&)=default
Path(Path const &)=default
Path & operator=(Path const &)=default
Immutable cryptographic account descriptor.
Definition: Account.h:39
A transaction testing environment.
Definition: Env.h:121
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition: Env.h:331
Add a path.
Definition: paths.h:58
Set Paths, SendMax on a JTx.
Definition: paths.h:35
bool isOffer(jtx::Env &env, jtx::Account const &account, STAmount const &takerPays, STAmount const &takerGets)
An offer exists.
Definition: PathSet.h:72
std::size_t countOffers(jtx::Env &env, jtx::Account const &account, Issue const &takerPays, Issue const &takerGets)
Count offer.
Definition: PathSet.h:34
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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:656