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#include <xrpl/basics/Log.h>
25#include <xrpl/protocol/TxFlags.h>
26
27namespace ripple {
28namespace test {
29
32inline 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
50inline 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
70inline 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
82inline 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
92class Path
93{
94public:
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
119private:
120 template <class First, class... Rest>
121 void
122 addHelper(First&& first, Rest&&... rest);
123};
124
125inline Path&
127{
128 path.emplace_back(pe);
129 return *this;
130}
131
132inline Path&
134{
135 path.emplace_back(
137 beast::zero,
138 iss.currency,
139 iss.account);
140 return *this;
141}
142
143inline Path&
145{
146 path.emplace_back(account.id(), beast::zero, beast::zero);
147 return *this;
148}
149
150template <class First, class... Rest>
151void
152Path::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
159inline Json::Value
161{
162 return path.getJson(JsonOptions::none);
163}
164
166{
167public:
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
191private:
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
Represents a JSON value.
Definition: json_value.h:148
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:179
PathSet(PathSet &&)=default
Json::Value json() const
Definition: PathSet.h:184
PathSet(PathSet const &)=default
void addHelper(First first, Rest... rest)
Definition: PathSet.h:194
PathSet & operator=(PathSet &&)=default
Json::Value json() const
Definition: PathSet.h:160
Path & operator=(Path &&)=default
Path(First &&first, Rest &&... rest)
Definition: PathSet.h:106
void addHelper(First &&first, Rest &&... rest)
Definition: PathSet.h:152
Path & push_back(Issue const &iss)
Definition: PathSet.h:133
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:118
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition: Env.h:326
Add a path.
Definition: paths.h:57
Set Paths, SendMax on a JTx.
Definition: paths.h:34
bool isOffer(jtx::Env &env, jtx::Account const &account, STAmount const &takerPays, STAmount const &takerGets)
An offer exists.
Definition: PathSet.h:71
std::size_t countOffers(jtx::Env &env, jtx::Account const &account, Issue const &takerPays, Issue const &takerGets)
Count offer.
Definition: PathSet.h:33
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:543