xrpld
Loading...
Searching...
No Matches
AccountOffers.cpp
1#include <xrpld/rpc/handlers/account/AccountOffers.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/Tuning.h>
7
8#include <xrpl/basics/base_uint.h>
9#include <xrpl/beast/utility/Zero.h>
10#include <xrpl/beast/utility/instrumentation.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/SField.h>
19#include <xrpl/protocol/STAmount.h>
20#include <xrpl/protocol/STLedgerEntry.h>
21#include <xrpl/protocol/jss.h>
22#include <xrpl/resource/Fees.h>
23
24#include <rpcspec/Errors.hpp>
25#include <rpcspec/handlers/account_offers/Types.hpp>
26
27#include <cstdint>
28#include <expected>
29#include <optional>
30#include <string>
31#include <utility>
32
33namespace xrpl::rpc {
34
35static_assert(::rpc::spec::handlers::account_offers::kLimitMin == tuning::kAccountOffers.rmin);
36static_assert(
37 ::rpc::spec::handlers::account_offers::kLimitDefault == tuning::kAccountOffers.rDefault);
38static_assert(::rpc::spec::handlers::account_offers::kLimitMax == tuning::kAccountOffers.rmax);
39
40namespace {
41
42void
43appendOfferJson(SLE::ConstRef offer, json::Value& offers)
44{
45 STAmount const dirRate = amountFromQuality(getQuality(offer->getFieldH256(sfBookDirectory)));
46 json::Value& obj(offers.append(json::ValueType::Object));
47 offer->getFieldAmount(sfTakerPays).setJson(obj[jss::taker_pays]);
48 offer->getFieldAmount(sfTakerGets).setJson(obj[jss::taker_gets]);
49 obj[jss::seq] = offer->getFieldU32(sfSequence);
50 obj[jss::flags] = offer->getFieldU32(sfFlags);
51 obj[jss::quality] = dirRate.getText();
52 if (offer->isFieldPresent(sfExpiration))
53 obj[jss::expiration] = offer->getFieldU32(sfExpiration);
54}
55
56} // namespace
57
61
62std::expected<AccountOffersHandler::Output, ::rpc::Status>
63AccountOffersHandler::process(Input const& input) const
64{
65 auto& context = context_.get();
66
67 Output output;
68 if (auto const status = getLedger(output.ledger, input.ledger, context);
69 status != ::rpc::Status::kOK)
70 return std::unexpected{status};
71 auto const& ledger = *output.ledger;
72
73 if (not input.account.has_value())
74 {
75 output.error = RpcActMalformed;
76 return output;
77 }
78 auto const accountID = *input.account;
79 output.account = accountID;
80
81 if (!ledger.exists(keylet::account(accountID)))
83
84 auto const limit = clampLimit(input.limit, tuning::kAccountOffers, context.role);
85
86 auto const parsed = input.marker.has_value()
87 ? parseAccountMarker(*input.marker)
89 if (not parsed.has_value())
90 {
91 // LCOV_EXCL_START
92 UNREACHABLE("xrpl::rpc::AccountOffersHandler::process : marker validated by the spec");
93 return std::unexpected{::rpc::Status{RpcInvalidParams, invalidFieldMessage(jss::marker)}};
94 // LCOV_EXCL_STOP
95 }
96 auto const [startAfter, startHint] = *parsed;
97
98 if (input.marker.has_value())
99 {
100 // We then must check if the object pointed to by the marker is actually
101 // owned by the account in the request.
102 auto const sle = ledger.read({ltANY, startAfter});
103
104 if (!sle)
106
107 if (!isRelatedToAccount(ledger, sle, accountID))
109 }
110
111 std::uint32_t count = 0;
112 std::optional<UInt256> marker = {};
113 std::uint64_t nextHint = 0;
114 if (!forEachItemAfter(
115 ledger,
116 accountID,
117 startAfter,
118 startHint,
119 limit + 1,
120 [&output, &count, &marker, &limit, &nextHint, &accountID](SLE::ConstRef sle) {
121 if (!sle)
122 {
123 // LCOV_EXCL_START
124 UNREACHABLE("xrpl::rpc::AccountOffersHandler::process : null SLE");
125 return false;
126 // LCOV_EXCL_STOP
127 }
128
129 if (++count == limit)
130 {
131 marker = sle->key();
132 nextHint = getStartHint(sle, accountID);
133 }
134
135 if (count <= limit && sle->getType() == ltOFFER)
136 {
137 output.offers.emplace_back(sle);
138 }
139
140 return true;
141 }))
142 {
144 }
145
146 // Both conditions need to be checked because marker is set on the limit-th
147 // item, but if there is no item on the limit + 1 iteration, then there is
148 // no need to return a marker.
149 if (count == limit + 1 && marker.has_value())
150 {
151 output.limit = limit;
152 output.marker = marker;
153 output.nextHint = nextHint;
154 }
155
156 context.loadType = resource::kFeeMediumBurdenRpc;
157 return output;
158}
159
160void
162{
163 injectLedgerFields(*output.ledger, context_.get(), value);
164
165 if (output.error.has_value())
166 {
167 injectError(*output.error, value);
168 return;
169 }
170
171 value[jss::account] = toBase58(output.account);
172
173 json::Value& jsonOffers(value[jss::offers] = json::ValueType::Array);
174 for (auto const& offer : output.offers)
175 appendOfferJson(offer, jsonOffers);
176
177 if (output.marker.has_value())
178 {
179 value[jss::limit] = output.limit;
180 value[jss::marker] = to_string(*output.marker) + "," + std::to_string(output.nextHint);
181 }
182}
183
184} // namespace xrpl::rpc
Represents a JSON value.
Definition json_value.h:121
Value & append(Value const &value)
Append value to array at the end.
std::shared_ptr< STLedgerEntry const > const & ConstRef
std::expected< Output, ::rpc::Status > process(Input const &input) const
Process the request.
void writeResult(json::Value &value, Output const &output) const
Write the output of process() into the response.
std::reference_wrapper< JsonContext > context_
AccountOffersHandler(JsonContext &context)
Construct a handler for a single request.
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 offer(AccountID const &id, SeqProxy const &seq) noexcept
An offer from an account.
Definition Indexes.cpp:310
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Charge const kFeeMediumBurdenRpc
static constexpr LimitRange kAccountOffers
Limits for the account_offers 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.
std::string invalidFieldMessage(std::string const &name)
Definition ErrorCodes.h:273
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::uint64_t getQuality(UInt256 const &uBase)
Definition Indexes.cpp:207
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
STAmount amountFromQuality(std::uint64_t rate)
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_offers request produces, written out by writeResult().
std::vector< SLE::const_pointer > offers
The offers on this page.
AccountID account
The account whose offers are listed.
std::optional< ErrorCodeI > error
An error written with the ledger fields.
std::uint32_t limit
The limit used; written only with a marker.
std::shared_ptr< ReadView const > ledger
The ledger the request selected.
std::uint64_t nextHint
The directory page hint written with the marker.
std::optional< uint256 > marker
Where the next page starts, if there is one.
T to_string(T... args)
T unexpected(T... args)