xrpld
Loading...
Searching...
No Matches
LedgerEntry.cpp
1#include <xrpld/rpc/Context.h>
2#include <xrpld/rpc/GRPCHandlers.h>
3#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
4#include <xrpld/rpc/handlers/ledger/LedgerEntryHelpers.h>
5
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/basics/strHex.h>
8#include <xrpl/json/json_errors.h>
9#include <xrpl/json/json_value.h>
10#include <xrpl/ledger/ReadView.h>
11#include <xrpl/ledger/helpers/CredentialHelpers.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/ErrorCodes.h>
14#include <xrpl/protocol/Indexes.h>
15#include <xrpl/protocol/Keylet.h>
16#include <xrpl/protocol/LedgerFormats.h>
17#include <xrpl/protocol/Protocol.h>
18#include <xrpl/protocol/SField.h>
19#include <xrpl/protocol/STArray.h>
20#include <xrpl/protocol/STObject.h>
21#include <xrpl/protocol/SeqProxy.h>
22#include <xrpl/protocol/Serializer.h>
23#include <xrpl/protocol/UintTypes.h>
24#include <xrpl/protocol/jss.h>
25
26#include <grpcpp/support/status.h>
27#include <org/xrpl/rpc/v1/get_ledger_entry.pb.h>
28
29#include <array>
30#include <cstdint>
31#include <expected>
32#include <functional>
33#include <memory>
34#include <string>
35#include <utility>
36
37namespace xrpl {
38
40 json::Value const&,
42 unsigned const apiVersion)>;
43
44static std::expected<UInt256, json::Value>
46 Keylet const& keylet,
47 json::Value const& params,
48 json::StaticString const& fieldName,
49 unsigned const apiVersion);
50
51// Helper function to return FunctionType for objects that have a fixed
52// location. That is, they don't take parameters to compute the index.
53// e.g. amendments, fees, negative UNL, etc.
54static FunctionType
56{
57 return [keylet](
58 json::Value const& params,
59 json::StaticString const fieldName,
60 unsigned const apiVersion) -> std::expected<UInt256, json::Value> {
61 return parseFixed(keylet, params, fieldName, apiVersion);
62 };
63}
64
65static std::expected<UInt256, json::Value>
67 json::Value const& params,
68 json::StaticString const fieldName,
69 std::string const& expectedType = "hex string or object")
70{
71 if (auto const uNodeIndex = ledger_entry_helpers::parse<UInt256>(params))
72 {
73 return *uNodeIndex;
74 }
75 return ledger_entry_helpers::invalidFieldError("malformedRequest", fieldName, expectedType);
76}
77
78static std::expected<UInt256, json::Value>
79parseIndex(json::Value const& params, json::StaticString const fieldName, unsigned const apiVersion)
80{
81 if (apiVersion > 2u && params.isString())
82 {
83 std::string const index = params.asString();
84 if (index == jss::amendments.cStr())
85 return keylet::amendments().key;
86 if (index == jss::fee.cStr())
87 return keylet::feeSettings().key;
88 if (index == jss::nunl)
89 return keylet::negativeUNL().key;
90 if (index == jss::hashes)
91 {
92 // Note this only finds the "short" skip list. Use "hashes":index to
93 // get the long list.
94 return keylet::skip().key;
95 }
96 }
97 return parseObjectID(params, fieldName, "hex string");
98}
99
100static std::expected<UInt256, json::Value>
102 json::Value const& params,
103 json::StaticString const fieldName,
104 [[maybe_unused]] unsigned const apiVersion)
105{
106 if (auto const account = ledger_entry_helpers::parse<AccountID>(params))
107 {
108 return keylet::account(*account).key;
109 }
110
111 return ledger_entry_helpers::invalidFieldError("malformedAddress", fieldName, "AccountID");
112}
113
115
116static std::expected<UInt256, json::Value>
118 json::Value const& params,
119 json::StaticString const fieldName,
120 [[maybe_unused]] unsigned const apiVersion)
121{
122 if (!params.isObject())
123 {
124 return parseObjectID(params, fieldName);
125 }
126
127 if (auto const value = ledger_entry_helpers::hasRequired(params, {jss::asset, jss::asset2});
128 !value)
129 {
130 return std::unexpected(value.error());
131 }
132
133 auto const asset = ledger_entry_helpers::requiredAsset(params, jss::asset, "malformedRequest");
134 if (!asset)
135 return std::unexpected(asset.error());
136
137 auto const asset2 =
138 ledger_entry_helpers::requiredAsset(params, jss::asset2, "malformedRequest");
139 if (!asset2)
140 return std::unexpected(asset2.error());
141
142 return keylet::amm(*asset, *asset2).key;
143}
144
145static std::expected<UInt256, json::Value>
147 json::Value const& params,
148 json::StaticString const fieldName,
149 [[maybe_unused]] unsigned const apiVersion)
150{
151 if (!params.isObject())
152 {
153 return parseObjectID(params, fieldName);
154 }
155
156 auto const account =
157 ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAddress");
158 if (!account)
159 return std::unexpected(account.error());
160
161 auto const sequence =
162 ledger_entry_helpers::requiredUInt32(params, jss::seq, "malformedRequest");
163 if (!sequence)
164 return std::unexpected(sequence.error());
165
166 auto const seqProxy = SeqProxy::rawSequence(*sequence);
167 return keylet::check(*account, seqProxy).key;
168}
169
170static std::expected<UInt256, json::Value>
172 json::Value const& cred,
173 json::StaticString const fieldName,
174 [[maybe_unused]] unsigned const apiVersion)
175{
176 if (!cred.isObject())
177 {
178 return parseObjectID(cred, fieldName);
179 }
180
181 auto const subject =
182 ledger_entry_helpers::requiredAccountID(cred, jss::subject, "malformedRequest");
183 if (!subject)
184 return std::unexpected(subject.error());
185
186 auto const issuer =
187 ledger_entry_helpers::requiredAccountID(cred, jss::issuer, "malformedRequest");
188 if (!issuer)
189 return std::unexpected(issuer.error());
190
191 auto const credType = ledger_entry_helpers::requiredHexBlob(
192 cred, jss::credential_type, kMaxCredentialTypeLength, "malformedRequest");
193 if (!credType)
194 return std::unexpected(credType.error());
195
196 return keylet::credential(*subject, *issuer, Slice(credType->data(), credType->size())).key;
197}
198
199static std::expected<UInt256, json::Value>
201 json::Value const& params,
202 json::StaticString const fieldName,
203 [[maybe_unused]] unsigned const apiVersion)
204{
205 if (!params.isObject())
206 {
207 return parseObjectID(params, fieldName);
208 }
209
210 auto const account =
211 ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAddress");
212 if (!account)
213 return std::unexpected(account.error());
214
215 auto const authorize =
216 ledger_entry_helpers::requiredAccountID(params, jss::authorize, "malformedAddress");
217 if (!authorize)
218 return std::unexpected(authorize.error());
219
220 return keylet::delegate(*account, *authorize).key;
221}
222
223static std::expected<STArray, json::Value>
225{
226 if (!jv.isArray())
227 {
229 "malformedAuthorizedCredentials", jss::authorized_credentials, "array");
230 }
231
232 std::uint32_t const n = jv.size();
234 {
235 return std::unexpected(
237 "malformedAuthorizedCredentials",
238 "Invalid field '" + std::string(jss::authorized_credentials) +
239 "', array too long."));
240 }
241
242 if (n == 0)
243 {
244 return std::unexpected(
246 "malformedAuthorizedCredentials",
247 "Invalid field '" + std::string(jss::authorized_credentials) + "', array empty."));
248 }
249
250 STArray arr(sfAuthorizeCredentials, n);
251 for (auto const& jo : jv)
252 {
253 if (!jo.isObject())
254 {
256 "malformedAuthorizedCredentials", jss::authorized_credentials, "array of objects");
257 }
258
259 if (auto const value = ledger_entry_helpers::hasRequired(
260 jo, {jss::issuer, jss::credential_type}, "malformedAuthorizedCredentials");
261 !value)
262 {
263 return std::unexpected(value.error());
264 }
265
266 auto const issuer = ledger_entry_helpers::requiredAccountID(
267 jo, jss::issuer, "malformedAuthorizedCredentials");
268 if (!issuer)
269 return std::unexpected(issuer.error());
270
271 auto const credentialType = ledger_entry_helpers::requiredHexBlob(
272 jo, jss::credential_type, kMaxCredentialTypeLength, "malformedAuthorizedCredentials");
273 if (!credentialType)
274 return std::unexpected(credentialType.error());
275
276 auto credential = STObject::makeInnerObject(sfCredential);
277 credential.setAccountID(sfIssuer, *issuer);
278 credential.setFieldVL(sfCredentialType, *credentialType);
279 arr.pushBack(std::move(credential));
280 }
281
282 return arr;
283}
284
285static std::expected<UInt256, json::Value>
287 json::Value const& dp,
288 json::StaticString const fieldName,
289 [[maybe_unused]] unsigned const apiVersion)
290{
291 if (!dp.isObject())
292 {
293 return parseObjectID(dp, fieldName);
294 }
295
296 if ((dp.isMember(jss::authorized) == dp.isMember(jss::authorized_credentials)))
297 {
299 "malformedRequest",
300 "Must have exactly one of `authorized` and "
301 "`authorized_credentials`.");
302 }
303
304 auto const owner = ledger_entry_helpers::requiredAccountID(dp, jss::owner, "malformedOwner");
305 if (!owner)
306 {
307 return std::unexpected(owner.error());
308 }
309
310 if (dp.isMember(jss::authorized))
311 {
312 if (auto const authorized = ledger_entry_helpers::parse<AccountID>(dp[jss::authorized]))
313 {
314 return keylet::depositPreauth(*owner, *authorized).key;
315 }
317 "malformedAuthorized", jss::authorized, "AccountID");
318 }
319
320 auto const& ac(dp[jss::authorized_credentials]);
321 auto const arr = parseAuthorizeCredentials(ac);
322 if (!arr.has_value())
323 return std::unexpected(arr.error());
324
325 auto const& sorted = credentials::makeSorted(arr.value());
326 if (sorted.empty())
327 {
328 // TODO: this error message is bad/inaccurate
330 "malformedAuthorizedCredentials", jss::authorized_credentials, "array");
331 }
332
333 return keylet::depositPreauth(*owner, sorted).key;
334}
335
336static std::expected<UInt256, json::Value>
338 json::Value const& params,
339 json::StaticString const fieldName,
340 [[maybe_unused]] unsigned const apiVersion)
341{
342 auto const account = ledger_entry_helpers::parse<AccountID>(params);
343 if (!account)
344 {
345 return ledger_entry_helpers::invalidFieldError("malformedAddress", fieldName, "AccountID");
346 }
347
348 return keylet::did(*account).key;
349}
350
351static std::expected<UInt256, json::Value>
353 json::Value const& params,
354 json::StaticString const fieldName,
355 [[maybe_unused]] unsigned const apiVersion)
356{
357 if (!params.isObject())
358 {
359 return parseObjectID(params, fieldName);
360 }
361
362 if (params.isMember(jss::sub_index) &&
363 (!params[jss::sub_index].isConvertibleTo(json::ValueType::UInt) ||
364 params[jss::sub_index].isBool()))
365 {
367 "malformedRequest", jss::sub_index, "number");
368 }
369
370 if (params.isMember(jss::owner) == params.isMember(jss::dir_root))
371 {
373 "malformedRequest", "Must have exactly one of `owner` and `dir_root` fields.");
374 }
375
376 std::uint64_t const uSubIndex = params.get(jss::sub_index, 0).asUInt();
377
378 if (params.isMember(jss::dir_root))
379 {
380 if (auto const uDirRoot = ledger_entry_helpers::parse<UInt256>(params[jss::dir_root]))
381 {
382 return keylet::page(*uDirRoot, uSubIndex).key;
383 }
384
385 return ledger_entry_helpers::invalidFieldError("malformedDirRoot", jss::dir_root, "hash");
386 }
387
388 if (params.isMember(jss::owner))
389 {
390 auto const ownerID = ledger_entry_helpers::parse<AccountID>(params[jss::owner]);
391 if (!ownerID)
392 {
394 "malformedAddress", jss::owner, "AccountID");
395 }
396
397 return keylet::page(keylet::ownerDir(*ownerID), uSubIndex).key;
398 }
399
400 return ledger_entry_helpers::malformedError("malformedRequest", "");
401}
402
403static std::expected<UInt256, json::Value>
405 json::Value const& params,
406 json::StaticString const fieldName,
407 [[maybe_unused]] unsigned const apiVersion)
408{
409 if (!params.isObject())
410 {
411 return parseObjectID(params, fieldName);
412 }
413
414 auto const id = ledger_entry_helpers::requiredAccountID(params, jss::owner, "malformedOwner");
415 if (!id)
416 return std::unexpected(id.error());
417 auto const seq = ledger_entry_helpers::requiredUInt32(params, jss::seq, "malformedSeq");
418 if (!seq)
419 return std::unexpected(seq.error());
420
421 auto const seqProxy = SeqProxy::rawSequence(*seq);
422 return keylet::escrow(*id, seqProxy).key;
423}
424
426
427static std::expected<UInt256, json::Value>
429 Keylet const& keylet,
430 json::Value const& params,
431 json::StaticString const& fieldName,
432 [[maybe_unused]] unsigned const apiVersion)
433{
434 if (!params.isBool())
435 {
436 return parseObjectID(params, fieldName, "hex string");
437 }
438 if (!params.asBool())
439 {
440 return ledger_entry_helpers::invalidFieldError("invalidParams", fieldName, "true");
441 }
442
443 return keylet.key;
444}
445
446static std::expected<UInt256, json::Value>
448 json::Value const& params,
449 json::StaticString const fieldName,
450 unsigned const apiVersion)
451{
452 if (params.isUInt() || params.isInt())
453 {
454 // If the index doesn't parse as a UInt, throw
455 auto const index = params.asUInt();
456
457 // Return the "long" skip list for the given ledger index.
458 auto const keylet = keylet::skip(index);
459 return keylet.key;
460 }
461 // Return the key in `params` or the "short" skip list, which contains
462 // hashes since the last flag ledger.
463 return parseFixed(keylet::skip(), params, fieldName, apiVersion);
464}
465
466static std::expected<UInt256, json::Value>
468 json::Value const& params,
469 json::StaticString const fieldName,
470 [[maybe_unused]] unsigned const apiVersion)
471{
472 if (!params.isObject())
473 {
474 return parseObjectID(params, fieldName, "hex string");
475 }
476
477 auto const id = ledger_entry_helpers::requiredAccountID(params, jss::owner, "malformedOwner");
478 if (!id)
479 return std::unexpected(id.error());
480 auto const seq = ledger_entry_helpers::requiredUInt32(params, jss::seq, "malformedSeq");
481 if (!seq)
482 return std::unexpected(seq.error());
483
484 auto const seqProxy = SeqProxy::rawSequence(*seq);
485 return keylet::loanBroker(*id, seqProxy).key;
486}
487
488static std::expected<UInt256, json::Value>
490 json::Value const& params,
491 json::StaticString const fieldName,
492 [[maybe_unused]] unsigned const apiVersion)
493{
494 if (!params.isObject())
495 {
496 return parseObjectID(params, fieldName, "hex string");
497 }
498
499 auto const id =
500 ledger_entry_helpers::requiredUInt256(params, jss::loan_broker_id, "malformedBroker");
501 if (!id)
502 return std::unexpected(id.error());
503 auto const seq = ledger_entry_helpers::requiredUInt32(params, jss::loan_seq, "malformedSeq");
504 if (!seq)
505 return std::unexpected(seq.error());
506
507 auto const seqProxy = SeqProxy::rawSequence(*seq);
508 return keylet::loan(*id, seqProxy).key;
509}
510
511static std::expected<UInt256, json::Value>
513 json::Value const& params,
514 json::StaticString const fieldName,
515 [[maybe_unused]] unsigned const apiVersion)
516{
517 if (!params.isObject())
518 {
519 return parseObjectID(params, fieldName);
520 }
521
522 auto const mptIssuanceID = ledger_entry_helpers::requiredUInt192(
523 params, jss::mpt_issuance_id, "malformedMPTIssuanceID");
524 if (!mptIssuanceID)
525 return std::unexpected(mptIssuanceID.error());
526
527 auto const account =
528 ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAccount");
529 if (!account)
530 return std::unexpected(account.error());
531
532 return keylet::mptoken(*mptIssuanceID, *account).key;
533}
534
535static std::expected<UInt256, json::Value>
537 json::Value const& params,
538 json::StaticString const fieldName,
539 [[maybe_unused]] unsigned const apiVersion)
540{
541 auto const mptIssuanceID = ledger_entry_helpers::parse<UInt192>(params);
542 if (!mptIssuanceID)
543 {
545 "malformedMPTokenIssuance", fieldName, "Hash192");
546 }
547
548 return keylet::mptokenIssuance(*mptIssuanceID).key;
549}
550
551static std::expected<UInt256, json::Value>
553 json::Value const& params,
554 json::StaticString const fieldName,
555 [[maybe_unused]] unsigned const apiVersion)
556{
557 if (!params.isObject())
558 {
559 return parseObjectID(params, fieldName);
560 }
561
562 auto const owner =
563 ledger_entry_helpers::requiredAccountID(params, jss::owner, "malformedOwner");
564 if (!owner)
565 return std::unexpected(owner.error());
566
567 auto const sequence =
568 ledger_entry_helpers::requiredUInt32(params, jss::seq, "malformedRequest");
569 if (!sequence)
570 return std::unexpected(sequence.error());
571
572 auto const seqProxy = SeqProxy::rawSequence(*sequence);
573 return keylet::nftokenOffer(*owner, seqProxy).key;
574}
575
576static std::expected<UInt256, json::Value>
578 json::Value const& params,
579 json::StaticString const fieldName,
580 [[maybe_unused]] unsigned const apiVersion)
581{
582 return parseObjectID(params, fieldName, "hex string");
583}
584
586
587static std::expected<UInt256, json::Value>
589 json::Value const& params,
590 json::StaticString const fieldName,
591 [[maybe_unused]] unsigned const apiVersion)
592{
593 if (!params.isObject())
594 {
595 return parseObjectID(params, fieldName);
596 }
597
598 auto const id =
599 ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAddress");
600 if (!id)
601 return std::unexpected(id.error());
602
603 auto const seq = ledger_entry_helpers::requiredUInt32(params, jss::seq, "malformedRequest");
604 if (!seq)
605 return std::unexpected(seq.error());
606
607 auto const seqProxy = SeqProxy::rawSequence(*seq);
608 return keylet::offer(*id, seqProxy).key;
609}
610
611static std::expected<UInt256, json::Value>
613 json::Value const& params,
614 json::StaticString const fieldName,
615 [[maybe_unused]] unsigned const apiVersion)
616{
617 if (!params.isObject())
618 {
619 return parseObjectID(params, fieldName);
620 }
621
622 auto const id =
623 ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAccount");
624 if (!id)
625 return std::unexpected(id.error());
626
628 params, jss::oracle_document_id, "malformedDocumentID");
629 if (!seq)
630 return std::unexpected(seq.error());
631
632 return keylet::oracle(*id, *seq).key;
633}
634
635static std::expected<UInt256, json::Value>
637 json::Value const& params,
638 json::StaticString const fieldName,
639 [[maybe_unused]] unsigned const apiVersion)
640{
641 if (!params.isObject())
642 {
643 return parseObjectID(params, fieldName);
644 }
645
646 auto const account =
647 ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAddress");
648 if (!account)
649 return std::unexpected(account.error());
650
651 auto const destination =
652 ledger_entry_helpers::requiredAccountID(params, jss::destination, "malformedAddress");
653 if (!destination)
654 return std::unexpected(destination.error());
655
656 auto const sequence =
657 ledger_entry_helpers::requiredUInt32(params, jss::seq, "malformedRequest");
658 if (!sequence)
659 return std::unexpected(sequence.error());
660
661 auto const seqProxy = SeqProxy::rawSequence(*sequence);
662 return keylet::payChannel(*account, *destination, seqProxy).key;
663}
664
665static std::expected<UInt256, json::Value>
667 json::Value const& pd,
668 json::StaticString const fieldName,
669 [[maybe_unused]] unsigned const apiVersion)
670{
671 if (pd.isString())
672 {
673 return parseObjectID(pd, fieldName);
674 }
675
676 if (!pd.isObject())
677 {
679 "malformedRequest", fieldName, "hex string or object");
680 }
681
682 auto const account =
683 ledger_entry_helpers::requiredAccountID(pd, jss::account, "malformedAddress");
684 if (!account)
685 return std::unexpected(account.error());
686
687 auto const seq = ledger_entry_helpers::requiredUInt32(pd, jss::seq, "malformedRequest");
688 if (!seq)
689 return std::unexpected(seq.error());
690
691 auto const seqProxy = SeqProxy::rawSequence(pd[jss::seq].asUInt());
692 return keylet::permissionedDomain(*account, seqProxy).key;
693}
694
695static std::expected<UInt256, json::Value>
697 json::Value const& jvRippleState,
698 json::StaticString const fieldName,
699 [[maybe_unused]] unsigned const apiVersion)
700{
701 Currency uCurrency;
702
703 if (!jvRippleState.isObject())
704 {
705 return parseObjectID(jvRippleState, fieldName);
706 }
707
708 if (auto const value =
709 ledger_entry_helpers::hasRequired(jvRippleState, {jss::currency, jss::accounts});
710 !value)
711 {
712 return std::unexpected(value.error());
713 }
714
715 if (!jvRippleState[jss::accounts].isArray() || jvRippleState[jss::accounts].size() != 2)
716 {
718 "malformedRequest", jss::accounts, "length-2 array of Accounts");
719 }
720
721 auto const id1 = ledger_entry_helpers::parse<AccountID>(jvRippleState[jss::accounts][0u]);
722 auto const id2 = ledger_entry_helpers::parse<AccountID>(jvRippleState[jss::accounts][1u]);
723 if (!id1 || !id2)
724 {
726 "malformedAddress", jss::accounts, "array of Accounts");
727 }
728 if (id1 == id2)
729 {
731 "malformedRequest", "Cannot have a trustline to self.");
732 }
733
734 if (!jvRippleState[jss::currency].isString() || jvRippleState[jss::currency] == "" ||
735 !toCurrency(uCurrency, jvRippleState[jss::currency].asString()))
736 {
738 "malformedCurrency", jss::currency, "Currency");
739 }
740
741 return keylet::trustLine(*id1, *id2, uCurrency).key;
742}
743
744static std::expected<UInt256, json::Value>
746 json::Value const& params,
747 json::StaticString const fieldName,
748 [[maybe_unused]] unsigned const apiVersion)
749{
750 if (!params.isObject())
751 {
752 return parseObjectID(params, fieldName);
753 }
754
755 auto const id =
756 ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAddress");
757 if (!id)
758 return std::unexpected(id.error());
759
760 return keylet::signerList(*id).key;
761}
762
763static std::expected<UInt256, json::Value>
765 json::Value const& params,
766 json::StaticString const fieldName,
767 [[maybe_unused]] unsigned const apiVersion)
768{
769 if (!params.isObject())
770 return parseObjectID(params, fieldName);
771
772 auto const sponsorID =
773 ledger_entry_helpers::requiredAccountID(params, jss::sponsor, "malformedSponsor");
774 if (!sponsorID)
775 return std::unexpected(sponsorID.error());
776
777 auto const sponseeID =
778 ledger_entry_helpers::requiredAccountID(params, jss::sponsee, "malformedSponsee");
779 if (!sponseeID)
780 return std::unexpected(sponseeID.error());
781
782 return keylet::sponsorship(*sponsorID, *sponseeID).key;
783}
784
785static std::expected<UInt256, json::Value>
787 json::Value const& params,
788 json::StaticString const fieldName,
789 [[maybe_unused]] unsigned const apiVersion)
790{
791 // In the non-object branch the caller must supply the proposal ID as a
792 // hex string (the object form is the {account, ticket_seq} pair handled
793 // below). Passing "hex string" here — rather than the more general
794 // "hex string or object" default — makes the error message name the
795 // exact form still on the table once an object has been ruled out.
796 if (!params.isObject())
797 return parseObjectID(params, fieldName, "hex string");
798
799 auto const targetID =
800 ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAddress");
801 if (!targetID)
802 return std::unexpected(targetID.error());
803
804 // The proposed transaction's TicketSequence (a proposed transaction is
805 // ticket-only), mirroring how parseTicket looks up a Ticket object.
806 auto const ticketSequence =
807 ledger_entry_helpers::requiredUInt32(params, jss::ticket_seq, "malformedRequest");
808 if (!ticketSequence)
809 return std::unexpected(ticketSequence.error());
810
811 return keylet::txProposal(*targetID, *ticketSequence).key;
812}
813
814static std::expected<UInt256, json::Value>
816 json::Value const& params,
817 json::StaticString const fieldName,
818 [[maybe_unused]] unsigned const apiVersion)
819{
820 if (!params.isObject())
821 {
822 return parseObjectID(params, fieldName);
823 }
824
825 auto const id =
826 ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAddress");
827 if (!id)
828 return std::unexpected(id.error());
829
830 auto const seq =
831 ledger_entry_helpers::requiredUInt32(params, jss::ticket_seq, "malformedRequest");
832 if (!seq)
833 return std::unexpected(seq.error());
834
835 auto const seqProxy = SeqProxy::rawTicket(*seq);
836 return keylet::ticket(*id, seqProxy).key;
837}
838
839static std::expected<UInt256, json::Value>
841 json::Value const& params,
842 json::StaticString const fieldName,
843 [[maybe_unused]] unsigned const apiVersion)
844{
845 if (!params.isObject())
846 {
847 return parseObjectID(params, fieldName);
848 }
849
850 auto const id = ledger_entry_helpers::requiredAccountID(params, jss::owner, "malformedOwner");
851 if (!id)
852 return std::unexpected(id.error());
853
854 auto const seq = ledger_entry_helpers::requiredUInt32(params, jss::seq, "malformedRequest");
855 if (!seq)
856 return std::unexpected(seq.error());
857
858 auto const seqProxy = SeqProxy::rawSequence(*seq);
859 return keylet::vault(*id, seqProxy).key;
860}
861
868
869// {
870// ledger_hash : <ledger>
871// ledger_index : <ledger_index>
872// ...
873// }
876{
877 static auto const kLedgerEntryParsers = std::to_array<LedgerEntry>({
878#pragma push_macro("LEDGER_ENTRY")
879#undef LEDGER_ENTRY
880
881#define LEDGER_ENTRY(tag, value, name, rpcName, fields) {jss::rpcName, parse##name, tag},
882
883#include <xrpl/protocol/detail/ledger_entries.macro>
884
885#undef LEDGER_ENTRY
886#pragma pop_macro("LEDGER_ENTRY")
887 {.fieldName = jss::index, .parseFunction = parseIndex, .expectedType = ltANY},
888 // aliases
889 {
890 .fieldName = jss::account_root,
891 .parseFunction = parseAccountRoot,
892 .expectedType = ltACCOUNT_ROOT,
893 },
894 {
895 .fieldName = jss::ripple_state,
896 .parseFunction = parseRippleState,
897 .expectedType = ltRIPPLE_STATE,
898 },
899 });
900
901 auto const hasMoreThanOneMember = [&] {
902 int count = 0;
903
904 for (auto const& ledgerEntry : kLedgerEntryParsers)
905 {
906 if (context.params.isMember(ledgerEntry.fieldName))
907 {
908 count++;
909 if (count > 1) // Early exit if more than one is found
910 return true;
911 }
912 }
913 return false; // Return false if <= 1 is found
914 }();
915
916 if (hasMoreThanOneMember)
917 {
918 return rpc::makeParamError("Too many fields provided.");
919 }
920
922 auto jvResult = rpc::lookupLedger(lpLedger, context);
923
924 if (!lpLedger)
925 return jvResult;
926
927 UInt256 uNodeIndex;
928 LedgerEntryType expectedType = ltANY;
929
930 try
931 {
932 bool found = false;
933 for (auto const& ledgerEntry : kLedgerEntryParsers)
934 {
935 if (context.params.isMember(ledgerEntry.fieldName))
936 {
937 expectedType = ledgerEntry.expectedType;
938 auto const result = ledgerEntry.parseFunction(
939 context.params[ledgerEntry.fieldName],
940 ledgerEntry.fieldName,
941 context.apiVersion);
942 if (!result)
943 return result.error();
944
945 uNodeIndex = result.value();
946 found = true;
947 break;
948 }
949 }
950 if (!found)
951 {
952 if (context.apiVersion < 2u)
953 {
954 jvResult[jss::error] = "unknownOption";
955 return jvResult;
956 }
957 return rpc::makeParamError("No ledger_entry params provided.");
958 }
959 }
960 catch (json::Error const& e)
961 {
962 if (context.apiVersion > 1u)
963 {
964 // For apiVersion 2 onwards, any parsing failures that throw
965 // this exception return an invalidParam error.
967 }
968
969 throw;
970 }
971
972 // Return the computed index regardless of whether the node exists.
973 jvResult[jss::index] = to_string(uNodeIndex);
974
975 if (uNodeIndex.isZero())
976 {
978 return jvResult;
979 }
980
981 auto const sleNode = lpLedger->read(keylet::unchecked(uNodeIndex));
982
983 bool bNodeBinary = false;
984 if (context.params.isMember(jss::binary))
985 bNodeBinary = context.params[jss::binary].asBool();
986
987 if (!sleNode)
988 {
989 // Not found.
991 return jvResult;
992 }
993
994 if ((expectedType != ltANY) && (expectedType != sleNode->getType()))
995 {
997 return jvResult;
998 }
999
1000 if (bNodeBinary)
1001 {
1002 Serializer s;
1003
1004 sleNode->add(s);
1005
1006 jvResult[jss::node_binary] = strHex(s.peekData());
1007 }
1008 else
1009 {
1010 jvResult[jss::node] = sleNode->getJson(JsonOptions::Values::None);
1011 }
1012
1013 return jvResult;
1014}
1015
1018{
1019 org::xrpl::rpc::v1::GetLedgerEntryRequest const& request = context.params;
1020 org::xrpl::rpc::v1::GetLedgerEntryResponse response;
1021 grpc::Status const status = grpc::Status::OK;
1022
1024 if (auto const status = rpc::ledgerFromRequest(ledger, context))
1025 {
1026 grpc::Status errorStatus;
1027 if (status == RpcInvalidParams)
1028 {
1029 errorStatus = grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, status.message);
1030 }
1031 else
1032 {
1033 errorStatus = grpc::Status(grpc::StatusCode::NOT_FOUND, status.message);
1034 }
1035 return {response, errorStatus};
1036 }
1037
1038 auto const key = UInt256::fromVoidChecked(request.key());
1039 if (!key)
1040 {
1041 grpc::Status const errorStatus{grpc::StatusCode::INVALID_ARGUMENT, "index malformed"};
1042 return {response, errorStatus};
1043 }
1044
1045 auto const sleNode = ledger->read(keylet::unchecked(*key));
1046 if (!sleNode)
1047 {
1048 grpc::Status const errorStatus{grpc::StatusCode::NOT_FOUND, "object not found"};
1049 return {response, errorStatus};
1050 }
1051
1052 Serializer s;
1053 sleNode->add(s);
1054
1055 auto& stateObject = *response.mutable_ledger_object();
1056 stateObject.set_data(s.peekData().data(), s.getLength());
1057 stateObject.set_key(request.key());
1058 *response.mutable_ledger() = request.ledger();
1059 return {response, status};
1060}
1061} // namespace xrpl
Lightweight wrapper to tag static string.
Definition json_value.h:52
Represents a JSON value.
Definition json_value.h:121
bool isObject() const
bool asBool() const
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
bool isBool() const
bool isArray() const
bool isString() const
UInt size() const
Number of values in array or object.
bool isUInt() const
bool isInt() const
UInt asUInt() const
bool isConvertibleTo(ValueType other) 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.
bool isZero() const
Definition base_uint.h:563
static std::optional< BaseUInt > fromVoidChecked(T const &from)
Definition base_uint.h:347
void pushBack(STObject const &object)
Definition STArray.h:212
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:79
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
static constexpr SeqProxy rawTicket(std::uint32_t v)
Factory function to return a ticket-based SeqProxy.
Definition SeqProxy.h:74
Blob const & peekData() const
Definition Serializer.h:273
int getLength() const
Definition Serializer.h:304
An immutable linear range of bytes.
Definition Slice.h:28
T data(T... args)
@ UInt
unsigned integer value
Definition json_value.h:28
std::set< std::pair< AccountID, Slice > > makeSorted(STArray const &credentials)
Keylet computation functions.
Definition Indexes.h:39
Keylet const & skip() noexcept
The index of the "short" skip list.
Definition Indexes.cpp:245
Keylet nftokenOffer(AccountID const &owner, SeqProxy const &seq)
An offer from an account to buy or sell an NFT.
Definition Indexes.cpp:465
Keylet oracle(AccountID const &account, std::uint32_t const documentID) noexcept
Definition Indexes.cpp:535
Keylet payChannel(AccountID const &src, AccountID const &dst, SeqProxy const &seq) noexcept
A PaymentChannel.
Definition Indexes.cpp:436
Keylet const & negativeUNL() noexcept
The (fixed) index of the object containing the ledger negativeUNL.
Definition Indexes.cpp:274
Keylet escrow(AccountID const &src, SeqProxy const &seq) noexcept
An escrow entry.
Definition Indexes.cpp:430
Keylet did(AccountID const &account) noexcept
Definition Indexes.cpp:529
Keylet offer(AccountID const &id, SeqProxy const &seq) noexcept
An offer from an account.
Definition Indexes.cpp:310
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Definition Indexes.cpp:386
Keylet const & feeSettings() noexcept
The (fixed) index of the object containing the ledger fees.
Definition Indexes.cpp:267
Keylet const & amendments() noexcept
The index of the amendment table.
Definition Indexes.cpp:260
Keylet signerList(AccountID const &account) noexcept
A SignerList.
Definition Indexes.cpp:360
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:415
Keylet txProposal(AccountID const &target, std::uint32_t ticketSequence) noexcept
A TransactionProposal.
Definition Indexes.cpp:378
Keylet check(AccountID const &id, SeqProxy const &seq) noexcept
A Check.
Definition Indexes.cpp:372
Keylet amm(Asset const &issue1, Asset const &issue2) noexcept
AMM entry.
Definition Indexes.cpp:483
Keylet unchecked(UInt256 const &key) noexcept
Any ledger entry.
Definition Indexes.cpp:409
Keylet ticket(AccountID const &id, SeqProxy const &ticketSeq)
A ticket belonging to an account.
Definition Indexes.cpp:344
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:565
Keylet permissionedDomain(AccountID const &account, SeqProxy const &seq) noexcept
Definition Indexes.cpp:583
Keylet page(UInt256 const &root, std::uint64_t const index=0) noexcept
A page in a directory.
Definition Indexes.cpp:421
Keylet mptoken(MPTID const &issuanceID, AccountID const &holder) noexcept
Definition Indexes.cpp:547
Keylet delegate(AccountID const &account, AccountID const &authorizedAccount) noexcept
A keylet for Delegate object.
Definition Indexes.cpp:523
Keylet loanBroker(AccountID const &owner, SeqProxy const &seq) noexcept
Definition Indexes.cpp:571
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Keylet sponsorship(AccountID const &sponsor, AccountID const &sponsee) noexcept
A Sponsorship.
Definition Indexes.cpp:366
Keylet loan(UInt256 const &loanBrokerID, SeqProxy const &loanSeq) noexcept
Definition Indexes.cpp:577
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Definition Indexes.cpp:559
Keylet mptokenIssuance(MPTID const &issuanceID) noexcept
Definition Indexes.cpp:541
Keylet trustLine(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:287
std::optional< T > parse(json::Value const &param)
std::expected< AccountID, json::Value > requiredAccountID(json::Value const &params, json::StaticString const fieldName, std::string const &err)
std::expected< UInt192, json::Value > requiredUInt192(json::Value const &params, json::StaticString const fieldName, std::string const &err)
std::expected< Asset, json::Value > requiredAsset(json::Value const &params, json::StaticString const fieldName, std::string const &err)
std::expected< Blob, json::Value > requiredHexBlob(json::Value const &params, json::StaticString const fieldName, std::size_t maxLength, std::string const &err)
std::unexpected< json::Value > malformedError(std::string const &err, std::string const &message)
std::expected< std::uint32_t, json::Value > requiredUInt32(json::Value const &params, json::StaticString const fieldName, std::string const &err)
std::expected< UInt256, json::Value > requiredUInt256(json::Value const &params, json::StaticString const fieldName, std::string const &err)
std::unexpected< json::Value > invalidFieldError(std::string const &err, json::StaticString const field, std::string const &type)
std::expected< bool, json::Value > hasRequired(json::Value const &params, std::initializer_list< json::StaticString > fields, std::optional< std::string > err=std::nullopt)
::rpc::Status ledgerFromRequest(T &ledger, GRPCContext< R > const &context)
Retrieves a ledger from a gRPC request context.
json::Value makeParamError(std::string const &message)
Returns a new json object that indicates invalid parameters.
Definition ErrorCodes.h:231
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
::rpc::Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext const &context, json::Value &result)
Looks up a ledger from a request and fills a json::Value with ledger data.
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::function< std::expected< UInt256, json::Value >( json::Value const &, json::StaticString const, unsigned const apiVersion)> FunctionType
@ RpcEntryNotFound
Definition ErrorCodes.h:144
@ RpcUnexpectedLedgerType
Definition ErrorCodes.h:145
@ RpcInvalidParams
Definition ErrorCodes.h:67
static std::expected< STArray, json::Value > parseAuthorizeCredentials(json::Value const &jv)
static std::expected< UInt256, json::Value > parseFixed(Keylet const &keylet, json::Value const &params, json::StaticString const &fieldName, unsigned const apiVersion)
auto const parseFeeSettings
std::pair< org::xrpl::rpc::v1::GetLedgerEntryResponse, grpc::Status > doLedgerEntryGrpc(rpc::GRPCContext< org::xrpl::rpc::v1::GetLedgerEntryRequest > const &context)
static std::expected< UInt256, json::Value > parseEscrow(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
constexpr std::size_t kMaxCredentialTypeLength
The maximum length of a CredentialType inside a Credential.
Definition Protocol.h:276
static std::expected< UInt256, json::Value > parseCredential(json::Value const &cred, json::StaticString const fieldName, unsigned const apiVersion)
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:13
static std::expected< UInt256, json::Value > parseSponsorship(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseLoan(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseMPToken(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseRippleState(json::Value const &jvRippleState, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseIndex(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseTransactionProposal(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
static bool authorized(Port const &port, std::map< std::string, std::string > const &h)
bool toCurrency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:65
static std::expected< UInt256, json::Value > parseLoanBroker(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseDirectoryNode(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static FunctionType fixed(Keylet const &keylet)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
auto const parseAmendments
static std::expected< UInt256, json::Value > parseTicket(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
BaseUInt< 256 > UInt256
Definition base_uint.h:581
static std::expected< UInt256, json::Value > parseOracle(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
constexpr std::size_t kMaxCredentialsArraySize
The maximum number of credentials can be passed in array.
Definition Protocol.h:281
static std::expected< UInt256, json::Value > parseAccountRoot(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parsePermissionedDomain(json::Value const &pd, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseLedgerHashes(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
auto const parseNegativeUNL
static std::expected< UInt256, json::Value > parseCheck(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
json::Value doLedgerEntry(rpc::JsonContext &)
static std::expected< UInt256, json::Value > parseOffer(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseSignerList(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseVault(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseMPTokenIssuance(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseNFTokenOffer(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
LedgerEntryType
Identifiers for on-ledger objects.
@ ltANY
A special type, matching any ledger entry type.
static std::expected< UInt256, json::Value > parseDID(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parsePayChannel(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseDepositPreauth(json::Value const &dp, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseNFTokenPage(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseObjectID(json::Value const &params, json::StaticString const fieldName, std::string const &expectedType="hex string or object")
static std::expected< UInt256, json::Value > parseAMM(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
static std::expected< UInt256, json::Value > parseDelegate(json::Value const &params, json::StaticString const fieldName, unsigned const apiVersion)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
UInt256 key
Definition Keylet.h:21
json::StaticString fieldName
FunctionType parseFunction
LedgerEntryType expectedType
unsigned int apiVersion
Definition Context.h:37
RequestType params
Definition Context.h:59
json::Value params
Definition Context.h:51
T unexpected(T... args)