rippled
paths.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/app/paths/Pathfinder.h>
21 #include <ripple/protocol/jss.h>
22 #include <test/jtx/paths.h>
23 
24 namespace ripple {
25 namespace test {
26 namespace jtx {
27 
28 void
29 paths::operator()(Env& env, JTx& jt) const
30 {
31  auto& jv = jt.jv;
32  auto const from = env.lookup(jv[jss::Account].asString());
33  auto const to = env.lookup(jv[jss::Destination].asString());
34  auto const amount = amountFromJson(sfAmount, jv[jss::Amount]);
35  Pathfinder pf(
36  std::make_shared<RippleLineCache>(
37  env.current(), env.app().journal("RippleLineCache")),
38  from,
39  to,
40  in_.currency,
41  in_.account,
42  amount,
43  std::nullopt,
44  env.app());
45  if (!pf.findPaths(depth_))
46  return;
47 
48  STPath fp;
50  auto const found = pf.getBestPaths(limit_, fp, {}, in_.account);
51 
52  // VFALCO TODO API to allow caller to examine the STPathSet
53  // VFALCO isDefault should be renamed to empty()
54  if (!found.isDefault())
55  jv[jss::Paths] = found.getJson(JsonOptions::none);
56 }
57 
58 //------------------------------------------------------------------------------
59 
62 {
64 }
65 
66 void
67 path::append_one(Account const& account)
68 {
69  auto& jv = create();
70  jv["account"] = toBase58(account.id());
71 }
72 
73 void
74 path::append_one(IOU const& iou)
75 {
76  auto& jv = create();
77  jv["currency"] = to_string(iou.issue().currency);
78  jv["account"] = toBase58(iou.issue().account);
79 }
80 
81 void
83 {
84  auto& jv = create();
85  jv["currency"] = to_string(book.currency);
86  jv["issuer"] = toBase58(book.account);
87 }
88 
89 void
90 path::operator()(Env& env, JTx& jt) const
91 {
92  jt.jv["Paths"].append(jv_);
93 }
94 
95 } // namespace jtx
96 } // namespace test
97 } // namespace ripple
ripple::Pathfinder
Calculates payment paths.
Definition: Pathfinder.h:38
ripple::sfAmount
const SF_AMOUNT sfAmount
ripple::test::jtx::BookSpec::currency
ripple::Currency currency
Definition: amount.h:158
ripple::Pathfinder::computePathRanks
void computePathRanks(int maxPaths, std::function< bool(void)> const &continueCallback={})
Compute the rankings of the paths.
Definition: Pathfinder.cpp:411
ripple::test::jtx::path::jv_
Json::Value jv_
Definition: paths.h:58
ripple::Issue::currency
Currency currency
Definition: Issue.h:37
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:104
ripple::test::jtx::path::append_one
void append_one(Account const &account)
Definition: paths.cpp:67
ripple::test::jtx::IOU::issue
Issue issue() const
Definition: amount.h:303
ripple::Pathfinder::findPaths
bool findPaths(int searchLevel, std::function< bool(void)> const &continueCallback={})
Definition: Pathfinder.cpp:195
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:241
ripple::test::jtx::paths::limit_
unsigned int limit_
Definition: paths.h:37
ripple::test::jtx::BookSpec
Definition: amount.h:155
ripple::test::jtx::paths::depth_
int depth_
Definition: paths.h:36
ripple::test::jtx::paths::operator()
void operator()(Env &, JTx &jt) const
Definition: paths.cpp:29
ripple::test::jtx::paths::in_
Issue in_
Definition: paths.h:35
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::JsonOptions::none
@ none
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::Env::lookup
Account const & lookup(AccountID const &id) const
Returns the Account given the AccountID.
Definition: Env.cpp:162
ripple::amountFromJson
STAmount amountFromJson(SField const &name, Json::Value const &v)
Definition: STAmount.cpp:929
ripple::Pathfinder::getBestPaths
STPathSet getBestPaths(int maxPaths, STPath &fullLiquidityPath, STPathSet const &extraPaths, AccountID const &srcIssuer, std::function< bool(void)> const &continueCallback={})
Definition: Pathfinder.cpp:567
ripple::test::jtx::BookSpec::account
AccountID account
Definition: amount.h:157
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Application::journal
virtual beast::Journal journal(std::string const &name)=0
ripple::test::jtx::IOU
Converts to IOU Issue or STAmount.
Definition: amount.h:291
ripple::STPathSet::getJson
Json::Value getJson(JsonOptions) const override
Definition: STPathSet.cpp:193
ripple::test::jtx::path::create
Json::Value & create()
Definition: paths.cpp:61
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::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
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::test::jtx::path::operator()
void operator()(Env &, JTx &jt) const
Definition: paths.cpp:90