rippled
Loading...
Searching...
No Matches
AccountTx.cpp
1#include <xrpld/app/ledger/LedgerMaster.h>
2#include <xrpld/app/main/Application.h>
3#include <xrpld/app/misc/DeliverMax.h>
4#include <xrpld/app/misc/Transaction.h>
5#include <xrpld/app/rdb/backend/SQLiteDatabase.h>
6#include <xrpld/rpc/Context.h>
7#include <xrpld/rpc/DeliveredAmount.h>
8#include <xrpld/rpc/MPTokenIssuanceID.h>
9#include <xrpld/rpc/Role.h>
10#include <xrpld/rpc/detail/RPCHelpers.h>
11#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
12#include <xrpld/rpc/detail/Tuning.h>
13
14#include <xrpl/json/json_value.h>
15#include <xrpl/ledger/ReadView.h>
16#include <xrpl/protocol/ErrorCodes.h>
17#include <xrpl/protocol/NFTSyntheticSerializer.h>
18#include <xrpl/protocol/RPCErr.h>
19#include <xrpl/protocol/UintTypes.h>
20#include <xrpl/protocol/jss.h>
21#include <xrpl/resource/Fees.h>
22
23namespace xrpl {
24
31
32// parses args into a ledger specifier, or returns a Json object on error
35{
36 Json::Value response;
37 // if ledger_index_min or max is specified, then ledger_hash or ledger_index
38 // should not be specified. Error out if it is
39 if (context.apiVersion > 1u)
40 {
41 if ((params.isMember(jss::ledger_index_min) || params.isMember(jss::ledger_index_max)) &&
42 (params.isMember(jss::ledger_hash) || params.isMember(jss::ledger_index)))
43 {
44 RPC::Status status{rpcINVALID_PARAMS, "invalidParams"};
45 status.inject(response);
46 return response;
47 }
48 }
49 if (params.isMember(jss::ledger_index_min) || params.isMember(jss::ledger_index_max))
50 {
51 uint32_t min = params.isMember(jss::ledger_index_min) && params[jss::ledger_index_min].asInt() >= 0
52 ? params[jss::ledger_index_min].asUInt()
53 : 0;
54 uint32_t max = params.isMember(jss::ledger_index_max) && params[jss::ledger_index_max].asInt() >= 0
55 ? params[jss::ledger_index_max].asUInt()
56 : UINT32_MAX;
57
58 return LedgerRange{min, max};
59 }
60 else if (params.isMember(jss::ledger_hash))
61 {
62 auto& hashValue = params[jss::ledger_hash];
63 if (!hashValue.isString())
64 {
65 RPC::Status status{rpcINVALID_PARAMS, "ledgerHashNotString"};
66 status.inject(response);
67 return response;
68 }
69
70 LedgerHash hash;
71 if (!hash.parseHex(hashValue.asString()))
72 {
73 RPC::Status status{rpcINVALID_PARAMS, "ledgerHashMalformed"};
74 status.inject(response);
75 return response;
76 }
77 return hash;
78 }
79 else if (params.isMember(jss::ledger_index))
80 {
81 LedgerSpecifier ledger;
82 if (params[jss::ledger_index].isNumeric())
83 ledger = params[jss::ledger_index].asUInt();
84 else
85 {
86 std::string ledgerStr = params[jss::ledger_index].asString();
87
88 if (ledgerStr == "current" || ledgerStr.empty())
90 else if (ledgerStr == "closed")
92 else if (ledgerStr == "validated")
94 else
95 {
96 RPC::Status status{rpcINVALID_PARAMS, "ledger_index string malformed"};
97 status.inject(response);
98 return response;
99 }
100 }
101 return ledger;
102 }
104}
105
108{
109 std::uint32_t uValidatedMin;
110 std::uint32_t uValidatedMax;
111 bool bValidated = context.ledgerMaster.getValidatedRange(uValidatedMin, uValidatedMax);
112
113 if (!bValidated)
114 {
115 // Don't have a validated ledger range.
116 if (context.apiVersion == 1)
117 return rpcLGR_IDXS_INVALID;
118 return rpcNOT_SYNCED;
119 }
120
121 std::uint32_t uLedgerMin = uValidatedMin;
122 std::uint32_t uLedgerMax = uValidatedMax;
123 // Does request specify a ledger or ledger range?
124 if (ledgerSpecifier)
125 {
126 auto const status = std::visit(
127 [&](auto const& ls) -> RPC::Status {
128 using T = std::decay_t<decltype(ls)>;
129 if constexpr (std::is_same_v<T, LedgerRange>)
130 {
131 // if ledger_index_min or ledger_index_max is out of
132 // valid ledger range, error out. exclude -1 as
133 // it is a valid input
134 if (context.apiVersion > 1u)
135 {
136 if ((ls.max > uValidatedMax && ls.max != -1) || (ls.min < uValidatedMin && ls.min != 0))
137 {
139 }
140 }
141 if (ls.min > uValidatedMin)
142 {
143 uLedgerMin = ls.min;
144 }
145 if (ls.max < uValidatedMax)
146 {
147 uLedgerMax = ls.max;
148 }
149 if (uLedgerMax < uLedgerMin)
150 {
151 if (context.apiVersion == 1)
152 return rpcLGR_IDXS_INVALID;
154 }
155 }
156 else
157 {
159 auto const status = getLedger(ledgerView, ls, context);
160 if (!ledgerView)
161 {
162 return status;
163 }
164
165 bool validated = context.ledgerMaster.isValidated(*ledgerView);
166
167 if (!validated || ledgerView->header().seq > uValidatedMax ||
168 ledgerView->header().seq < uValidatedMin)
169 {
171 }
172 uLedgerMin = uLedgerMax = ledgerView->header().seq;
173 }
174 return RPC::Status::OK;
175 },
176 *ledgerSpecifier);
177
178 if (status)
179 return status;
180 }
181 return LedgerRange{uLedgerMin, uLedgerMax};
182}
183
186{
188
189 AccountTxResult result;
190
191 auto lgrRange = getLedgerRange(context, args.ledger);
192 if (auto stat = std::get_if<RPC::Status>(&lgrRange))
193 {
194 // An error occurred getting the requested ledger range
195 return {result, *stat};
196 }
197
198 result.ledgerRange = std::get<LedgerRange>(lgrRange);
199
200 result.marker = args.marker;
201
203 args.account,
204 result.ledgerRange.min,
205 result.ledgerRange.max,
206 result.marker,
207 args.limit,
208 isUnlimited(context.role)};
209
210 auto& db = context.app.getRelationalDatabase();
211
212 if (args.binary)
213 {
214 if (args.forward)
215 {
216 auto [tx, marker] = db.oldestAccountTxPageB(options);
217 result.transactions = tx;
218 result.marker = marker;
219 }
220 else
221 {
222 auto [tx, marker] = db.newestAccountTxPageB(options);
223 result.transactions = tx;
224 result.marker = marker;
225 }
226 }
227 else
228 {
229 if (args.forward)
230 {
231 auto [tx, marker] = db.oldestAccountTxPage(options);
232 result.transactions = tx;
233 result.marker = marker;
234 }
235 else
236 {
237 auto [tx, marker] = db.newestAccountTxPage(options);
238 result.transactions = tx;
239 result.marker = marker;
240 }
241 }
242
243 result.limit = args.limit;
244 JLOG(context.j.debug()) << __func__ << " : finished";
245
246 return {result, rpcSUCCESS};
247}
248
252 AccountTxArgs const& args,
253 RPC::JsonContext const& context)
254{
255 Json::Value response;
256 RPC::Status const& error = res.second;
257 if (error.toErrorCode() != rpcSUCCESS)
258 {
259 error.inject(response);
260 }
261 else
262 {
263 AccountTxResult const& result = res.first;
264 response[jss::validated] = true;
265 response[jss::limit] = result.limit;
266 response[jss::account] = context.params[jss::account].asString();
267 response[jss::ledger_index_min] = result.ledgerRange.min;
268 response[jss::ledger_index_max] = result.ledgerRange.max;
269
270 Json::Value& jvTxns = (response[jss::transactions] = Json::arrayValue);
271
272 if (auto txnsData = std::get_if<TxnsData>(&result.transactions))
273 {
274 XRPL_ASSERT(!args.binary, "xrpl::populateJsonResponse : binary is not set");
275
276 for (auto const& [txn, txnMeta] : *txnsData)
277 {
278 if (txn)
279 {
280 Json::Value& jvObj = jvTxns.append(Json::objectValue);
281 jvObj[jss::validated] = true;
282
283 auto const json_tx = (context.apiVersion > 1 ? jss::tx_json : jss::tx);
284 if (context.apiVersion > 1)
285 {
286 jvObj[json_tx] =
288 jvObj[jss::hash] = to_string(txn->getID());
289 jvObj[jss::ledger_index] = txn->getLedger();
290 jvObj[jss::ledger_hash] = to_string(context.ledgerMaster.getHashBySeq(txn->getLedger()));
291
292 if (auto closeTime = context.ledgerMaster.getCloseTimeBySeq(txn->getLedger()))
293 jvObj[jss::close_time_iso] = to_string_iso(*closeTime);
294 }
295 else
296 jvObj[json_tx] = txn->getJson(JsonOptions::include_date);
297
298 auto const& sttx = txn->getSTransaction();
299 RPC::insertDeliverMax(jvObj[json_tx], sttx->getTxnType(), context.apiVersion);
300 if (txnMeta)
301 {
302 jvObj[jss::meta] = txnMeta->getJson(JsonOptions::include_date);
303 insertDeliveredAmount(jvObj[jss::meta], context, txn, *txnMeta);
304 RPC::insertNFTSyntheticInJson(jvObj, sttx, *txnMeta);
305 RPC::insertMPTokenIssuanceID(jvObj[jss::meta], sttx, *txnMeta);
306 }
307 else
308 {
309 // LCOV_EXCL_START
310 UNREACHABLE(
311 "xrpl::populateJsonResponse : missing "
312 "transaction metadata");
313 // LCOV_EXCL_STOP
314 }
315 }
316 }
317 }
318 else
319 {
320 XRPL_ASSERT(args.binary, "xrpl::populateJsonResponse : binary is set");
321
322 for (auto const& binaryData : std::get<TxnsDataBinary>(result.transactions))
323 {
324 Json::Value& jvObj = jvTxns.append(Json::objectValue);
325
326 jvObj[jss::tx_blob] = strHex(std::get<0>(binaryData));
327 auto const json_meta = (context.apiVersion > 1 ? jss::meta_blob : jss::meta);
328 jvObj[json_meta] = strHex(std::get<1>(binaryData));
329 jvObj[jss::ledger_index] = std::get<2>(binaryData);
330 jvObj[jss::validated] = true;
331 }
332 }
333
334 if (result.marker)
335 {
336 response[jss::marker] = Json::objectValue;
337 response[jss::marker][jss::ledger] = result.marker->ledgerSeq;
338 response[jss::marker][jss::seq] = result.marker->txnSeq;
339 }
340 }
341
342 JLOG(context.j.debug()) << __func__ << " : finished";
343 return response;
344}
345
346// {
347// account: account,
348// ledger_index_min: ledger_index // optional, defaults to earliest
349// ledger_index_max: ledger_index, // optional, defaults to latest
350// binary: boolean, // optional, defaults to false
351// forward: boolean, // optional, defaults to false
352// limit: integer, // optional
353// marker: object {ledger: ledger_index, seq: txn_sequence} // optional,
354// resume previous query
355// }
358{
359 if (!context.app.config().useTxTables())
360 return rpcError(rpcNOT_ENABLED);
361
362 auto& params = context.params;
363 AccountTxArgs args;
364 Json::Value response;
365
366 // The document[https://xrpl.org/account_tx.html#account_tx] states that
367 // binary and forward params are both boolean values, however, assigning any
368 // string value works. Do not allow this. This check is for api Version 2
369 // onwards only
370 if (context.apiVersion > 1u && params.isMember(jss::binary) && !params[jss::binary].isBool())
371 {
372 return RPC::invalid_field_error(jss::binary);
373 }
374 if (context.apiVersion > 1u && params.isMember(jss::forward) && !params[jss::forward].isBool())
375 {
376 return RPC::invalid_field_error(jss::forward);
377 }
378
379 if (auto const err = RPC::readLimitField(args.limit, RPC::Tuning::accountTx, context))
380 return *err;
381
382 args.binary = params.isMember(jss::binary) && params[jss::binary].asBool();
383 args.forward = params.isMember(jss::forward) && params[jss::forward].asBool();
384
385 if (!params.isMember(jss::account))
386 return RPC::missing_field_error(jss::account);
387
388 if (!params[jss::account].isString())
389 return RPC::invalid_field_error(jss::account);
390
391 auto const account = parseBase58<AccountID>(params[jss::account].asString());
392 if (!account)
394
395 args.account = *account;
396
397 auto parseRes = parseLedgerArgs(context, params);
398 if (auto jv = std::get_if<Json::Value>(&parseRes))
399 {
400 return *jv;
401 }
402 else
403 {
405 }
406
407 if (params.isMember(jss::marker))
408 {
409 auto& token = params[jss::marker];
410 if (!token.isMember(jss::ledger) || !token.isMember(jss::seq) ||
411 !token[jss::ledger].isConvertibleTo(Json::ValueType::uintValue) ||
412 !token[jss::seq].isConvertibleTo(Json::ValueType::uintValue))
413 {
414 RPC::Status status{
416 "invalid marker. Provide ledger index via ledger field, and "
417 "transaction sequence number via seq field"};
418 status.inject(response);
419 return response;
420 }
421 args.marker = {token[jss::ledger].asUInt(), token[jss::seq].asUInt()};
422 }
423
424 auto res = doAccountTxHelp(context, args);
425 JLOG(context.j.debug()) << __func__ << " populating response";
426 return populateJsonResponse(res, args, context);
427}
428
429} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Value & append(Value const &value)
Append value to array at the end.
Int asInt() const
UInt asUInt() const
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
Stream debug() const
Definition Journal.h:300
virtual Config & config()=0
bool useTxTables() const
Definition Config.h:318
bool getValidatedRange(std::uint32_t &minVal, std::uint32_t &maxVal)
uint256 getHashBySeq(std::uint32_t index)
Get a ledger's hash by sequence number using the cache.
std::optional< NetClock::time_point > getCloseTimeBySeq(LedgerIndex ledgerIndex)
bool isValidated(ReadView const &ledger)
virtual std::pair< MetaTxsList, std::optional< AccountTxMarker > > oldestAccountTxPageB(AccountTxPageOptions const &options)=0
oldestAccountTxPageB Returns the oldest transactions in binary form for the account that matches the ...
std::vector< AccountTx > AccountTxs
std::vector< txnMetaLedgerType > MetaTxsList
std::variant< LedgerRange, LedgerShortcut, LedgerSequence, LedgerHash > LedgerSpecifier
std::tuple< Blob, Blob, std::uint32_t > txnMetaLedgerType
virtual RelationalDatabase & getRelationalDatabase()=0
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:471
T empty(T... args)
T is_same_v
@ arrayValue
array value (ordered list)
Definition json_value.h:25
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
@ uintValue
unsigned integer value
Definition json_value.h:21
static LimitRange constexpr accountTx
Limits for the account_tx command.
Json::Value invalid_field_error(std::string const &name)
Definition ErrorCodes.h:268
void insertDeliverMax(Json::Value &tx_json, TxType txnType, unsigned int apiVersion)
Copy Amount field to DeliverMax field in transaction output JSON.
Definition DeliverMax.cpp:9
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:226
void insertMPTokenIssuanceID(Json::Value &response, std::shared_ptr< STTx const > const &transaction, TxMeta const &transactionMeta)
void insertNFTSyntheticInJson(Json::Value &, std::shared_ptr< STTx const > const &, TxMeta const &)
Adds common synthetic fields to transaction-related JSON responses.
std::optional< Json::Value > readLimitField(unsigned int &limit, Tuning::LimitRange const &range, JsonContext const &context)
Retrieves the limit value from a JsonContext or sets a default.
Charge const feeMediumBurdenRPC
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Json::Value populateJsonResponse(std::pair< AccountTxResult, RPC::Status > const &res, AccountTxArgs const &args, RPC::JsonContext const &context)
std::variant< std::optional< LedgerSpecifier >, Json::Value > parseLedgerArgs(RPC::Context &context, Json::Value const &params)
Definition AccountTx.cpp:34
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:597
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:10
@ Closed
The most recently closed ledger (may not be validated)
@ Current
The current working ledger (open, not yet closed)
@ Validated
The most recently validated ledger.
Json::Value doAccountTxJson(RPC::JsonContext &context)
std::variant< LedgerRange, RPC::Status > getLedgerRange(RPC::Context &context, std::optional< LedgerSpecifier > const &ledgerSpecifier)
Json::Value rpcError(error_code_i iError)
Definition RPCErr.cpp:12
std::string to_string_iso(date::sys_time< Duration > tp)
Definition chrono.h:67
std::pair< AccountTxResult, RPC::Status > doAccountTxHelp(RPC::Context &context, AccountTxArgs const &args)
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition Role.cpp:96
@ rpcNOT_ENABLED
Definition ErrorCodes.h:39
@ rpcLGR_NOT_VALIDATED
Definition ErrorCodes.h:53
@ rpcLGR_IDX_MALFORMED
Definition ErrorCodes.h:93
@ rpcLGR_IDXS_INVALID
Definition ErrorCodes.h:92
@ rpcNOT_SYNCED
Definition ErrorCodes.h:47
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:64
@ rpcACT_MALFORMED
Definition ErrorCodes.h:70
@ rpcINVALID_LGR_RANGE
Definition ErrorCodes.h:116
@ rpcSUCCESS
Definition ErrorCodes.h:24
The context of information needed to call an RPC.
Definition Context.h:19
beast::Journal const j
Definition Context.h:20
Application & app
Definition Context.h:21
Resource::Charge & loadType
Definition Context.h:22
unsigned int apiVersion
Definition Context.h:29
LedgerMaster & ledgerMaster
Definition Context.h:24
Json::Value params
Definition Context.h:43
Status represents the results of an operation that might fail.
Definition Status.h:20
static constexpr Code OK
Definition Status.h:26
std::optional< AccountTxMarker > marker
std::optional< LedgerSpecifier > ledger
std::variant< AccountTxs, MetaTxsList > transactions
std::optional< AccountTxMarker > marker
T visit(T... args)