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