xrpld
Loading...
Searching...
No Matches
AccountLines.cpp
1#include <xrpld/rpc/handlers/account/AccountLines.h>
2
3#include <xrpld/rpc/Context.h>
4#include <xrpld/rpc/detail/RPCHelpers.h>
5#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
6#include <xrpld/rpc/detail/TrustLine.h>
7#include <xrpld/rpc/detail/Tuning.h>
8
9#include <xrpl/basics/base_uint.h>
10#include <xrpl/beast/utility/Zero.h>
11#include <xrpl/beast/utility/instrumentation.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/SField.h>
20#include <xrpl/protocol/STAmount.h>
21#include <xrpl/protocol/STLedgerEntry.h>
22#include <xrpl/protocol/UintTypes.h>
23#include <xrpl/protocol/jss.h>
24#include <xrpl/resource/Fees.h>
25
26#include <rpcspec/AccountTypes.hpp>
27#include <rpcspec/Errors.hpp>
28#include <rpcspec/handlers/account_lines/Types.hpp>
29
30#include <cstdint>
31#include <expected>
32#include <optional>
33#include <string>
34#include <utility>
35
36namespace xrpl::rpc {
37
38static_assert(::rpc::spec::handlers::account_lines::kLimitMin == tuning::kAccountLines.rmin);
39static_assert(
40 ::rpc::spec::handlers::account_lines::kLimitDefault == tuning::kAccountLines.rDefault);
41static_assert(::rpc::spec::handlers::account_lines::kLimitMax == tuning::kAccountLines.rmax);
42
43namespace {
44
45void
46appendLineJson(RPCTrustLine const& line, json::Value& lines)
47{
48 STAmount const& saBalance(line.getBalance());
49 STAmount const& saLimit(line.getLimit());
50 STAmount const& saLimitPeer(line.getLimitPeer());
51 json::Value& jPeer(lines.append(json::ValueType::Object));
52
53 jPeer[jss::account] = to_string(line.getAccountIDPeer());
54 // Amount reported is positive if current account holds other
55 // account's IOUs.
56 //
57 // Amount reported is negative if other account holds current
58 // account's IOUs.
59 jPeer[jss::balance] = saBalance.getText();
60 jPeer[jss::currency] = to_string(saBalance.get<Issue>().currency);
61 jPeer[jss::limit] = saLimit.getText();
62 jPeer[jss::limit_peer] = saLimitPeer.getText();
63 jPeer[jss::quality_in] = line.getQualityIn().value;
64 jPeer[jss::quality_out] = line.getQualityOut().value;
65 if (line.getAuth())
66 jPeer[jss::authorized] = true;
67 if (line.getAuthPeer())
68 jPeer[jss::peer_authorized] = true;
69 if (line.getNoRipple())
70 jPeer[jss::no_ripple] = true;
71 if (line.getNoRipplePeer())
72 jPeer[jss::no_ripple_peer] = true;
73 if (line.getFreeze())
74 jPeer[jss::freeze] = true;
75 if (line.getFreezePeer())
76 jPeer[jss::freeze_peer] = true;
77 if (line.getDeepFreeze())
78 jPeer[jss::deep_freeze] = true;
79 if (line.getDeepFreezePeer())
80 jPeer[jss::deep_freeze_peer] = true;
81}
82
83} // namespace
84
88
89std::expected<AccountLinesHandler::Output, ::rpc::Status>
90AccountLinesHandler::process(Input const& input) const
91{
92 auto& context = context_.get();
93
94 Output output;
95 if (auto const status = getLedger(output.ledger, input.ledger, context);
96 status != ::rpc::Status::kOK)
97 return std::unexpected{status};
98 auto const& ledger = *output.ledger;
99
100 if (!input.account.has_value())
101 {
102 output.error = RpcActMalformed;
103 return output;
104 }
105 auto const accountID = *input.account;
106 output.account = accountID;
107
108 if (!ledger.exists(keylet::account(accountID)))
110
111 auto const& peer = input.peer;
112 if (!peer.has_value() && peer.error() == ::rpc::spec::AccountError::Malformed)
113 {
114 output.error = RpcActMalformed;
115 return output;
116 }
117
118 auto const limit = clampLimit(input.limit, tuning::kAccountLines, context.role);
119
120 auto const parsed = input.marker.has_value()
121 ? parseAccountMarker(*input.marker)
123 if (!parsed.has_value())
124 {
125 // LCOV_EXCL_START
126 UNREACHABLE("xrpl::rpc::AccountLinesHandler::process : marker validated by the spec");
128 // LCOV_EXCL_STOP
129 }
130 auto const [startAfter, startHint] = *parsed;
131
132 if (input.marker.has_value())
133 {
134 // We then must check if the object pointed to by the marker is actually
135 // owned by the account in the request.
136 auto const sle = ledger.read({ltANY, startAfter});
137
138 if (!sle)
140
141 if (!isRelatedToAccount(ledger, sle, accountID))
143 }
144
145 bool const ignoreDefault = input.ignoreDefault;
146 std::uint32_t count = 0;
148 std::uint64_t nextHint = 0;
149 if (!forEachItemAfter(
150 ledger,
151 accountID,
152 startAfter,
153 startHint,
154 limit + 1,
155 [&output, &accountID, &peer, ignoreDefault, &count, &limit, &marker, &nextHint](
156 SLE::ConstRef sle) {
157 if (!sle)
158 {
159 // LCOV_EXCL_START
160 UNREACHABLE("xrpl::rpc::AccountLinesHandler::process : null SLE");
161 return false;
162 // LCOV_EXCL_STOP
163 }
164
165 if (++count == limit)
166 {
167 marker = sle->key();
168 nextHint = getStartHint(sle, accountID);
169 }
170
171 if (sle->getType() != ltRIPPLE_STATE)
172 return true;
173
174 bool ignore = false;
175 if (ignoreDefault)
176 {
177 if (sle->getFieldAmount(sfLowLimit).getIssuer() == accountID)
178 {
179 ignore = !sle->isFlag(lsfLowReserve);
180 }
181 else
182 {
183 ignore = !sle->isFlag(lsfHighReserve);
184 }
185 }
186
187 if (!ignore && count <= limit)
188 {
189 auto const line = RPCTrustLine::makeItem(accountID, sle);
190
191 if (line.has_value() &&
192 (!peer.has_value() || *peer == line->getAccountIDPeer()))
193 {
194 output.lines.emplace_back(*line);
195 }
196 }
197
198 return true;
199 }))
200 {
202 }
203
204 // Both conditions need to be checked because marker is set on the limit-th
205 // item, but if there is no item on the limit + 1 iteration, then there is
206 // no need to return a marker.
207 if (count == limit + 1 && marker.has_value())
208 {
209 output.limit = limit;
210 output.marker = marker;
211 output.nextHint = nextHint;
212 }
213
214 context.loadType = resource::kFeeMediumBurdenRpc;
215 return output;
216}
217
218void
220{
221 injectLedgerFields(*output.ledger, context_.get(), value);
222
223 if (output.error.has_value())
224 {
225 injectError(*output.error, value);
226 return;
227 }
228
229 json::Value& lines(value[jss::lines] = json::ValueType::Array);
230 for (auto const& line : output.lines)
231 appendLineJson(line, lines);
232
233 if (output.marker.has_value())
234 {
235 value[jss::limit] = output.limit;
236 value[jss::marker] = to_string(*output.marker) + "," + std::to_string(output.nextHint);
237 }
238
239 value[jss::account] = toBase58(output.account);
240}
241
242} // namespace xrpl::rpc
Represents a JSON value.
Definition json_value.h:121
Value & append(Value const &value)
Append value to array at the end.
static std::optional< RPCTrustLine > makeItem(AccountID const &accountID, SLE::ConstRef sle)
Definition TrustLine.cpp:87
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::expected< Output, ::rpc::Status > process(Input const &input) const
Process the request.
AccountLinesHandler(JsonContext &context)
Construct a handler for a single request.
void writeResult(json::Value &value, Output const &output) const
Write the output of process() into the response.
std::reference_wrapper< JsonContext > context_
constexpr Zero kZero
Definition Zero.h:30
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Charge const kFeeMediumBurdenRpc
static constexpr LimitRange kAccountLines
Limits for the account_lines command.
The api_version numbers this server serves.
Definition ApiVersion.h:26
unsigned int clampLimit(unsigned int limit, tuning::LimitRange const &range, Role const &role)
Clamps a requested limit to the range a role may use.
std::uint64_t getStartHint(SLE::ConstRef sle, AccountID const &accountID)
Gets the start hint for traversing account objects.
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
::rpc::Status getLedger(T &ledger, UInt256 const &ledgerHash, Context const &context)
Retrieves a ledger by its hash.
void injectLedgerFields(ReadView const &ledger, Context const &context, json::Value &result)
Writes the fields that identify a ledger into a result.
bool isRelatedToAccount(ReadView const &ledger, SLE::ConstRef sle, AccountID const &accountID)
Tests if a ledger entry (SLE) is owned by the specified account.
std::optional< std::pair< uint256, std::uint64_t > > parseAccountMarker(std::string_view marker)
Splits an account marker into the entry to start after and its start hint.
@ RpcActNotFound
Definition ErrorCodes.h:53
@ RpcActMalformed
Definition ErrorCodes.h:73
@ RpcInvalidParams
Definition ErrorCodes.h:67
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:658
bool forEachItemAfter(ReadView const &view, Keylet const &root, UInt256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(SLE::ConstRef)> const &f)
Iterate all items after an item in the given directory.
@ ltANY
A special type, matching any ledger entry type.
UInt256 uint256
Definition base_uint.h:587
T has_value(T... args)
The data an account_lines request produces, written out by writeResult().
std::optional< ErrorCodeI > error
An error written with the ledger fields.
std::optional< uint256 > marker
Where the next page starts, if there is one.
std::vector< RPCTrustLine > lines
The trust lines on this page.
AccountID account
The account whose trust lines are listed.
std::shared_ptr< ReadView const > ledger
The ledger the request selected.
std::uint32_t limit
The limit used; written only with a marker.
std::uint64_t nextHint
The directory page hint written with the marker.
T to_string(T... args)
T unexpected(T... args)