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  Json::Value jsonChannels{Json::arrayValue};
105  struct VisitData
106  {
108  AccountID const& accountID;
109  bool hasDst;
110  AccountID const& raDstAccount;
111  };
112  VisitData visitData = {{}, accountID, hasDst, raDstAccount};
113  visitData.items.reserve(limit + 1);
114  uint256 startAfter;
115  std::uint64_t startHint;
116 
117  if (params.isMember(jss::marker))
118  {
119  Json::Value const& marker(params[jss::marker]);
120 
121  if (!marker.isString())
122  return RPC::expected_field_error(jss::marker, "string");
123 
124  if (!startAfter.parseHex(marker.asString()))
125  {
126  return rpcError(rpcINVALID_PARAMS);
127  }
128 
129  auto const sleChannel = ledger->read({ltPAYCHAN, startAfter});
130 
131  if (!sleChannel)
132  return rpcError(rpcINVALID_PARAMS);
133 
134  if (!visitData.hasDst ||
135  visitData.raDstAccount == (*sleChannel)[sfDestination])
136  {
137  visitData.items.emplace_back(sleChannel);
138  startHint = sleChannel->getFieldU64(sfOwnerNode);
139  }
140  else
141  {
142  return rpcError(rpcINVALID_PARAMS);
143  }
144  }
145  else
146  {
147  startHint = 0;
148  }
149 
150  if (!forEachItemAfter(
151  *ledger,
152  accountID,
153  startAfter,
154  startHint,
155  limit - visitData.items.size() + 1,
156  [&visitData, &accountID](std::shared_ptr<SLE const> const& sleCur) {
157  if (sleCur && sleCur->getType() == ltPAYCHAN &&
158  (*sleCur)[sfAccount] == accountID &&
159  (!visitData.hasDst ||
160  visitData.raDstAccount == (*sleCur)[sfDestination]))
161  {
162  visitData.items.emplace_back(sleCur);
163  return true;
164  }
165 
166  return false;
167  }))
168  {
169  return rpcError(rpcINVALID_PARAMS);
170  }
171 
172  if (visitData.items.size() == limit + 1)
173  {
174  result[jss::limit] = limit;
175 
176  result[jss::marker] = to_string(visitData.items.back()->key());
177  visitData.items.pop_back();
178  }
179 
180  result[jss::account] = context.app.accountIDCache().toBase58(accountID);
181 
182  for (auto const& item : visitData.items)
183  addChannel(jsonChannels, *item);
184 
185  context.loadType = Resource::feeMediumBurdenRPC;
186  result[jss::channels] = std::move(jsonChannels);
187  return result;
188 }
189 
190 } // namespace ripple
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
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1009
ripple::sfOwnerNode
const SF_UINT64 sfOwnerNode
ripple::publicKeyType
boost::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:203
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
std::vector
STL class.
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:29
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:45
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:487
ripple::VisitData
Definition: AccountLines.cpp:33
ripple::RPC::expected_field_error
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:304
ripple::sfExpiration
const SF_UINT32 sfExpiration
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:238
ripple::Keylet::key
uint256 key
Definition: Keylet.h:41
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:134
ripple::rpcACT_NOT_FOUND
@ rpcACT_NOT_FOUND
Definition: ErrorCodes.h:70
ripple::ReadView::exists
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
std::uint64_t
ripple::rpcError
Json::Value rpcError(int iError, Json::Value jvResult)
Definition: RPCErr.cpp:29
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
ripple::base_uint::parseHex
bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:384
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:274
ripple::ltPAYCHAN
@ ltPAYCHAN
Definition: LedgerFormats.h:83
ripple::doAccountChannels
Json::Value doAccountChannels(RPC::JsonContext &context)
Definition: AccountChannels.cpp:68
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::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:82
Json::Value
Represents a JSON value.
Definition: json_value.h:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469