xrpld
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/basics/StringUtilities.h>
7#include <xrpl/basics/base_uint.h>
8#include <xrpl/basics/strHex.h>
9#include <xrpl/beast/utility/Zero.h>
10#include <xrpl/beast/utility/instrumentation.h>
11#include <xrpl/core/ServiceRegistry.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/ledger/ReadView.h>
14#include <xrpl/ledger/helpers/DirectoryHelpers.h>
15#include <xrpl/protocol/AccountID.h>
16#include <xrpl/protocol/ErrorCodes.h>
17#include <xrpl/protocol/Indexes.h>
18#include <xrpl/protocol/LedgerFormats.h>
19#include <xrpl/protocol/PublicKey.h>
20#include <xrpl/protocol/RPCErr.h>
21#include <xrpl/protocol/SField.h>
22#include <xrpl/protocol/jss.h>
23#include <xrpl/protocol/tokens.h>
24#include <xrpl/resource/Fees.h>
25
26#include <cstdint>
27#include <memory>
28#include <optional>
29#include <sstream>
30#include <string>
31#include <utility>
32#include <vector>
33
34namespace xrpl {
35
36void
37addChannel(json::Value& jsonLines, SLE const& line)
38{
40 jDst[jss::channel_id] = to_string(line.key());
41 jDst[jss::account] = to_string(line[sfAccount]);
42 jDst[jss::destination_account] = to_string(line[sfDestination]);
43 jDst[jss::amount] = line[sfAmount].getText();
44 jDst[jss::balance] = line[sfBalance].getText();
45 if (publicKeyType(line[sfPublicKey]))
46 {
47 PublicKey const pk(line[sfPublicKey]);
48 jDst[jss::public_key] = toBase58(TokenType::AccountPublic, pk);
49 jDst[jss::public_key_hex] = strHex(pk);
50 }
51 jDst[jss::settle_delay] = line[sfSettleDelay];
52 if (auto const& v = line[~sfExpiration])
53 jDst[jss::expiration] = *v;
54 if (auto const& v = line[~sfCancelAfter])
55 jDst[jss::cancel_after] = *v;
56 if (auto const& v = line[~sfSourceTag])
57 jDst[jss::source_tag] = *v;
58 if (auto const& v = line[~sfDestinationTag])
59 jDst[jss::destination_tag] = *v;
60}
61
62// {
63// account: <account>
64// ledger_hash : <ledger>
65// ledger_index : <ledger_index>
66// limit: integer // optional
67// marker: opaque // optional, resume previous query
68// }
71{
72 auto const& params(context.params);
73 if (!params.isMember(jss::account))
74 return rpc::missingFieldError(jss::account);
75
76 if (!params[jss::account].isString())
77 return rpc::invalidFieldError(jss::account);
78
80 auto result = rpc::lookupLedger(ledger, context);
81 if (!ledger)
82 return result;
83
84 auto id = parseBase58<AccountID>(params[jss::account].asString());
85 if (!id)
86 {
88 }
89 AccountID const accountID{id.value()};
90
91 if (!ledger->exists(keylet::account(accountID)))
93
94 std::string strDst;
95 if (params.isMember(jss::destination_account))
96 {
97 if (!params[jss::destination_account].isString())
98 return rpc::invalidFieldError(jss::destination_account);
99 strDst = params[jss::destination_account].asString();
100 }
101
102 auto const raDstAccount = [&]() -> std::optional<AccountID> {
103 return strDst.empty() ? std::nullopt : parseBase58<AccountID>(strDst);
104 }();
105 if (!strDst.empty() && !raDstAccount)
107
108 unsigned int limit = 0;
109 if (auto err = readLimitField(limit, rpc::tuning::kAccountChannels, context))
110 return *err;
111
113 struct VisitData
114 {
116 AccountID const& accountID;
117 std::optional<AccountID> const& raDstAccount;
118 };
119 VisitData visitData = {.items = {}, .accountID = accountID, .raDstAccount = raDstAccount};
120 visitData.items.reserve(limit);
121 uint256 startAfter = beast::kZero;
122 std::uint64_t startHint = 0;
123
124 if (params.isMember(jss::marker))
125 {
126 if (!params[jss::marker].isString())
127 return rpc::expectedFieldError(jss::marker, "string");
128
129 // Marker is composed of a comma separated index and start hint. The
130 // former will be read as hex, and the latter as a decimal integer.
131 std::stringstream marker(params[jss::marker].asString());
132 std::string value;
133 if (!std::getline(marker, value, ','))
135
136 if (!startAfter.parseHex(value))
138
139 if (!std::getline(marker, value, ','))
141
142 auto const hint = toUInt64(value);
143 if (!hint.has_value())
145 startHint = *hint;
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)
153
154 if (!rpc::isRelatedToAccount(*ledger, sle, accountID))
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](SLE::const_ref sleCur) {
168 if (!sleCur)
169 {
170 // LCOV_EXCL_START
171 UNREACHABLE("xrpl::doAccountChannels : null SLE");
172 return false;
173 // LCOV_EXCL_STOP
174 }
175
176 if (++count == limit)
177 {
178 marker = sleCur->key();
179 nextHint = rpc::getStartHint(sleCur, visitData.accountID);
180 }
181
182 if (count <= limit && sleCur->getType() == ltPAYCHAN &&
183 (*sleCur)[sfAccount] == accountID &&
184 (!visitData.raDstAccount ||
185 *visitData.raDstAccount == (*sleCur)[sfDestination]))
186 {
187 visitData.items.emplace_back(sleCur);
188 }
189
190 return true;
191 }))
192 {
194 }
195
196 // Both conditions need to be checked because marker is set on the limit-th
197 // item, but if there is no item on the limit + 1 iteration, then there is
198 // no need to return a marker.
199 if (count == limit + 1 && marker)
200 {
201 result[jss::limit] = limit;
202 result[jss::marker] = to_string(*marker) + "," + std::to_string(nextHint);
203 }
204
205 result[jss::account] = 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 xrpl
Represents a JSON value.
Definition json_value.h:117
Value & append(Value const &value)
Append value to array at the end.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:525
A public key.
Definition PublicKey.h:53
uint256 const & key() const
Returns the 'key' (or 'index') of this item.
std::shared_ptr< STLedgerEntry const > const & const_ref
std::string getText() const override
T empty(T... args)
T getline(T... args)
constexpr Zero kZero
Definition Zero.h:30
@ Array
array value (ordered list)
Definition json_value.h:28
@ Object
object value (collection of name/value pairs).
Definition json_value.h:29
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:198
Charge const kFeeMediumBurdenRpc
static constexpr LimitRange kAccountChannels
Limits for the account_channels command.
json::Value expectedFieldError(std::string const &name, std::string const &type)
Definition ErrorCodes.h:309
std::uint64_t getStartHint(SLE::const_ref sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
bool isRelatedToAccount(ReadView const &ledger, SLE::const_ref sle, AccountID const &accountID)
Tests if a ledger entry (SLE) is owned by the specified account.
json::Value invalidFieldError(std::string const &name)
Definition ErrorCodes.h:285
json::Value missingFieldError(std::string const &name)
Definition ErrorCodes.h:243
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext const &context, json::Value &result)
Looks up a ledger from a request and fills a json::Value with ledger data.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
json::Value doAccountChannels(rpc::JsonContext &context)
@ RpcActNotFound
Definition ErrorCodes.h:53
@ RpcActMalformed
Definition ErrorCodes.h:73
@ RpcInvalidParams
Definition ErrorCodes.h:67
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
STLedgerEntry SLE
void addChannel(json::Value &jsonLines, SLE const &line)
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
std::optional< std::uint64_t > toUInt64(std::string const &s)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
@ ltANY
A special type, matching any ledger entry type.
BaseUInt< 256 > uint256
Definition base_uint.h:580
bool forEachItemAfter(ReadView const &view, Keylet const &root, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(SLE::const_ref)> const &f)
Iterate all items after an item in the given directory.
resource::Charge & loadType
Definition Context.h:30
json::Value params
Definition Context.h:51
T to_string(T... args)