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/jss.h>
26 #include <ripple/protocol/PublicKey.h>
27 #include <ripple/protocol/STAccount.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 addChannel (Json::Value& jsonLines, SLE const& line)
35 {
36  Json::Value& jDst (jsonLines.append (Json::objectValue));
37  jDst[jss::channel_id] = to_string (line.key ());
38  jDst[jss::account] = to_string (line[sfAccount]);
39  jDst[jss::destination_account] = to_string (line[sfDestination]);
40  jDst[jss::amount] = line[sfAmount].getText ();
41  jDst[jss::balance] = line[sfBalance].getText ();
42  if (publicKeyType(line[sfPublicKey]))
43  {
44  PublicKey const pk (line[sfPublicKey]);
45  jDst[jss::public_key] = toBase58 (TokenType::AccountPublic, pk);
46  jDst[jss::public_key_hex] = strHex (pk);
47  }
48  jDst[jss::settle_delay] = line[sfSettleDelay];
49  if (auto const& v = line[~sfExpiration])
50  jDst[jss::expiration] = *v;
51  if (auto const& v = line[~sfCancelAfter])
52  jDst[jss::cancel_after] = *v;
53  if (auto const& v = line[~sfSourceTag])
54  jDst[jss::source_tag] = *v;
55  if (auto const& v = line[~sfDestinationTag])
56  jDst[jss::destination_tag] = *v;
57 }
58 
59 // {
60 // account: <account>|<account_public_key>
61 // ledger_hash : <ledger>
62 // ledger_index : <ledger_index>
63 // limit: integer // optional
64 // marker: opaque // optional, resume previous query
65 // }
67 {
68  auto const& params (context.params);
69  if (! params.isMember (jss::account))
70  return RPC::missing_field_error (jss::account);
71 
73  auto result = RPC::lookupLedger (ledger, context);
74  if (! ledger)
75  return result;
76 
77  std::string strIdent (params[jss::account].asString ());
78  AccountID accountID;
79 
80  if (auto const err = RPC::accountFromString (accountID, strIdent))
81  return err;
82 
83  if (! ledger->exists(keylet::account (accountID)))
84  return rpcError (rpcACT_NOT_FOUND);
85 
86  std::string strDst;
87  if (params.isMember (jss::destination_account))
88  strDst = params[jss::destination_account].asString ();
89  auto hasDst = ! strDst.empty ();
90 
91  AccountID raDstAccount;
92  if (hasDst)
93  {
94  if (auto const err = RPC::accountFromString (raDstAccount, strDst))
95  return err;
96  }
97 
98  unsigned int limit;
99  if (auto err = readLimitField(limit, RPC::Tuning::accountChannels, context))
100  return *err;
101 
102  Json::Value jsonChannels{Json::arrayValue};
103  struct VisitData
104  {
106  AccountID const& accountID;
107  bool hasDst;
108  AccountID const& raDstAccount;
109  };
110  VisitData visitData = {{}, accountID, hasDst, raDstAccount};
111  visitData.items.reserve(limit+1);
112  uint256 startAfter;
113  std::uint64_t startHint;
114 
115  if (params.isMember (jss::marker))
116  {
117  Json::Value const& marker(params[jss::marker]);
118 
119  if (!marker.isString())
120  return RPC::expected_field_error(jss::marker, "string");
121 
122  if (!startAfter.SetHex(marker.asString()))
123  {
124  return rpcError(rpcINVALID_PARAMS);
125  }
126 
127  auto const sleChannel = ledger->read({ltPAYCHAN, startAfter});
128 
129  if (!sleChannel)
130  return rpcError(rpcINVALID_PARAMS);
131 
132 
133  if (!visitData.hasDst ||
134  visitData.raDstAccount == (*sleChannel)[sfDestination])
135  {
136  visitData.items.emplace_back(sleChannel);
137  startHint = sleChannel->getFieldU64(sfOwnerNode);
138  }
139  else
140  {
141  return rpcError(rpcINVALID_PARAMS);
142  }
143  }
144  else
145  {
146  startHint = 0;
147  }
148 
149  if (!forEachItemAfter(*ledger, accountID, startAfter, startHint,
150  limit-visitData.items.size()+1,
151  [&visitData, &accountID](std::shared_ptr<SLE const> const& sleCur) {
152  if (sleCur && sleCur->getType() == ltPAYCHAN &&
153  (*sleCur)[sfAccount] == accountID &&
154  (!visitData.hasDst ||
155  visitData.raDstAccount == (*sleCur)[sfDestination]))
156  {
157  visitData.items.emplace_back(sleCur);
158  return true;
159  }
160 
161  return false;
162  }))
163  {
164  return rpcError (rpcINVALID_PARAMS);
165  }
166 
167  if (visitData.items.size () == limit+1)
168  {
169  result[jss::limit] = limit;
170 
171  result[jss::marker] = to_string (visitData.items.back()->key());
172  visitData.items.pop_back ();
173  }
174 
175  result[jss::account] = context.app.accountIDCache().toBase58 (accountID);
176 
177  for (auto const& item : visitData.items)
178  addChannel (jsonChannels, *item);
179 
180  context.loadType = Resource::feeMediumBurdenRPC;
181  result[jss::channels] = std::move(jsonChannels);
182  return result;
183 }
184 
185 } // ripple
ripple::RPC::JsonContext
Definition: Context.h:52
ripple::STLedgerEntry
Definition: STLedgerEntry.h:30
std::string
STL class.
std::shared_ptr
STL class.
ripple::rpcINVALID_PARAMS
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:85
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1049
ripple::publicKeyType
boost::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Definition: PublicKey.cpp:207
ripple::Resource::feeMediumBurdenRPC
const Charge feeMediumBurdenRPC
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:44
std::vector
STL class.
ripple::sfAccount
const SF_Account sfAccount(access, STI_ACCOUNT, 1, "Account")
Definition: SField.h:460
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:41
ripple::sfOwnerNode
const SF_U64 sfOwnerNode(access, STI_UINT64, 4, "OwnerNode")
Definition: SField.h:382
ripple::sfAmount
const SF_Amount sfAmount(access, STI_AMOUNT, 1, "Amount")
Definition: SField.h:423
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:484
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:286
ripple::sfDestinationTag
const SF_U32 sfDestinationTag(access, STI_UINT32, 14, "DestinationTag")
Definition: SField.h:350
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:229
ripple::STLedgerEntry::key
uint256 const & key() const
Returns the 'key' (or 'index') of this item.
Definition: STLedgerEntry.h:86
ripple::base_uint
Definition: base_uint.h:65
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:907
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:45
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::keylet::account
static const account_t account
Definition: Indexes.h:116
ripple::STLedgerEntry::getText
std::string getText() const override
Definition: STLedgerEntry.cpp:102
ripple::rpcACT_NOT_FOUND
@ rpcACT_NOT_FOUND
Definition: ErrorCodes.h:71
ripple::base_uint::SetHex
bool SetHex(const char *psz, bool bStrict=false)
Parse a hex string into a base_uint The input can be:
Definition: base_uint.h:362
ripple::ReadView::exists
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
std::uint64_t
ripple::sfSourceTag
const SF_U32 sfSourceTag(access, STI_UINT32, 3, "SourceTag")
Definition: SField.h:339
ripple::rpcError
Json::Value rpcError(int iError, Json::Value jvResult)
Definition: RPCErr.cpp:28
ripple::sfExpiration
const SF_U32 sfExpiration(access, STI_UINT32, 10, "Expiration")
Definition: SField.h:346
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::sfPublicKey
const SF_Blob sfPublicKey(access, STI_VL, 1, "PublicKey")
Definition: SField.h:440
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::sfBalance
const SF_Amount sfBalance(access, STI_AMOUNT, 2, "Balance")
Definition: SField.h:424
ripple::TokenType::AccountPublic
@ AccountPublic
ripple::VisitData::items
std::vector< RippleState::pointer > items
Definition: AccountLines.cpp:35
ripple::RPC::Tuning::accountChannels
static constexpr LimitRange accountChannels
Limits for the account_channels command.
Definition: rpc/impl/Tuning.h:39
ripple::sfSettleDelay
const SF_U32 sfSettleDelay(access, STI_UINT32, 39, "SettleDelay")
Definition: SField.h:376
std::string::empty
T empty(T... args)
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:266
ripple::sfDestination
const SF_Account sfDestination(access, STI_ACCOUNT, 3, "Destination")
Definition: SField.h:462
ripple::ltPAYCHAN
@ ltPAYCHAN
Definition: LedgerFormats.h:84
ripple::doAccountChannels
Json::Value doAccountChannels(RPC::JsonContext &context)
Definition: AccountChannels.cpp:66
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:70
ripple::addChannel
void addChannel(Json::Value &jsonLines, SLE const &line)
Definition: AccountChannels.cpp:34
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:63
ripple::sfCancelAfter
const SF_U32 sfCancelAfter(access, STI_UINT32, 36, "CancelAfter")
Definition: SField.h:373
ripple::RPC::accountFromString
Json::Value accountFromString(AccountID &result, std::string const &strIdent, bool bStrict)
Definition: RPCHelpers.cpp:86
Json::Value
Represents a JSON value.
Definition: json_value.h:141
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:482