rippled
AccountChannels.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2014 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/main/Application.h>
21 #include <ripple/ledger/ReadView.h>
22 #include <ripple/ledger/View.h>
23 #include <ripple/net/RPCErr.h>
24 #include <ripple/protocol/ErrorCodes.h>
25 #include <ripple/protocol/PublicKey.h>
26 #include <ripple/protocol/STAccount.h>
27 #include <ripple/protocol/jss.h>
28 #include <ripple/resource/Fees.h>
29 #include <ripple/rpc/Context.h>
30 #include <ripple/rpc/impl/RPCHelpers.h>
31 #include <ripple/rpc/impl/Tuning.h>
32 namespace ripple {
33 
34 void
35 addChannel(Json::Value& jsonLines, SLE const& line)
36 {
37  Json::Value& jDst(jsonLines.append(Json::objectValue));
38  jDst[jss::channel_id] = to_string(line.key());
39  jDst[jss::account] = to_string(line[sfAccount]);
40  jDst[jss::destination_account] = to_string(line[sfDestination]);
41  jDst[jss::amount] = line[sfAmount].getText();
42  jDst[jss::balance] = line[sfBalance].getText();
43  if (publicKeyType(line[sfPublicKey]))
44  {
45  PublicKey const pk(line[sfPublicKey]);
46  jDst[jss::public_key] = toBase58(TokenType::AccountPublic, pk);
47  jDst[jss::public_key_hex] = strHex(pk);
48  }
49  jDst[jss::settle_delay] = line[sfSettleDelay];
50  if (auto const& v = line[~sfExpiration])
51  jDst[jss::expiration] = *v;
52  if (auto const& v = line[~sfCancelAfter])
53  jDst[jss::cancel_after] = *v;
54  if (auto const& v = line[~sfSourceTag])
55  jDst[jss::source_tag] = *v;
56  if (auto const& v = line[~sfDestinationTag])
57  jDst[jss::destination_tag] = *v;
58 }
59 
60 // {
61 // account: <account>|<account_public_key>
62 // ledger_hash : <ledger>
63 // ledger_index : <ledger_index>
64 // limit: integer // optional
65 // marker: opaque // optional, resume previous query
66 // }
69 {
70  auto const& params(context.params);
71  if (!params.isMember(jss::account))
72  return RPC::missing_field_error(jss::account);
73 
75  auto result = RPC::lookupLedger(ledger, context);
76  if (!ledger)
77  return result;
78 
79  std::string strIdent(params[jss::account].asString());
80  AccountID accountID;
81 
82  if (auto const err = RPC::accountFromString(accountID, strIdent))
83  return err;
84 
85  if (!ledger->exists(keylet::account(accountID)))
86  return rpcError(rpcACT_NOT_FOUND);
87 
88  std::string strDst;
89  if (params.isMember(jss::destination_account))
90  strDst = params[jss::destination_account].asString();
91  auto hasDst = !strDst.empty();
92 
93  AccountID raDstAccount;
94  if (hasDst)
95  {
96  if (auto const err = RPC::accountFromString(raDstAccount, strDst))
97  return err;
98  }
99 
100  unsigned int limit;
101  if (auto err = readLimitField(limit, RPC::Tuning::accountChannels, context))
102  return *err;
103 
104  if (limit == 0u)
105  return rpcError(rpcINVALID_PARAMS);
106 
107  Json::Value jsonChannels{Json::arrayValue};
108  struct VisitData
109  {
111  AccountID const& accountID;
112  bool hasDst;
113  AccountID const& raDstAccount;
114  };
115  VisitData visitData = {{}, accountID, hasDst, raDstAccount};
116  visitData.items.reserve(limit);
117  uint256 startAfter = beast::zero;
118  std::uint64_t startHint = 0;
119 
120  if (params.isMember(jss::marker))
121  {
122  if (!params[jss::marker].isString())
123  return RPC::expected_field_error(jss::marker, "string");
124 
125  // Marker is composed of a comma separated index and start hint. The
126  // former will be read as hex, and the latter using boost lexical cast.
127  std::stringstream marker(params[jss::marker].asString());
128  std::string value;
129  if (!std::getline(marker, value, ','))
130  return rpcError(rpcINVALID_PARAMS);
131 
132  if (!startAfter.parseHex(value))
133  return rpcError(rpcINVALID_PARAMS);
134 
135  if (!std::getline(marker, value, ','))
136  return rpcError(rpcINVALID_PARAMS);
137 
138  try
139  {
140  startHint = boost::lexical_cast<std::uint64_t>(value);
141  }
142  catch (boost::bad_lexical_cast&)
143  {
144  return rpcError(rpcINVALID_PARAMS);
145  }
146 
147  // We then must check if the object pointed to by the marker is actually
148  // owned by the account in the request.
149  auto const sle = ledger->read({ltANY, startAfter});
150 
151  if (!sle)
152  return rpcError(rpcINVALID_PARAMS);
153 
154  if (!RPC::isOwnedByAccount(*ledger, sle, accountID))
155  return rpcError(rpcINVALID_PARAMS);
156  }
157 
158  auto count = 0;
159  std::optional<uint256> marker = {};
160  std::uint64_t nextHint = 0;
161  if (!forEachItemAfter(
162  *ledger,
163  accountID,
164  startAfter,
165  startHint,
166  limit + 1,
167  [&visitData, &accountID, &count, &limit, &marker, &nextHint](
168  std::shared_ptr<SLE const> const& sleCur) {
169  if (!sleCur)
170  {
171  assert(false);
172  return false;
173  }
174 
175  if (++count == limit)
176  {
177  marker = sleCur->key();
178  nextHint = RPC::getStartHint(sleCur, visitData.accountID);
179  }
180 
181  if (count <= limit && sleCur->getType() == ltPAYCHAN &&
182  (*sleCur)[sfAccount] == accountID &&
183  (!visitData.hasDst ||
184  visitData.raDstAccount == (*sleCur)[sfDestination]))
185  {
186  visitData.items.emplace_back(sleCur);
187  }
188 
189  return true;
190  }))
191  {
192  return rpcError(rpcINVALID_PARAMS);
193  }
194 
195  // Both conditions need to be checked because marker is set on the limit-th
196  // item, but if there is no item on the limit + 1 iteration, then there is
197  // no need to return a marker.
198  if (count == limit + 1 && marker)
199  {
200  result[jss::limit] = limit;
201  result[jss::marker] =
202  to_string(*marker) + "," + std::to_string(nextHint);
203  }
204 
205  result[jss::account] = context.app.accountIDCache().toBase58(accountID);
206 
207  for (auto const& item : visitData.items)
208  addChannel(jsonChannels, *item);
209 
211  result[jss::channels] = std::move(jsonChannels);
212  return result;
213 }
214 
215 } // namespace ripple
ripple::STLedgerEntry::key
uint256 const & key() const
Returns the 'key' (or 'index') of this item.
Definition: STLedgerEntry.h:113
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::sfSourceTag
const SF_UINT32 sfSourceTag
ripple::STLedgerEntry
Definition: STLedgerEntry.h:30
std::string
STL class.
std::shared_ptr
STL class.
ripple::rpcINVALID_PARAMS
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
ripple::rpcError
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
ripple::ltANY
@ ltANY
A special type, matching any ledger entry type.
Definition: LedgerFormats.h:164
ripple::sfDestination
const SF_ACCOUNT sfDestination
ripple::sfAmount
const SF_AMOUNT sfAmount
ripple::Resource::feeMediumBurdenRPC
const Charge feeMediumBurdenRPC
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
ripple::RPC::Context::loadType
Resource::Charge & loadType
Definition: Context.h:43
std::vector
STL class.
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:29
std::stringstream
STL class.
ripple::RPC::lookupLedger
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext &context, Json::Value &result)
Look up a ledger from a request and fill a Json::Result with the data representing a ledger.
Definition: RPCHelpers.cpp:582
ripple::VisitData
Definition: AccountLines.cpp:33
ripple::Application::accountIDCache
virtual AccountIDCache const & accountIDCache() const =0
ripple::RPC::expected_field_error
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:309
ripple::sfExpiration
const SF_UINT32 sfExpiration
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:243
ripple::publicKeyType
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:203
ripple::Keylet::key
uint256 key
Definition: Keylet.h:40
ripple::base_uint< 160, detail::AccountIDTag >
ripple::VisitData::accountID
AccountID const & accountID
Definition: AccountLines.cpp:36
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
ripple::sfSettleDelay
const SF_UINT32 sfSettleDelay
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:130
ripple::RPC::isOwnedByAccount
bool isOwnedByAccount(ReadView const &ledger, std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Tests if a SLE is owned by accountID.
Definition: RPCHelpers.cpp:111
ripple::rpcACT_NOT_FOUND
@ rpcACT_NOT_FOUND
Definition: ErrorCodes.h:70
std::to_string
T to_string(T... args)
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
ripple::ReadView::exists
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
std::uint64_t
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::getline
T getline(T... args)
ripple::RPC::getStartHint
std::uint64_t getStartHint(std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
Definition: RPCHelpers.cpp:94
ripple::sfDestinationTag
const SF_UINT32 sfDestinationTag
ripple::TokenType::AccountPublic
@ AccountPublic
ripple::VisitData::items
std::vector< RippleState::pointer > items
Definition: AccountLines.cpp:35
ripple::sfBalance
const SF_AMOUNT sfBalance
ripple::RPC::Tuning::accountChannels
static constexpr LimitRange accountChannels
Limits for the account_channels command.
Definition: rpc/impl/Tuning.h:40
std::string::empty
T empty(T... args)
ripple::sfCancelAfter
const SF_UINT32 sfCancelAfter
ripple::forEachItemAfter
bool forEachItemAfter(ReadView const &view, AccountID const &id, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(std::shared_ptr< SLE const > const &)> f)
Iterate all items after an item in an owner directory.
Definition: View.cpp:398
std::optional
ripple::doAccountChannels
Json::Value doAccountChannels(RPC::JsonContext &context)
Definition: AccountChannels.cpp:68
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:38
ripple::sfAccount
const SF_ACCOUNT sfAccount
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:45
ripple::addChannel
void addChannel(Json::Value &jsonLines, SLE const &line)
Definition: AccountChannels.cpp:35
ripple::base_uint::parseHex
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:475
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::sfPublicKey
const SF_VL sfPublicKey
ripple::RPC::accountFromString
Json::Value accountFromString(AccountID &result, std::string const &strIdent, bool bStrict)
Definition: RPCHelpers.cpp:84
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::ltPAYCHAN
@ ltPAYCHAN
A ledger object describing a single unidirectional XRP payment channel.
Definition: LedgerFormats.h:130
ripple::AccountIDCache::toBase58
std::string toBase58(AccountID const &) const
Return ripple::toBase58 for the AccountID.
Definition: AccountID.cpp:134