rippled
Loading...
Searching...
No Matches
AccountLines.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2014 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/main/Application.h>
21#include <xrpld/app/paths/TrustLine.h>
22#include <xrpld/ledger/ReadView.h>
23#include <xrpld/rpc/Context.h>
24#include <xrpld/rpc/detail/RPCHelpers.h>
25#include <xrpld/rpc/detail/Tuning.h>
26#include <xrpl/protocol/ErrorCodes.h>
27#include <xrpl/protocol/RPCErr.h>
28#include <xrpl/protocol/jss.h>
29#include <xrpl/resource/Fees.h>
30
31namespace ripple {
32
33void
34addLine(Json::Value& jsonLines, RPCTrustLine const& line)
35{
36 STAmount const& saBalance(line.getBalance());
37 STAmount const& saLimit(line.getLimit());
38 STAmount const& saLimitPeer(line.getLimitPeer());
39 Json::Value& jPeer(jsonLines.append(Json::objectValue));
40
41 jPeer[jss::account] = to_string(line.getAccountIDPeer());
42 // Amount reported is positive if current account holds other
43 // account's IOUs.
44 //
45 // Amount reported is negative if other account holds current
46 // account's IOUs.
47 jPeer[jss::balance] = saBalance.getText();
48 jPeer[jss::currency] = to_string(saBalance.issue().currency);
49 jPeer[jss::limit] = saLimit.getText();
50 jPeer[jss::limit_peer] = saLimitPeer.getText();
51 jPeer[jss::quality_in] = line.getQualityIn().value;
52 jPeer[jss::quality_out] = line.getQualityOut().value;
53 if (line.getAuth())
54 jPeer[jss::authorized] = true;
55 if (line.getAuthPeer())
56 jPeer[jss::peer_authorized] = true;
57 if (line.getNoRipple() || !line.getDefaultRipple())
58 jPeer[jss::no_ripple] = line.getNoRipple();
59 if (line.getNoRipplePeer() || !line.getDefaultRipple())
60 jPeer[jss::no_ripple_peer] = line.getNoRipplePeer();
61 if (line.getFreeze())
62 jPeer[jss::freeze] = true;
63 if (line.getFreezePeer())
64 jPeer[jss::freeze_peer] = true;
65}
66
67// {
68// account: <account>
69// ledger_hash : <ledger>
70// ledger_index : <ledger_index>
71// limit: integer // optional
72// marker: opaque // optional, resume previous query
73// ignore_default: bool // do not return lines in default state (on
74// this account's side)
75// }
78{
79 auto const& params(context.params);
80 if (!params.isMember(jss::account))
81 return RPC::missing_field_error(jss::account);
82
83 if (!params[jss::account].isString())
84 return RPC::invalid_field_error(jss::account);
85
87 auto result = RPC::lookupLedger(ledger, context);
88 if (!ledger)
89 return result;
90
91 auto id = parseBase58<AccountID>(params[jss::account].asString());
92 if (!id)
93 {
95 return result;
96 }
97 auto const accountID{std::move(id.value())};
98
99 if (!ledger->exists(keylet::account(accountID)))
101
102 std::string strPeer;
103 if (params.isMember(jss::peer))
104 strPeer = params[jss::peer].asString();
105
106 auto const raPeerAccount = [&]() -> std::optional<AccountID> {
107 return strPeer.empty() ? std::nullopt : parseBase58<AccountID>(strPeer);
108 }();
109 if (!strPeer.empty() && !raPeerAccount)
110 {
112 return result;
113 }
114
115 unsigned int limit;
116 if (auto err = readLimitField(limit, RPC::Tuning::accountLines, context))
117 return *err;
118
119 if (limit == 0)
121
122 // this flag allows the requester to ask incoming trustlines in default
123 // state be omitted
124 bool ignoreDefault = params.isMember(jss::ignore_default) &&
125 params[jss::ignore_default].asBool();
126
127 Json::Value& jsonLines(result[jss::lines] = Json::arrayValue);
128 struct VisitData
129 {
131 AccountID const& accountID;
132 std::optional<AccountID> const& raPeerAccount;
133 bool ignoreDefault;
134 uint32_t foundCount;
135 };
136 VisitData visitData = {{}, accountID, raPeerAccount, ignoreDefault, 0};
137 uint256 startAfter = beast::zero;
138 std::uint64_t startHint = 0;
139
140 if (params.isMember(jss::marker))
141 {
142 if (!params[jss::marker].isString())
143 return RPC::expected_field_error(jss::marker, "string");
144
145 // Marker is composed of a comma separated index and start hint. The
146 // former will be read as hex, and the latter using boost lexical cast.
147 std::stringstream marker(params[jss::marker].asString());
148 std::string value;
149 if (!std::getline(marker, value, ','))
151
152 if (!startAfter.parseHex(value))
154
155 if (!std::getline(marker, value, ','))
157
158 try
159 {
160 startHint = boost::lexical_cast<std::uint64_t>(value);
161 }
162 catch (boost::bad_lexical_cast&)
163 {
165 }
166
167 // We then must check if the object pointed to by the marker is actually
168 // owned by the account in the request.
169 auto const sle = ledger->read({ltANY, startAfter});
170
171 if (!sle)
173
174 if (!RPC::isRelatedToAccount(*ledger, sle, accountID))
176 }
177
178 auto count = 0;
179 std::optional<uint256> marker = {};
180 std::uint64_t nextHint = 0;
181 {
182 if (!forEachItemAfter(
183 *ledger,
184 accountID,
185 startAfter,
186 startHint,
187 limit + 1,
188 [&visitData, &count, &marker, &limit, &nextHint](
189 std::shared_ptr<SLE const> const& sleCur) {
190 if (!sleCur)
191 {
192 UNREACHABLE("ripple::doAccountLines : null SLE");
193 return false;
194 }
195
196 if (++count == limit)
197 {
198 marker = sleCur->key();
199 nextHint =
200 RPC::getStartHint(sleCur, visitData.accountID);
201 }
202
203 if (sleCur->getType() != ltRIPPLE_STATE)
204 return true;
205
206 bool ignore = false;
207 if (visitData.ignoreDefault)
208 {
209 if (sleCur->getFieldAmount(sfLowLimit).getIssuer() ==
210 visitData.accountID)
211 ignore =
212 !(sleCur->getFieldU32(sfFlags) & lsfLowReserve);
213 else
214 ignore = !(
215 sleCur->getFieldU32(sfFlags) & lsfHighReserve);
216 }
217
218 if (!ignore && count <= limit)
219 {
220 auto const line =
221 RPCTrustLine::makeItem(visitData.accountID, sleCur);
222
223 if (line &&
224 (!visitData.raPeerAccount ||
225 *visitData.raPeerAccount ==
226 line->getAccountIDPeer()))
227 {
228 visitData.items.emplace_back(*line);
229 }
230 }
231
232 return true;
233 }))
234 {
236 }
237 }
238
239 // Both conditions need to be checked because marker is set on the limit-th
240 // item, but if there is no item on the limit + 1 iteration, then there is
241 // no need to return a marker.
242 if (count == limit + 1 && marker)
243 {
244 result[jss::limit] = limit;
245 result[jss::marker] =
246 to_string(*marker) + "," + std::to_string(nextHint);
247 }
248
249 result[jss::account] = toBase58(accountID);
250
251 for (auto const& item : visitData.items)
252 addLine(jsonLines, item);
253
255 return result;
256}
257
258} // namespace ripple
Represents a JSON value.
Definition: json_value.h:147
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:891
Currency currency
Definition: Issue.h:38
static std::optional< RPCTrustLine > makeItem(AccountID const &accountID, std::shared_ptr< SLE const > const &sle)
Definition: TrustLine.cpp:110
std::string getText() const override
Definition: STAmount.cpp:515
Issue const & issue() const
Definition: STAmount.h:487
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:502
T empty(T... args)
T getline(T... args)
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
static LimitRange constexpr accountLines
Limits for the account_lines command.
Json::Value invalid_field_error(std::string const &name)
Definition: ErrorCodes.h:312
bool isRelatedToAccount(ReadView const &ledger, std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Tests if a SLE is owned by accountID.
Definition: RPCHelpers.cpp:116
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition: ErrorCodes.h:220
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:336
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.
Definition: RPCHelpers.cpp:623
std::uint64_t getStartHint(std::shared_ptr< SLE const > const &sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
Definition: RPCHelpers.cpp:99
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:270
Charge const feeMediumBurdenRPC
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:160
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:106
void addLine(Json::Value &jsonLines, RPCTrustLine const &line)
@ rpcACT_NOT_FOUND
Definition: ErrorCodes.h:70
@ rpcACT_MALFORMED
Definition: ErrorCodes.h:90
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
Json::Value doAccountLines(RPC::JsonContext &context)
@ lsfHighReserve
@ lsfLowReserve
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
@ ltANY
A special type, matching any ledger entry type.
Definition: LedgerFormats.h:78
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:495
Resource::Charge & loadType
Definition: Context.h:43
Json::Value params
Definition: Context.h:64
T to_string(T... args)