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/base_uint.h>
7#include <xrpl/basics/strHex.h>
8#include <xrpl/beast/utility/Zero.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/core/ServiceRegistry.h>
11#include <xrpl/json/json_value.h>
12#include <xrpl/ledger/ReadView.h>
13#include <xrpl/ledger/helpers/DirectoryHelpers.h>
14#include <xrpl/protocol/AccountID.h>
15#include <xrpl/protocol/ErrorCodes.h>
16#include <xrpl/protocol/Indexes.h>
17#include <xrpl/protocol/LedgerFormats.h>
18#include <xrpl/protocol/PublicKey.h>
19#include <xrpl/protocol/RPCErr.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/jss.h>
22#include <xrpl/protocol/tokens.h>
23#include <xrpl/resource/Fees.h>
24
25#include <boost/lexical_cast.hpp>
26#include <boost/lexical_cast/bad_lexical_cast.hpp>
27
28#include <cstdint>
29#include <memory>
30#include <optional>
31#include <sstream>
32#include <string>
33#include <utility>
34#include <vector>
35
36namespace xrpl {
37
38void
39addChannel(json::Value& jsonLines, SLE const& line)
40{
42 jDst[jss::channel_id] = to_string(line.key());
43 jDst[jss::account] = to_string(line[sfAccount]);
44 jDst[jss::destination_account] = to_string(line[sfDestination]);
45 jDst[jss::amount] = line[sfAmount].getText();
46 jDst[jss::balance] = line[sfBalance].getText();
47 if (publicKeyType(line[sfPublicKey]))
48 {
49 PublicKey const pk(line[sfPublicKey]);
50 jDst[jss::public_key] = toBase58(TokenType::AccountPublic, pk);
51 jDst[jss::public_key_hex] = strHex(pk);
52 }
53 jDst[jss::settle_delay] = line[sfSettleDelay];
54 if (auto const& v = line[~sfExpiration])
55 jDst[jss::expiration] = *v;
56 if (auto const& v = line[~sfCancelAfter])
57 jDst[jss::cancel_after] = *v;
58 if (auto const& v = line[~sfSourceTag])
59 jDst[jss::source_tag] = *v;
60 if (auto const& v = line[~sfDestinationTag])
61 jDst[jss::destination_tag] = *v;
62}
63
64// {
65// account: <account>
66// ledger_hash : <ledger>
67// ledger_index : <ledger_index>
68// limit: integer // optional
69// marker: opaque // optional, resume previous query
70// }
73{
74 auto const& params(context.params);
75 if (!params.isMember(jss::account))
76 return RPC::missingFieldError(jss::account);
77
78 if (!params[jss::account].isString())
79 return RPC::invalidFieldError(jss::account);
80
82 auto result = RPC::lookupLedger(ledger, context);
83 if (!ledger)
84 return result;
85
86 auto id = parseBase58<AccountID>(params[jss::account].asString());
87 if (!id)
88 {
90 }
91 AccountID const accountID{id.value()};
92
93 if (!ledger->exists(keylet::account(accountID)))
95
96 std::string strDst;
97 if (params.isMember(jss::destination_account))
98 {
99 if (!params[jss::destination_account].isString())
100 return RPC::invalidFieldError(jss::destination_account);
101 strDst = params[jss::destination_account].asString();
102 }
103
104 auto const raDstAccount = [&]() -> std::optional<AccountID> {
105 return strDst.empty() ? std::nullopt : parseBase58<AccountID>(strDst);
106 }();
107 if (!strDst.empty() && !raDstAccount)
109
110 unsigned int limit = 0;
111 if (auto err = readLimitField(limit, RPC::Tuning::kAccountChannels, context))
112 return *err;
113
115 struct VisitData
116 {
118 AccountID const& accountID;
119 std::optional<AccountID> const& raDstAccount;
120 };
121 VisitData visitData = {.items = {}, .accountID = accountID, .raDstAccount = raDstAccount};
122 visitData.items.reserve(limit);
123 uint256 startAfter = beast::kZero;
124 std::uint64_t startHint = 0;
125
126 if (params.isMember(jss::marker))
127 {
128 if (!params[jss::marker].isString())
129 return RPC::expectedFieldError(jss::marker, "string");
130
131 // Marker is composed of a comma separated index and start hint. The
132 // former will be read as hex, and the latter using boost lexical cast.
133 std::stringstream marker(params[jss::marker].asString());
134 std::string value;
135 if (!std::getline(marker, value, ','))
137
138 if (!startAfter.parseHex(value))
140
141 if (!std::getline(marker, value, ','))
143
144 try
145 {
146 startHint = boost::lexical_cast<std::uint64_t>(value);
147 }
148 catch (boost::bad_lexical_cast&)
149 {
151 }
152
153 // We then must check if the object pointed to by the marker is actually
154 // owned by the account in the request.
155 auto const sle = ledger->read({ltANY, startAfter});
156
157 if (!sle)
159
160 if (!RPC::isRelatedToAccount(*ledger, sle, accountID))
162 }
163
164 auto count = 0;
165 std::optional<uint256> marker = {};
166 std::uint64_t nextHint = 0;
167 if (!forEachItemAfter(
168 *ledger,
169 accountID,
170 startAfter,
171 startHint,
172 limit + 1,
173 [&visitData, &accountID, &count, &limit, &marker, &nextHint](SLE::const_ref sleCur) {
174 if (!sleCur)
175 {
176 // LCOV_EXCL_START
177 UNREACHABLE("xrpl::doAccountChannels : null SLE");
178 return false;
179 // LCOV_EXCL_STOP
180 }
181
182 if (++count == limit)
183 {
184 marker = sleCur->key();
185 nextHint = RPC::getStartHint(sleCur, visitData.accountID);
186 }
187
188 if (count <= limit && sleCur->getType() == ltPAYCHAN &&
189 (*sleCur)[sfAccount] == accountID &&
190 (!visitData.raDstAccount ||
191 *visitData.raDstAccount == (*sleCur)[sfDestination]))
192 {
193 visitData.items.emplace_back(sleCur);
194 }
195
196 return true;
197 }))
198 {
200 }
201
202 // Both conditions need to be checked because marker is set on the limit-th
203 // item, but if there is no item on the limit + 1 iteration, then there is
204 // no need to return a marker.
205 if (count == limit + 1 && marker)
206 {
207 result[jss::limit] = limit;
208 result[jss::marker] = to_string(*marker) + "," + std::to_string(nextHint);
209 }
210
211 result[jss::account] = toBase58(accountID);
212
213 for (auto const& item : visitData.items)
214 addChannel(jsonChannels, *item);
215
217 result[jss::channels] = std::move(jsonChannels);
218 return result;
219}
220
221} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
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:507
A public key.
Definition PublicKey.h:42
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)
@ Array
array value (ordered list)
Definition json_value.h:25
@ Object
object value (collection of name/value pairs).
Definition json_value.h:26
static constexpr LimitRange kAccountChannels
Limits for the account_channels command.
bool isRelatedToAccount(ReadView const &ledger, SLE::const_ref sle, AccountID const &accountID)
Tests if a ledger entry (SLE) is owned by the specified account.
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.
std::uint64_t getStartHint(SLE::const_ref sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
json::Value invalidFieldError(std::string const &name)
Definition ErrorCodes.h:273
json::Value missingFieldError(std::string const &name)
Definition ErrorCodes.h:231
json::Value expectedFieldError(std::string const &name, std::string const &type)
Definition ErrorCodes.h:297
Charge const kFeeMediumBurdenRpc
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcActNotFound
Definition ErrorCodes.h:52
@ RpcActMalformed
Definition ErrorCodes.h:72
@ RpcInvalidParams
Definition ErrorCodes.h:66
json::Value doAccountChannels(RPC::JsonContext &context)
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:10
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:93
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
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
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
@ ltANY
A special type, matching any ledger entry type.
BaseUInt< 256 > uint256
Definition base_uint.h:562
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:22
json::Value params
Definition Context.h:43
T to_string(T... args)