xrpld
Loading...
Searching...
No Matches
AccountChannels.cpp
1#include <xrpld/rpc/handlers/account/AccountChannels.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/basics/strHex.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/PublicKey.h>
20#include <xrpl/protocol/SField.h>
21#include <xrpl/protocol/STLedgerEntry.h>
22#include <xrpl/protocol/jss.h>
23#include <xrpl/protocol/tokens.h>
24#include <xrpl/resource/Fees.h>
25
26#include <rpcspec/Errors.hpp>
27#include <rpcspec/handlers/account_channels/Types.hpp>
28
29#include <cstdint>
30#include <expected>
31#include <optional>
32#include <string>
33#include <utility>
34
35namespace xrpl::rpc {
36
37static_assert(::rpc::spec::handlers::account_channels::kLimitMin == tuning::kAccountChannels.rmin);
38static_assert(
39 ::rpc::spec::handlers::account_channels::kLimitDefault == tuning::kAccountChannels.rDefault);
40static_assert(::rpc::spec::handlers::account_channels::kLimitMax == tuning::kAccountChannels.rmax);
41
42namespace {
43
44void
45appendChannelJson(SLE const& channel, json::Value& channels)
46{
47 json::Value& jDst(channels.append(json::ValueType::Object));
48 jDst[jss::channel_id] = to_string(channel.key());
49 jDst[jss::account] = to_string(channel[sfAccount]);
50 jDst[jss::destination_account] = to_string(channel[sfDestination]);
51 jDst[jss::amount] = channel[sfAmount].getText();
52 jDst[jss::balance] = channel[sfBalance].getText();
53 if (publicKeyType(channel[sfPublicKey]))
54 {
55 PublicKey const pk(channel[sfPublicKey]);
56 jDst[jss::public_key] = toBase58(TokenType::AccountPublic, pk);
57 jDst[jss::public_key_hex] = strHex(pk);
58 }
59 jDst[jss::settle_delay] = channel[sfSettleDelay];
60 if (auto const& v = channel[~sfExpiration])
61 jDst[jss::expiration] = *v;
62 if (auto const& v = channel[~sfCancelAfter])
63 jDst[jss::cancel_after] = *v;
64 if (auto const& v = channel[~sfSourceTag])
65 jDst[jss::source_tag] = *v;
66 if (auto const& v = channel[~sfDestinationTag])
67 jDst[jss::destination_tag] = *v;
68}
69
70} // namespace
71
75
76std::expected<AccountChannelsHandler::Output, ::rpc::Status>
77AccountChannelsHandler::process(Input const& input) const
78{
79 auto& context = context_.get();
80
81 Output output;
82 if (auto const status = getLedger(output.ledger, input.ledger, context);
83 status != ::rpc::Status::kOK)
84 return std::unexpected{status};
85 auto const& ledger = *output.ledger;
86
87 auto const accountID = input.account;
88 output.account = accountID;
89
90 if (!ledger.exists(keylet::account(accountID)))
92
93 auto const& destination = input.destinationAccount;
94 auto const limit = clampLimit(input.limit, tuning::kAccountChannels, context.role);
95
96 auto const parsed = input.marker.has_value()
97 ? parseAccountMarker(*input.marker)
99 if (!parsed.has_value())
100 {
101 // LCOV_EXCL_START
102 UNREACHABLE("xrpl::rpc::AccountChannelsHandler::process : marker validated by the spec");
104 // LCOV_EXCL_STOP
105 }
106 auto const [startAfter, startHint] = *parsed;
107
108 if (input.marker.has_value())
109 {
110 // We then must check if the object pointed to by the marker is actually
111 // owned by the account in the request.
112 auto const sle = ledger.read({ltANY, startAfter});
113
114 if (!sle)
116
117 if (!isRelatedToAccount(ledger, sle, accountID))
119 }
120
121 std::uint32_t count = 0;
123 std::uint64_t nextHint = 0;
124 if (!forEachItemAfter(
125 ledger,
126 accountID,
127 startAfter,
128 startHint,
129 limit + 1,
130 [&output, &accountID, &destination, &count, &limit, &marker, &nextHint](
131 SLE::ConstRef sle) {
132 if (!sle)
133 {
134 // LCOV_EXCL_START
135 UNREACHABLE("xrpl::rpc::AccountChannelsHandler::process : null SLE");
136 return false;
137 // LCOV_EXCL_STOP
138 }
139
140 if (++count == limit)
141 {
142 marker = sle->key();
143 nextHint = getStartHint(sle, accountID);
144 }
145
146 if (count <= limit && sle->getType() == ltPAYCHAN &&
147 (*sle)[sfAccount] == accountID &&
148 (!destination.has_value() || *destination == (*sle)[sfDestination]))
149 {
150 output.channels.emplace_back(sle);
151 }
152
153 return true;
154 }))
155 {
157 }
158
159 // Both conditions need to be checked because marker is set on the limit-th
160 // item, but if there is no item on the limit + 1 iteration, then there is
161 // no need to return a marker.
162 if (count == limit + 1 && marker.has_value())
163 {
164 output.limit = limit;
165 output.marker = marker;
166 output.nextHint = nextHint;
167 }
168
169 context.loadType = resource::kFeeMediumBurdenRpc;
170 return output;
171}
172
173void
175{
176 injectLedgerFields(*output.ledger, context_.get(), value);
177
178 if (output.marker.has_value())
179 {
180 value[jss::limit] = output.limit;
181 value[jss::marker] = to_string(*output.marker) + "," + std::to_string(output.nextHint);
182 }
183
184 value[jss::account] = toBase58(output.account);
185
186 json::Value& channels(value[jss::channels] = json::ValueType::Array);
187 for (auto const& channel : output.channels)
188 appendChannelJson(*channel, channels);
189}
190
191} // 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.
std::reference_wrapper< JsonContext > context_
void writeResult(json::Value &value, Output const &output) const
Write the output of process() into the response.
AccountChannelsHandler(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 account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Charge const kFeeMediumBurdenRpc
static constexpr LimitRange kAccountChannels
Limits for the account_channels 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.
::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.
uint256 channel(AccountID const &account, AccountID const &dst, std::uint32_t seqProxyValue)
Definition paychan.cpp:96
@ RpcActNotFound
Definition ErrorCodes.h:53
@ RpcInvalidParams
Definition ErrorCodes.h:67
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
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
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
STLedgerEntry SLE
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_channels request produces, written out by writeResult().
std::shared_ptr< ReadView const > ledger
The ledger the request selected.
AccountID account
The account whose channels are listed.
std::vector< SLE::const_pointer > channels
The channels on this page.
std::optional< uint256 > marker
Where the next page starts, if there is one.
std::uint64_t nextHint
The directory page hint written with the marker.
std::uint32_t limit
The limit used; written only with a marker.
T to_string(T... args)
T unexpected(T... args)