xrpld
Loading...
Searching...
No Matches
AccountLines.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/TrustLine.h>
5#include <xrpld/rpc/detail/Tuning.h>
6
7#include <xrpl/basics/StringUtilities.h>
8#include <xrpl/basics/base_uint.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/RPCErr.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/STAmount.h>
22#include <xrpl/protocol/UintTypes.h>
23#include <xrpl/protocol/jss.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 <vector>
32
33namespace xrpl {
34
35void
36addLine(json::Value& jsonLines, RPCTrustLine const& line)
37{
38 STAmount const& saBalance(line.getBalance());
39 STAmount const& saLimit(line.getLimit());
40 STAmount const& saLimitPeer(line.getLimitPeer());
42
43 jPeer[jss::account] = to_string(line.getAccountIDPeer());
44 // Amount reported is positive if current account holds other
45 // account's IOUs.
46 //
47 // Amount reported is negative if other account holds current
48 // account's IOUs.
49 jPeer[jss::balance] = saBalance.getText();
50 jPeer[jss::currency] = to_string(saBalance.get<Issue>().currency);
51 jPeer[jss::limit] = saLimit.getText();
52 jPeer[jss::limit_peer] = saLimitPeer.getText();
53 jPeer[jss::quality_in] = line.getQualityIn().value;
54 jPeer[jss::quality_out] = line.getQualityOut().value;
55 if (line.getAuth())
56 jPeer[jss::authorized] = true;
57 if (line.getAuthPeer())
58 jPeer[jss::peer_authorized] = true;
59 if (line.getNoRipple())
60 jPeer[jss::no_ripple] = true;
61 if (line.getNoRipplePeer())
62 jPeer[jss::no_ripple_peer] = true;
63 if (line.getFreeze())
64 jPeer[jss::freeze] = true;
65 if (line.getFreezePeer())
66 jPeer[jss::freeze_peer] = true;
67 if (line.getDeepFreeze())
68 jPeer[jss::deep_freeze] = true;
69 if (line.getDeepFreezePeer())
70 jPeer[jss::deep_freeze_peer] = true;
71}
72
73// {
74// account: <account>
75// ledger_hash : <ledger>
76// ledger_index : <ledger_index>
77// limit: integer // optional
78// marker: opaque // optional, resume previous query
79// ignore_default: bool // do not return lines in default state (on
80// this account's side)
81// }
84{
85 auto const& params(context.params);
86 if (!params.isMember(jss::account))
87 return rpc::missingFieldError(jss::account);
88
89 if (!params[jss::account].isString())
90 return rpc::invalidFieldError(jss::account);
91
93 auto result = rpc::lookupLedger(ledger, context);
94 if (!ledger)
95 return result;
96
97 auto id = parseBase58<AccountID>(params[jss::account].asString());
98 if (!id)
99 {
101 return result;
102 }
103 auto const accountID{id.value()};
104
105 if (!ledger->exists(keylet::account(accountID)))
106 return rpcError(RpcActNotFound);
107
108 std::string strPeer;
109 if (params.isMember(jss::peer))
110 {
111 if (!params[jss::peer].isString())
112 return rpc::invalidFieldError(jss::peer);
113
114 strPeer = params[jss::peer].asString();
115 }
116
117 auto const raPeerAccount = [&]() -> std::optional<AccountID> {
118 return strPeer.empty() ? std::nullopt : parseBase58<AccountID>(strPeer);
119 }();
120 if (!strPeer.empty() && !raPeerAccount)
121 {
123 return result;
124 }
125
126 unsigned int limit = 0;
127 if (auto err = readLimitField(limit, rpc::tuning::kAccountLines, context))
128 return *err;
129
130 // this flag allows the requester to ask incoming trustlines in default
131 // state be omitted
132 bool const ignoreDefault =
133 params.isMember(jss::ignore_default) && params[jss::ignore_default].asBool();
134
135 json::Value& jsonLines(result[jss::lines] = json::ValueType::Array);
136 struct VisitData
137 {
139 AccountID const& accountID;
140 std::optional<AccountID> const& raPeerAccount;
141 bool ignoreDefault;
142 uint32_t foundCount;
143 };
144 VisitData visitData = {
145 .items = {},
146 .accountID = accountID,
147 .raPeerAccount = raPeerAccount,
148 .ignoreDefault = ignoreDefault,
149 .foundCount = 0};
150 uint256 startAfter = beast::kZero;
151 std::uint64_t startHint = 0;
152
153 if (params.isMember(jss::marker))
154 {
155 if (!params[jss::marker].isString())
156 return rpc::expectedFieldError(jss::marker, "string");
157
158 // Marker is composed of a comma separated index and start hint. The
159 // former will be read as hex, and the latter as a decimal integer.
160 std::stringstream marker(params[jss::marker].asString());
161 std::string value;
162 if (!std::getline(marker, value, ','))
164
165 if (!startAfter.parseHex(value))
167
168 if (!std::getline(marker, value, ','))
170
171 auto const hint = toUInt64(value);
172 if (!hint.has_value())
174 startHint = *hint;
175
176 // We then must check if the object pointed to by the marker is actually
177 // owned by the account in the request.
178 auto const sle = ledger->read({ltANY, startAfter});
179
180 if (!sle)
182
183 if (!rpc::isRelatedToAccount(*ledger, sle, accountID))
185 }
186
187 auto count = 0;
188 std::optional<uint256> marker = {};
189 std::uint64_t nextHint = 0;
190 {
191 if (!forEachItemAfter(
192 *ledger,
193 accountID,
194 startAfter,
195 startHint,
196 limit + 1,
197 [&visitData, &count, &marker, &limit, &nextHint](SLE::const_ref sleCur) {
198 if (!sleCur)
199 {
200 // LCOV_EXCL_START
201 UNREACHABLE("xrpl::doAccountLines : null SLE");
202 return false;
203 // LCOV_EXCL_STOP
204 }
205
206 if (++count == limit)
207 {
208 marker = sleCur->key();
209 nextHint = rpc::getStartHint(sleCur, visitData.accountID);
210 }
211
212 if (sleCur->getType() != ltRIPPLE_STATE)
213 return true;
214
215 bool ignore = false;
216 if (visitData.ignoreDefault)
217 {
218 if (sleCur->getFieldAmount(sfLowLimit).getIssuer() == visitData.accountID)
219 {
220 ignore = !sleCur->isFlag(lsfLowReserve);
221 }
222 else
223 {
224 ignore = !sleCur->isFlag(lsfHighReserve);
225 }
226 }
227
228 if (!ignore && count <= limit)
229 {
230 auto const line = RPCTrustLine::makeItem(visitData.accountID, sleCur);
231
232 if (line &&
233 (!visitData.raPeerAccount ||
234 *visitData.raPeerAccount == line->getAccountIDPeer()))
235 {
236 visitData.items.emplace_back(*line);
237 }
238 }
239
240 return true;
241 }))
242 {
244 }
245 }
246
247 // Both conditions need to be checked because marker is set on the limit-th
248 // item, but if there is no item on the limit + 1 iteration, then there is
249 // no need to return a marker.
250 if (count == limit + 1 && marker)
251 {
252 result[jss::limit] = limit;
253 result[jss::marker] = to_string(*marker) + "," + std::to_string(nextHint);
254 }
255
256 result[jss::account] = toBase58(accountID);
257
258 for (auto const& item : visitData.items)
259 addLine(jsonLines, item);
260
262 return result;
263}
264
265} // 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 currency issued by an account.
Definition Issue.h:18
Currency currency
Definition Issue.h:20
Rate const & getQualityIn() const
Definition TrustLine.h:213
static std::optional< RPCTrustLine > makeItem(AccountID const &accountID, SLE::const_ref sle)
Definition TrustLine.cpp:87
Rate const & getQualityOut() const
Definition TrustLine.h:219
constexpr TIss const & get() const
std::string getText() const override
Definition STAmount.cpp:646
std::shared_ptr< STLedgerEntry const > const & const_ref
AccountID const & getAccountIDPeer() const
Definition TrustLine.h:74
bool getNoRipplePeer() const
Definition TrustLine.h:99
bool getAuth() const
Definition TrustLine.h:81
STAmount const & getLimit() const
Definition TrustLine.h:159
bool getDeepFreeze() const
Have we set the deep freeze flag on our peer.
Definition TrustLine.h:129
bool getFreezePeer() const
Has the peer set the freeze flag on us.
Definition TrustLine.h:138
bool getDeepFreezePeer() const
Has the peer set the deep freeze flag on us.
Definition TrustLine.h:147
STAmount const & getLimitPeer() const
Definition TrustLine.h:165
bool getFreeze() const
Have we set the freeze flag on our peer.
Definition TrustLine.h:120
bool getAuthPeer() const
Definition TrustLine.h:87
STAmount const & getBalance() const
Definition TrustLine.h:153
bool getNoRipple() const
Definition TrustLine.h:93
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 kAccountLines
Limits for the account_lines 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.
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
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
@ 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 toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
json::Value doAccountLines(rpc::JsonContext &context)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
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
void addLine(json::Value &jsonLines, RPCTrustLine const &line)
@ 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.
std::uint32_t value
Definition Rate.h:22
resource::Charge & loadType
Definition Context.h:30
json::Value params
Definition Context.h:51
T to_string(T... args)