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/Tuning.h>
4
5#include <xrpl/ledger/ReadView.h>
6#include <xrpl/ledger/View.h>
7#include <xrpl/protocol/ErrorCodes.h>
8#include <xrpl/protocol/PublicKey.h>
9#include <xrpl/protocol/RPCErr.h>
10#include <xrpl/protocol/jss.h>
11#include <xrpl/resource/Fees.h>
12
13namespace ripple {
14
15void
16addChannel(Json::Value& jsonLines, SLE const& line)
17{
18 Json::Value& jDst(jsonLines.append(Json::objectValue));
19 jDst[jss::channel_id] = to_string(line.key());
20 jDst[jss::account] = to_string(line[sfAccount]);
21 jDst[jss::destination_account] = to_string(line[sfDestination]);
22 jDst[jss::amount] = line[sfAmount].getText();
23 jDst[jss::balance] = line[sfBalance].getText();
24 if (publicKeyType(line[sfPublicKey]))
25 {
26 PublicKey const pk(line[sfPublicKey]);
27 jDst[jss::public_key] = toBase58(TokenType::AccountPublic, pk);
28 jDst[jss::public_key_hex] = strHex(pk);
29 }
30 jDst[jss::settle_delay] = line[sfSettleDelay];
31 if (auto const& v = line[~sfExpiration])
32 jDst[jss::expiration] = *v;
33 if (auto const& v = line[~sfCancelAfter])
34 jDst[jss::cancel_after] = *v;
35 if (auto const& v = line[~sfSourceTag])
36 jDst[jss::source_tag] = *v;
37 if (auto const& v = line[~sfDestinationTag])
38 jDst[jss::destination_tag] = *v;
39}
40
41// {
42// account: <account>
43// ledger_hash : <ledger>
44// ledger_index : <ledger_index>
45// limit: integer // optional
46// marker: opaque // optional, resume previous query
47// }
50{
51 auto const& params(context.params);
52 if (!params.isMember(jss::account))
53 return RPC::missing_field_error(jss::account);
54
55 if (!params[jss::account].isString())
56 return RPC::invalid_field_error(jss::account);
57
59 auto result = RPC::lookupLedger(ledger, context);
60 if (!ledger)
61 return result;
62
63 auto id = parseBase58<AccountID>(params[jss::account].asString());
64 if (!id)
65 {
67 }
68 AccountID const accountID{std::move(id.value())};
69
70 if (!ledger->exists(keylet::account(accountID)))
72
73 std::string strDst;
74 if (params.isMember(jss::destination_account))
75 strDst = params[jss::destination_account].asString();
76
77 auto const raDstAccount = [&]() -> std::optional<AccountID> {
78 return strDst.empty() ? std::nullopt : parseBase58<AccountID>(strDst);
79 }();
80 if (!strDst.empty() && !raDstAccount)
82
83 unsigned int limit;
84 if (auto err = readLimitField(limit, RPC::Tuning::accountChannels, context))
85 return *err;
86
87 Json::Value jsonChannels{Json::arrayValue};
88 struct VisitData
89 {
91 AccountID const& accountID;
92 std::optional<AccountID> const& raDstAccount;
93 };
94 VisitData visitData = {{}, accountID, raDstAccount};
95 visitData.items.reserve(limit);
96 uint256 startAfter = beast::zero;
97 std::uint64_t startHint = 0;
98
99 if (params.isMember(jss::marker))
100 {
101 if (!params[jss::marker].isString())
102 return RPC::expected_field_error(jss::marker, "string");
103
104 // Marker is composed of a comma separated index and start hint. The
105 // former will be read as hex, and the latter using boost lexical cast.
106 std::stringstream marker(params[jss::marker].asString());
107 std::string value;
108 if (!std::getline(marker, value, ','))
110
111 if (!startAfter.parseHex(value))
113
114 if (!std::getline(marker, value, ','))
116
117 try
118 {
119 startHint = boost::lexical_cast<std::uint64_t>(value);
120 }
121 catch (boost::bad_lexical_cast&)
122 {
124 }
125
126 // We then must check if the object pointed to by the marker is actually
127 // owned by the account in the request.
128 auto const sle = ledger->read({ltANY, startAfter});
129
130 if (!sle)
132
133 if (!RPC::isRelatedToAccount(*ledger, sle, accountID))
135 }
136
137 auto count = 0;
138 std::optional<uint256> marker = {};
139 std::uint64_t nextHint = 0;
140 if (!forEachItemAfter(
141 *ledger,
142 accountID,
143 startAfter,
144 startHint,
145 limit + 1,
146 [&visitData, &accountID, &count, &limit, &marker, &nextHint](
147 std::shared_ptr<SLE const> const& sleCur) {
148 if (!sleCur)
149 {
150 // LCOV_EXCL_START
151 UNREACHABLE("ripple::doAccountChannels : null SLE");
152 return false;
153 // LCOV_EXCL_STOP
154 }
155
156 if (++count == limit)
157 {
158 marker = sleCur->key();
159 nextHint = RPC::getStartHint(sleCur, visitData.accountID);
160 }
161
162 if (count <= limit && sleCur->getType() == ltPAYCHAN &&
163 (*sleCur)[sfAccount] == accountID &&
164 (!visitData.raDstAccount ||
165 *visitData.raDstAccount == (*sleCur)[sfDestination]))
166 {
167 visitData.items.emplace_back(sleCur);
168 }
169
170 return true;
171 }))
172 {
174 }
175
176 // Both conditions need to be checked because marker is set on the limit-th
177 // item, but if there is no item on the limit + 1 iteration, then there is
178 // no need to return a marker.
179 if (count == limit + 1 && marker)
180 {
181 result[jss::limit] = limit;
182 result[jss::marker] =
183 to_string(*marker) + "," + std::to_string(nextHint);
184 }
185
186 result[jss::account] = toBase58(accountID);
187
188 for (auto const& item : visitData.items)
189 addChannel(jsonChannels, *item);
190
192 result[jss::channels] = std::move(jsonChannels);
193 return result;
194}
195
196} // 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)