xrpld
Loading...
Searching...
No Matches
AccountNFTs.cpp
1#include <xrpld/rpc/handlers/account/AccountNFTs.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/json/json_value.h>
10#include <xrpl/ledger/ReadView.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/ErrorCodes.h>
13#include <xrpl/protocol/Indexes.h>
14#include <xrpl/protocol/LedgerFormats.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STArray.h> // IWYU pragma: keep
17#include <xrpl/protocol/jss.h>
18#include <xrpl/protocol/nft.h>
19#include <xrpl/protocol/nftPageMask.h>
20#include <xrpl/resource/Fees.h>
21
22#include <rpcspec/Errors.hpp>
23#include <rpcspec/handlers/account_nfts/Types.hpp>
24
25#include <cstdint>
26#include <expected>
27
28namespace xrpl::rpc {
29
30static_assert(::rpc::spec::handlers::account_nfts::kLimitMin == tuning::kAccountNfTokens.rmin);
31static_assert(
32 ::rpc::spec::handlers::account_nfts::kLimitDefault == tuning::kAccountNfTokens.rDefault);
33static_assert(::rpc::spec::handlers::account_nfts::kLimitMax == tuning::kAccountNfTokens.rmax);
34
38
39std::expected<AccountNFTsHandler::Output, ::rpc::Status>
40AccountNFTsHandler::process(Input const& input) const
41{
42 auto& context = context_.get();
43
44 Output output;
45 if (auto const status = getLedger(output.ledger, input.ledger, context);
46 status != ::rpc::Status::kOK)
47 return std::unexpected{status};
48 auto const& ledger = *output.ledger;
49 auto const accountID = input.account;
50 output.account = accountID;
51
52 if (!ledger.exists(keylet::account(accountID)))
54
55 output.limit = clampLimit(input.limit, tuning::kAccountNfTokens, context.role);
56
57 ::rpc::Status const invalidMarker{RpcInvalidParams, invalidFieldMessage(jss::marker)};
58 bool const markerSet = input.marker.has_value();
59 UInt256 const marker = input.marker.value_or(UInt256{});
60
61 auto const first = keylet::nftokenPage(keylet::nftokenPageMin(accountID), marker);
62 auto const last = keylet::nftokenPageMax(accountID);
63
64 auto cp = ledger.read(
65 Keylet(ltNFTOKEN_PAGE, ledger.succ(first.key, last.key.next()).value_or(last.key)));
66
67 std::uint32_t cnt = 0;
68
69 // Continue iteration from the current page:
70 bool pastMarker = marker.isZero();
71 bool markerFound = false;
72 UInt256 const maskedMarker = marker & nft::kPageMask;
73 while (cp)
74 {
75 auto const& pageNftokens = cp->getFieldArray(sfNFTokens);
76
77 for (auto const& nftoken : pageNftokens)
78 {
79 // Scrolling past the marker gets weird. We need to look at
80 // a couple of conditions.
81 //
82 // 1. If the low 96-bits don't match, then we compare only
83 // against the low 96-bits, since that's what determines
84 // the sort order of the pages.
85 //
86 // 2. However, within one page there can be a number of
87 // NFTokenIDs that all have the same low 96 bits. If we're
88 // in that case then we need to compare against the full
89 // 256 bits.
90 UInt256 const nftokenID = nftoken[sfNFTokenID];
91 UInt256 const maskedNftokenID = nftokenID & nft::kPageMask;
92
93 if (!pastMarker)
94 {
95 if (maskedNftokenID < maskedMarker)
96 continue;
97
98 if (maskedNftokenID == maskedMarker && nftokenID < marker)
99 continue;
100
101 if (nftokenID == marker)
102 {
103 markerFound = true;
104 continue;
105 }
106 }
107
108 if (markerSet && !markerFound)
109 return std::unexpected{invalidMarker};
110
111 pastMarker = true;
112
113 output.nfts.push_back(nftoken);
114
115 if (++cnt == output.limit)
116 {
117 output.marker = nftokenID;
118 return output;
119 }
120 }
121
122 if (auto const npm = (*cp)[~sfNextPageMin]; npm.has_value())
123 {
124 cp = ledger.read(Keylet(ltNFTOKEN_PAGE, *npm));
125 }
126 else
127 {
128 cp = nullptr;
129 }
130 }
131
132 if (markerSet && !markerFound)
133 return std::unexpected{invalidMarker};
134
135 context.loadType = resource::kFeeMediumBurdenRpc;
136 return output;
137}
138
139void
141{
142 injectLedgerFields(*output.ledger, context_.get(), value);
143
144 auto& nfts = (value[jss::account_nfts] = json::ValueType::Array);
145 for (auto const& nftoken : output.nfts)
146 {
147 json::Value& obj = nfts.append(nftoken.getJson(JsonOptions::Values::None));
148
149 // Pull out the components of the nft ID.
150 UInt256 const nftokenID = nftoken[sfNFTokenID];
151 obj[sfFlags.jsonName] = nft::getFlags(nftokenID);
152 obj[sfIssuer.jsonName] = to_string(nft::getIssuer(nftokenID));
153 obj[sfNFTokenTaxon.jsonName] = nft::toUInt32(nft::getTaxon(nftokenID));
154 obj[jss::nft_serial] = nft::getSequence(nftokenID);
155 if (std::uint16_t const xferFee = {nft::getTransferFee(nftokenID)})
156 obj[sfTransferFee.jsonName] = xferFee;
157 }
158
159 if (output.marker.has_value())
160 {
161 value[jss::limit] = output.limit;
162 value[jss::marker] = to_string(*output.marker);
163 return;
164 }
165
166 value[jss::account] = toBase58(output.account);
167}
168
169} // namespace xrpl::rpc
Represents a JSON value.
Definition json_value.h:121
Value & append(Value const &value)
Append value to array at the end.
bool isZero() const
Definition base_uint.h:563
AccountNFTsHandler(JsonContext &context)
Construct a handler for a single request.
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_
Definition AccountNFTs.h:70
@ Array
array value (ordered list)
Definition json_value.h:32
Keylet nftokenPage(Keylet const &k, UInt256 const &token)
Definition Indexes.cpp:458
Keylet nftokenPageMin(AccountID const &owner)
NFT page keylets.
Definition Indexes.cpp:442
Keylet nftokenPageMax(AccountID const &owner)
A keylet for the owner's last possible NFT page.
Definition Indexes.cpp:450
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
std::uint32_t toUInt32(Taxon t)
Definition nft.h:27
std::uint32_t getSequence(UInt256 const &id)
Definition nft.h:55
std::uint16_t getFlags(UInt256 const &id)
Definition nft.h:39
AccountID getIssuer(UInt256 const &id)
Definition nft.h:99
std::uint16_t getTransferFee(UInt256 const &id)
Definition nft.h:47
constexpr UInt256 kPageMask(std::string_view("0000000000000000000000000000000000000000ffffffffffffffffffffffff"))
Taxon getTaxon(UInt256 const &id)
Definition nft.h:87
Charge const kFeeMediumBurdenRpc
static constexpr LimitRange kAccountNfTokens
Limits for the account_nftokens command, in pages.
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::string invalidFieldMessage(std::string const &name)
Definition ErrorCodes.h:273
::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.
@ RpcActNotFound
Definition ErrorCodes.h:53
@ 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
BaseUInt< 256 > UInt256
Definition base_uint.h:581
T push_back(T... args)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
The data an account_nfts request produces, written out by writeResult().
Definition AccountNFTs.h:36
std::shared_ptr< ReadView const > ledger
The ledger the request selected.
Definition AccountNFTs.h:37
std::uint32_t limit
The limit used; written only with a marker.
Definition AccountNFTs.h:39
AccountID account
The account; written only on the last page.
Definition AccountNFTs.h:38
std::vector< STObject > nfts
The NFTs on this page.
Definition AccountNFTs.h:40
std::optional< uint256 > marker
Where the next page starts, if there is one.
Definition AccountNFTs.h:41
T unexpected(T... args)