rippled
Loading...
Searching...
No Matches
AccountChannels.cpp
1#include <xrpld/rpc/Context.h>
2#include <xrpld/rpc/detail/RPCHelpers.h>
3#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
4#include <xrpld/rpc/detail/Tuning.h>
5
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/ledger/View.h>
8#include <xrpl/protocol/ErrorCodes.h>
9#include <xrpl/protocol/PublicKey.h>
10#include <xrpl/protocol/RPCErr.h>
11#include <xrpl/protocol/jss.h>
12#include <xrpl/resource/Fees.h>
13
14namespace ripple {
15
16void
17addChannel(Json::Value& jsonLines, SLE const& line)
18{
19 Json::Value& jDst(jsonLines.append(Json::objectValue));
20 jDst[jss::channel_id] = to_string(line.key());
21 jDst[jss::account] = to_string(line[sfAccount]);
22 jDst[jss::destination_account] = to_string(line[sfDestination]);
23 jDst[jss::amount] = line[sfAmount].getText();
24 jDst[jss::balance] = line[sfBalance].getText();
25 if (publicKeyType(line[sfPublicKey]))
26 {
27 PublicKey const pk(line[sfPublicKey]);
28 jDst[jss::public_key] = toBase58(TokenType::AccountPublic, pk);
29 jDst[jss::public_key_hex] = strHex(pk);
30 }
31 jDst[jss::settle_delay] = line[sfSettleDelay];
32 if (auto const& v = line[~sfExpiration])
33 jDst[jss::expiration] = *v;
34 if (auto const& v = line[~sfCancelAfter])
35 jDst[jss::cancel_after] = *v;
36 if (auto const& v = line[~sfSourceTag])
37 jDst[jss::source_tag] = *v;
38 if (auto const& v = line[~sfDestinationTag])
39 jDst[jss::destination_tag] = *v;
40}
41
42// {
43// account: <account>
44// ledger_hash : <ledger>
45// ledger_index : <ledger_index>
46// limit: integer // optional
47// marker: opaque // optional, resume previous query
48// }
51{
52 auto const& params(context.params);
53 if (!params.isMember(jss::account))
54 return RPC::missing_field_error(jss::account);
55
56 if (!params[jss::account].isString())
57 return RPC::invalid_field_error(jss::account);
58
60 auto result = RPC::lookupLedger(ledger, context);
61 if (!ledger)
62 return result;
63
64 auto id = parseBase58<AccountID>(params[jss::account].asString());
65 if (!id)
66 {
68 }
69 AccountID const accountID{std::move(id.value())};
70
71 if (!ledger->exists(keylet::account(accountID)))
73
74 std::string strDst;
75 if (params.isMember(jss::destination_account))
76 strDst = params[jss::destination_account].asString();
77
78 auto const raDstAccount = [&]() -> std::optional<AccountID> {
79 return strDst.empty() ? std::nullopt : parseBase58<AccountID>(strDst);
80 }();
81 if (!strDst.empty() && !raDstAccount)
83
84 unsigned int limit;
85 if (auto err = readLimitField(limit, RPC::Tuning::accountChannels, context))
86 return *err;
87
88 Json::Value jsonChannels{Json::arrayValue};
89 struct VisitData
90 {
92 AccountID const& accountID;
93 std::optional<AccountID> const& raDstAccount;
94 };
95 VisitData visitData = {{}, accountID, raDstAccount};
96 visitData.items.reserve(limit);
97 uint256 startAfter = beast::zero;
98 std::uint64_t startHint = 0;
99
100 if (params.isMember(jss::marker))
101 {
102 if (!params[jss::marker].isString())
103 return RPC::expected_field_error(jss::marker, "string");
104
105 // Marker is composed of a comma separated index and start hint. The
106 // former will be read as hex, and the latter using boost lexical cast.
107 std::stringstream marker(params[jss::marker].asString());
108 std::string value;
109 if (!std::getline(marker, value, ','))
111
112 if (!startAfter.parseHex(value))
114
115 if (!std::getline(marker, value, ','))
117
118 try
119 {
120 startHint = boost::lexical_cast<std::uint64_t>(value);
121 }
122 catch (boost::bad_lexical_cast&)
123 {
125 }
126
127 // We then must check if the object pointed to by the marker is actually
128 // owned by the account in the request.
129 auto const sle = ledger->read({ltANY, startAfter});
130
131 if (!sle)
133
134 if (!RPC::isRelatedToAccount(*ledger, sle, accountID))
136 }
137
138 auto count = 0;
139 std::optional<uint256> marker = {};
140 std::uint64_t nextHint = 0;
141 if (!forEachItemAfter(
142 *ledger,
143 accountID,
144 startAfter,
145 startHint,
146 limit + 1,
147 [&visitData, &accountID, &count, &limit, &marker, &nextHint](
148 std::shared_ptr<SLE const> const& sleCur) {
149 if (!sleCur)
150 {
151 // LCOV_EXCL_START
152 UNREACHABLE("ripple::doAccountChannels : null SLE");
153 return false;
154 // LCOV_EXCL_STOP
155 }
156
157 if (++count == limit)
158 {
159 marker = sleCur->key();
160 nextHint = RPC::getStartHint(sleCur, visitData.accountID);
161 }
162
163 if (count <= limit && sleCur->getType() == ltPAYCHAN &&
164 (*sleCur)[sfAccount] == accountID &&
165 (!visitData.raDstAccount ||
166 *visitData.raDstAccount == (*sleCur)[sfDestination]))
167 {
168 visitData.items.emplace_back(sleCur);
169 }
170
171 return true;
172 }))
173 {
175 }
176
177 // Both conditions need to be checked because marker is set on the limit-th
178 // item, but if there is no item on the limit + 1 iteration, then there is
179 // no need to return a marker.
180 if (count == limit + 1 && marker)
181 {
182 result[jss::limit] = limit;
183 result[jss::marker] =
184 to_string(*marker) + "," + std::to_string(nextHint);
185 }
186
187 result[jss::account] = toBase58(accountID);
188
189 for (auto const& item : visitData.items)
190 addChannel(jsonChannels, *item);
191
193 result[jss::channels] = std::move(jsonChannels);
194 return result;
195}
196
197} // namespace ripple
Represents a JSON value.
Definition json_value.h:131
Value & append(Value const &value)
Append value to array at the end.
A public key.
Definition PublicKey.h:43
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:484
T empty(T... args)
T getline(T... args)
T is_same_v
@ arrayValue
array value (ordered list)
Definition json_value.h:26
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:27
static LimitRange constexpr accountChannels
Limits for the account_channels command.
Json::Value invalid_field_error(std::string const &name)
Definition ErrorCodes.h:306
bool isRelatedToAccount(ReadView const &ledger, std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Tests if a SLE is owned by accountID.
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition ErrorCodes.h:330
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.
std::uint64_t getStartHint(std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:264
Charge const feeMediumBurdenRPC
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
void addChannel(Json::Value &jsonLines, SLE const &line)
@ rpcACT_NOT_FOUND
Definition ErrorCodes.h:51
@ rpcACT_MALFORMED
Definition ErrorCodes.h:71
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:65
Json::Value rpcError(int iError)
Definition RPCErr.cpp:12
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:11
bool forEachItemAfter(ReadView const &view, Keylet const &root, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items after an item in the given directory.
Definition View.cpp:665
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
@ ltANY
A special type, matching any ledger entry type.
Json::Value doAccountChannels(RPC::JsonContext &context)
uint256 key
Definition Keylet.h:21
Resource::Charge & loadType
Definition Context.h:23
T to_string(T... args)