rippled
TestHelpers.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2023 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 #ifndef RIPPLE_TEST_JTX_TESTHELPERS_H_INCLUDED
20 #define RIPPLE_TEST_JTX_TESTHELPERS_H_INCLUDED
21 
22 #include <ripple/basics/base_uint.h>
23 #include <ripple/json/json_value.h>
24 #include <ripple/protocol/AccountID.h>
25 #include <ripple/protocol/Quality.h>
26 #include <ripple/protocol/jss.h>
27 #include <test/jtx/Env.h>
28 
29 namespace ripple {
30 namespace test {
31 namespace jtx {
32 
33 // Functions used in debugging
35 getAccountOffers(Env& env, AccountID const& acct, bool current = false);
36 
37 inline Json::Value
38 getAccountOffers(Env& env, Account const& acct, bool current = false)
39 {
40  return getAccountOffers(env, acct.id(), current);
41 }
42 
44 getAccountLines(Env& env, AccountID const& acctId);
45 
46 inline Json::Value
47 getAccountLines(Env& env, Account const& acct)
48 {
49  return getAccountLines(env, acct.id());
50 }
51 
52 template <typename... IOU>
54 getAccountLines(Env& env, AccountID const& acctId, IOU... ious)
55 {
56  auto const jrr = getAccountLines(env, acctId);
57  Json::Value res;
58  for (auto const& line : jrr[jss::lines])
59  {
60  for (auto const& iou : {ious...})
61  {
62  if (line[jss::currency].asString() == to_string(iou.currency))
63  {
64  Json::Value v;
65  v[jss::currency] = line[jss::currency];
66  v[jss::balance] = line[jss::balance];
67  v[jss::limit] = line[jss::limit];
68  v[jss::account] = line[jss::account];
69  res[jss::lines].append(v);
70  }
71  }
72  }
73  if (!res.isNull())
74  return res;
75  return jrr;
76 }
77 
78 [[nodiscard]] bool
79 checkArraySize(Json::Value const& val, unsigned int size);
80 
81 /* Path finding */
82 /******************************************************************************/
83 void
84 stpath_append_one(STPath& st, Account const& account);
85 
86 template <class T>
88 stpath_append_one(STPath& st, T const& t)
89 {
90  stpath_append_one(st, Account{t});
91 }
92 
93 void
94 stpath_append_one(STPath& st, STPathElement const& pe);
95 
96 template <class T, class... Args>
97 void
98 stpath_append(STPath& st, T const& t, Args const&... args)
99 {
100  stpath_append_one(st, t);
101  if constexpr (sizeof...(args) > 0)
102  stpath_append(st, args...);
103 }
104 
105 template <class... Args>
106 void
107 stpathset_append(STPathSet& st, STPath const& p, Args const&... args)
108 {
109  st.push_back(p);
110  if constexpr (sizeof...(args) > 0)
111  stpathset_append(st, args...);
112 }
113 
114 bool
115 equal(STAmount const& sa1, STAmount const& sa2);
116 
117 // Issue path element
119 IPE(Issue const& iss);
120 
121 template <class... Args>
122 STPath
123 stpath(Args const&... args)
124 {
125  STPath st;
126  stpath_append(st, args...);
127  return st;
128 }
129 
130 template <class... Args>
131 bool
132 same(STPathSet const& st1, Args const&... args)
133 {
134  STPathSet st2;
135  stpathset_append(st2, args...);
136  if (st1.size() != st2.size())
137  return false;
138 
139  for (auto const& p : st2)
140  {
141  if (std::find(st1.begin(), st1.end(), p) == st1.end())
142  return false;
143  }
144  return true;
145 }
146 
147 /******************************************************************************/
148 
149 XRPAmount
150 txfee(Env const& env, std::uint16_t n);
151 
153 xrpMinusFee(Env const& env, std::int64_t xrpAmount);
154 
155 bool
156 expectLine(
157  Env& env,
158  AccountID const& account,
159  STAmount const& value,
160  bool defaultLimits = false);
161 
162 template <typename... Amts>
163 bool
165  Env& env,
166  AccountID const& account,
167  STAmount const& value,
168  Amts const&... amts)
169 {
170  return expectLine(env, account, value, false) &&
171  expectLine(env, account, amts...);
172 }
173 
174 bool
175 expectLine(Env& env, AccountID const& account, None const& value);
176 
177 bool
179  Env& env,
180  AccountID const& account,
181  std::uint16_t size,
182  std::vector<Amounts> const& toMatch = {});
183 
185 ledgerEntryRoot(Env& env, Account const& acct);
186 
189  Env& env,
190  Account const& acct_a,
191  Account const& acct_b,
192  std::string const& currency);
193 
195 accountBalance(Env& env, Account const& acct);
196 
197 [[nodiscard]] bool
199  Env& env,
200  Account const& acct,
201  STAmount const& expectedValue);
202 
203 /* Escrow */
204 /******************************************************************************/
205 
207 escrow(AccountID const& account, AccountID const& to, STAmount const& amount);
208 
209 inline Json::Value
210 escrow(Account const& account, Account const& to, STAmount const& amount)
211 {
212  return escrow(account.id(), to.id(), amount);
213 }
214 
216 finish(AccountID const& account, AccountID const& from, std::uint32_t seq);
217 
218 inline Json::Value
219 finish(Account const& account, Account const& from, std::uint32_t seq)
220 {
221  return finish(account.id(), from.id(), seq);
222 }
223 
225 cancel(AccountID const& account, Account const& from, std::uint32_t seq);
226 
227 inline Json::Value
228 cancel(Account const& account, Account const& from, std::uint32_t seq)
229 {
230  return cancel(account.id(), from, seq);
231 }
232 
234  {0xA0, 0x25, 0x80, 0x20, 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC,
235  0x1C, 0x14, 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
236  0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, 0xA4, 0x95,
237  0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55, 0x81, 0x01, 0x00}};
238 
239 // A PreimageSha256 fulfillments and its associated condition.
240 std::array<std::uint8_t, 4> const fb1 = {{0xA0, 0x02, 0x80, 0x00}};
241 
244 {
245 private:
247 
248 public:
249  explicit finish_time(NetClock::time_point const& value) : value_(value)
250  {
251  }
252 
253  void
254  operator()(Env&, JTx& jt) const
255  {
257  }
258 };
259 
262 {
263 private:
265 
266 public:
267  explicit cancel_time(NetClock::time_point const& value) : value_(value)
268  {
269  }
270 
271  void
273  {
275  }
276 };
277 
278 struct condition
279 {
280 private:
282 
283 public:
284  explicit condition(Slice const& cond) : value_(strHex(cond))
285  {
286  }
287 
288  template <size_t N>
290  : condition(makeSlice(c))
291  {
292  }
293 
294  void
295  operator()(Env&, JTx& jt) const
296  {
298  }
299 };
300 
302 {
303 private:
305 
306 public:
308  {
309  }
310 
311  template <size_t N>
313  : fulfillment(makeSlice(f))
314  {
315  }
316 
317  void
318  operator()(Env&, JTx& jt) const
319  {
321  }
322 };
323 
324 /* Payment Channel */
325 /******************************************************************************/
326 
328 create(
329  AccountID const& account,
330  AccountID const& to,
331  STAmount const& amount,
332  NetClock::duration const& settleDelay,
333  PublicKey const& pk,
334  std::optional<NetClock::time_point> const& cancelAfter = std::nullopt,
335  std::optional<std::uint32_t> const& dstTag = std::nullopt);
336 
337 inline Json::Value
339  Account const& account,
340  Account const& to,
341  STAmount const& amount,
342  NetClock::duration const& settleDelay,
343  PublicKey const& pk,
344  std::optional<NetClock::time_point> const& cancelAfter = std::nullopt,
345  std::optional<std::uint32_t> const& dstTag = std::nullopt)
346 {
347  return create(
348  account.id(), to.id(), amount, settleDelay, pk, cancelAfter, dstTag);
349 }
350 
352 fund(
353  AccountID const& account,
354  uint256 const& channel,
355  STAmount const& amount,
356  std::optional<NetClock::time_point> const& expiration = std::nullopt);
357 
359 claim(
360  AccountID const& account,
361  uint256 const& channel,
362  std::optional<STAmount> const& balance = std::nullopt,
363  std::optional<STAmount> const& amount = std::nullopt,
364  std::optional<Slice> const& signature = std::nullopt,
365  std::optional<PublicKey> const& pk = std::nullopt);
366 
367 uint256
368 channel(
369  AccountID const& account,
370  AccountID const& dst,
371  std::uint32_t seqProxyValue);
372 
373 inline uint256
374 channel(Account const& account, Account const& dst, std::uint32_t seqProxyValue)
375 {
376  return channel(account.id(), dst.id(), seqProxyValue);
377 }
378 
379 STAmount
380 channelBalance(ReadView const& view, uint256 const& chan);
381 
382 bool
383 channelExists(ReadView const& view, uint256 const& chan);
384 
385 /* Crossing Limits */
386 /******************************************************************************/
387 
388 void
389 n_offers(
390  Env& env,
391  std::size_t n,
392  Account const& account,
393  STAmount const& in,
394  STAmount const& out);
395 
396 /* Pay Strand */
397 /***************************************************************/
398 
399 // Currency path element
401 cpe(Currency const& c);
402 
403 // All path element
405 allpe(AccountID const& a, Issue const& iss);
406 /***************************************************************/
407 
408 } // namespace jtx
409 } // namespace test
410 } // namespace ripple
411 
412 #endif // RIPPLE_TEST_JTX_TESTHELPERS_H_INCLUDED
ripple::test::jtx::getAccountOffers
Json::Value getAccountOffers(Env &env, AccountID const &acct, bool current)
Definition: TestHelpers.cpp:32
ripple::test::jtx::cancel_time::operator()
void operator()(jtx::Env &, jtx::JTx &jt) const
Definition: TestHelpers.h:272
ripple::makeSlice
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition: Slice.h:241
ripple::test::jtx::claim
Json::Value claim(AccountID const &account, uint256 const &channel, std::optional< STAmount > const &balance, std::optional< STAmount > const &amount, std::optional< Slice > const &signature, std::optional< PublicKey > const &pk)
Definition: TestHelpers.cpp:293
ripple::Issue
A currency issued by an account.
Definition: Issue.h:35
ripple::test::jtx::checkArraySize
bool checkArraySize(Json::Value const &val, unsigned int size)
Definition: TestHelpers.cpp:48
std::string
STL class.
ripple::test::jtx::condition::condition
condition(Slice const &cond)
Definition: TestHelpers.h:284
ripple::test::jtx::same
bool same(STPathSet const &st1, Args const &... args)
Definition: TestHelpers.h:132
ripple::test::jtx::finish
Json::Value finish(AccountID const &account, AccountID const &from, std::uint32_t seq)
Definition: TestHelpers.cpp:224
ripple::test::jtx::stpath_append_one
void stpath_append_one(STPath &st, Account const &account)
Definition: TestHelpers.cpp:56
ripple::test::jtx::ledgerEntryState
Json::Value ledgerEntryState(Env &env, Account const &acct_a, Account const &acct_b, std::string const &currency)
Definition: TestHelpers.cpp:177
ripple::test::jtx::finish_time
Set the "FinishAfter" time tag on a JTx.
Definition: TestHelpers.h:243
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::test::jtx::balance
A balance matches.
Definition: balance.h:38
std::vector
STL class.
std::find
T find(T... args)
std::chrono::duration
ripple::test::jtx::xrpMinusFee
PrettyAmount xrpMinusFee(Env const &env, std::int64_t xrpAmount)
Definition: TestHelpers.cpp:93
ripple::test::jtx::channelExists
bool channelExists(ReadView const &view, uint256 const &chan)
Definition: TestHelpers.cpp:337
Json::Value::isNull
bool isNull() const
isNull() tests to see if this field is null.
Definition: json_value.cpp:967
ripple::STPathSet::end
std::vector< STPath >::const_iterator end() const
Definition: STPathSet.h:491
ripple::SField::jsonName
const Json::StaticString jsonName
Definition: SField.h:139
ripple::test::jtx::n_offers
void n_offers(Env &env, std::size_t n, Account const &account, STAmount const &in, STAmount const &out)
Definition: TestHelpers.cpp:347
ripple::STPathSet
Definition: STPathSet.h:176
ripple::test::jtx::equal
bool equal(STAmount const &sa1, STAmount const &sa2)
Definition: TestHelpers.cpp:68
ripple::test::jtx::Account::id
AccountID id() const
Returns the Account ID.
Definition: Account.h:106
ripple::test::jtx::expiration
Set Expiration on a JTx.
Definition: Check_test.cpp:29
ripple::base_uint< 160, detail::AccountIDTag >
ripple::test::jtx::cancel_time::cancel_time
cancel_time(NetClock::time_point const &value)
Definition: TestHelpers.h:267
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
ripple::test::jtx::channel
uint256 channel(AccountID const &account, AccountID const &dst, std::uint32_t seqProxyValue)
Definition: TestHelpers.cpp:318
ripple::STPathSet::size
std::vector< STPath >::size_type size() const
Definition: STPathSet.h:497
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
std::enable_if_t
ripple::test::jtx::stpath_append
void stpath_append(STPath &st, T const &t, Args const &... args)
Definition: TestHelpers.h:98
ripple::test::jtx::IPE
STPathElement IPE(Issue const &iss)
Definition: TestHelpers.cpp:75
ripple::test::jtx::condition::condition
condition(std::array< std::uint8_t, N > const &c)
Definition: TestHelpers.h:289
ripple::test::jtx::stpathset_append
void stpathset_append(STPathSet &st, STPath const &p, Args const &... args)
Definition: TestHelpers.h:107
ripple::test::jtx::JTx
Execution context for applying a JSON transaction.
Definition: JTx.h:42
ripple::test::jtx::JTx::jv
Json::Value jv
Definition: JTx.h:44
ripple::test::jtx::fulfillment
Definition: TestHelpers.h:301
ripple::test::jtx::finish_time::operator()
void operator()(Env &, JTx &jt) const
Definition: TestHelpers.h:254
ripple::test::jtx::stpath
STPath stpath(Args const &... args)
Definition: TestHelpers.h:123
ripple::test::jtx::expectOffers
bool expectOffers(Env &env, AccountID const &account, std::uint16_t size, std::vector< Amounts > const &toMatch)
Definition: TestHelpers.cpp:140
std::array< std::uint8_t, 39 >
ripple::test::jtx::cancel_time
Set the "CancelAfter" time tag on a JTx.
Definition: TestHelpers.h:261
ripple::STAmount
Definition: STAmount.h:45
std::chrono::time_point
ripple::test::jtx::cancel_time::value_
NetClock::time_point value_
Definition: TestHelpers.h:264
ripple::ValStatus::current
@ current
This was a new validation and was added.
std::uint16_t
ripple::test::jtx::cpe
STPathElement cpe(Currency const &c)
Definition: TestHelpers.cpp:368
ripple::test::jtx::allpe
STPathElement allpe(AccountID const &a, Issue const &iss)
Definition: TestHelpers.cpp:376
ripple::test::jtx::cancel
Json::Value cancel(AccountID const &account, Account const &from, std::uint32_t seq)
Definition: TestHelpers.cpp:236
ripple::test::jtx::finish_time::finish_time
finish_time(NetClock::time_point const &value)
Definition: TestHelpers.h:249
ripple::test::jtx::condition::operator()
void operator()(Env &, JTx &jt) const
Definition: TestHelpers.h:295
ripple::test::jtx::channelBalance
STAmount channelBalance(ReadView const &view, uint256 const &chan)
Definition: TestHelpers.cpp:328
ripple::test::jtx::fulfillment::fulfillment
fulfillment(Slice condition)
Definition: TestHelpers.h:307
ripple::test::jtx::None
Definition: amount.h:57
ripple::test::jtx::seq
Set the sequence number on a JTx.
Definition: seq.h:33
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::jtx::condition::value_
std::string value_
Definition: TestHelpers.h:281
ripple::test::jtx::IOU
Converts to IOU Issue or STAmount.
Definition: amount.h:291
ripple::STPathSet::begin
std::vector< STPath >::const_iterator begin() const
Definition: STPathSet.h:485
ripple::sfCondition
const SF_VL sfCondition
ripple::STPathElement
Definition: STPathSet.h:34
ripple::test::jtx::condition
Definition: TestHelpers.h:278
ripple::test::jtx::fulfillment::operator()
void operator()(Env &, JTx &jt) const
Definition: TestHelpers.h:318
ripple::test::jtx::accountBalance
Json::Value accountBalance(Env &env, Account const &acct)
Definition: TestHelpers.cpp:193
ripple::test::jtx::fund
void fund(jtx::Env &env, jtx::Account const &gw, std::vector< jtx::Account > const &accounts, std::vector< STAmount > const &amts, Fund how)
Definition: AMMTest.cpp:35
ripple::test::jtx::cb1
constexpr std::array< std::uint8_t, 39 > cb1
Definition: TestHelpers.h:233
ripple::sfCancelAfter
const SF_UINT32 sfCancelAfter
std::optional
std::size_t
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::sfFinishAfter
const SF_UINT32 sfFinishAfter
ripple::test::jtx::Account
Immutable cryptographic account descriptor.
Definition: Account.h:37
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
ripple::test::jtx::finish_time::value_
NetClock::time_point value_
Definition: TestHelpers.h:246
ripple::test::jtx::fulfillment::value_
std::string value_
Definition: TestHelpers.h:304
ripple::test::jtx::fb1
const std::array< std::uint8_t, 4 > fb1
Definition: TestHelpers.h:240
ripple::STPath
Definition: STPathSet.h:118
ripple::test::jtx::escrow
Json::Value escrow(AccountID const &account, AccountID const &to, STAmount const &amount)
Definition: TestHelpers.cpp:212
ripple::test::jtx::expectLedgerEntryRoot
bool expectLedgerEntryRoot(Env &env, Account const &acct, STAmount const &expectedValue)
Definition: TestHelpers.cpp:200
ripple::sfFulfillment
const SF_VL sfFulfillment
ripple::test::jtx::expectLine
bool expectLine(Env &env, AccountID const &account, STAmount const &value, bool defaultLimits)
Definition: TestHelpers.cpp:100
ripple::test::jtx::create
Json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount, NetClock::duration const &settleDelay, PublicKey const &pk, std::optional< NetClock::time_point > const &cancelAfter, std::optional< std::uint32_t > const &dstTag)
Definition: TestHelpers.cpp:250
ripple::test::jtx::txfee
XRPAmount txfee(Env const &env, std::uint16_t n)
Definition: TestHelpers.cpp:87
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:116
ripple::test::jtx::getAccountLines
Json::Value getAccountLines(Env &env, AccountID const &acctId)
Definition: TestHelpers.cpp:40
ripple::test::jtx::ledgerEntryRoot
Json::Value ledgerEntryRoot(Env &env, Account const &acct)
Definition: TestHelpers.cpp:168
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::test::jtx::PrettyAmount
Represents an XRP or IOU quantity This customizes the string conversion and supports XRP conversions ...
Definition: amount.h:73
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::test::jtx::fulfillment::fulfillment
fulfillment(std::array< std::uint8_t, N > f)
Definition: TestHelpers.h:312
ripple::STPathSet::push_back
void push_back(STPath const &e)
Definition: STPathSet.h:509