Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
LedgerUtils.hpp
1#pragma once
2
3#include "rpc/JS.hpp"
4
5#include <fmt/format.h>
6#include <xrpl/basics/Slice.h>
7#include <xrpl/basics/StringUtilities.h>
8#include <xrpl/basics/strHex.h>
9#include <xrpl/protocol/LedgerFormats.h>
10#include <xrpl/protocol/LedgerHeader.h>
11#include <xrpl/protocol/jss.h>
12
13#include <algorithm>
14#include <array>
15#include <functional>
16#include <optional>
17#include <string>
18#include <unordered_set>
19#include <vector>
20
21namespace util {
22
23class LedgerTypes;
24
25namespace impl {
26
27class LedgerTypeAttribute {
28 enum class LedgerCategory {
29 Invalid,
30 AccountOwned, // The ledger object is owned by account
31 Chain, // The ledger object is shared across the chain
32 DeletionBlocker // The ledger object is owned by account and it blocks deletion
33 };
34
35 xrpl::LedgerEntryType type_ = xrpl::ltANY;
36 char const* name_ = nullptr;
37 char const* rpcName_ = nullptr;
38 LedgerCategory category_ = LedgerCategory::Invalid;
39
40 constexpr LedgerTypeAttribute(
41 char const* name,
42 char const* rpcName,
43 xrpl::LedgerEntryType type,
44 LedgerCategory category
45 )
46 : type_{type}, name_{name}, rpcName_{rpcName}, category_{category}
47 {
48 }
49
50public:
51 static constexpr LedgerTypeAttribute
52 chainLedgerType(char const* name, char const* rpcName, xrpl::LedgerEntryType type)
53 {
54 return LedgerTypeAttribute(name, rpcName, type, LedgerCategory::Chain);
55 }
56
57 static constexpr LedgerTypeAttribute
58 accountOwnedLedgerType(char const* name, char const* rpcName, xrpl::LedgerEntryType type)
59 {
60 return LedgerTypeAttribute(name, rpcName, type, LedgerCategory::AccountOwned);
61 }
62
63 static constexpr LedgerTypeAttribute
64 deletionBlockerLedgerType(char const* name, char const* rpcName, xrpl::LedgerEntryType type)
65 {
66 return LedgerTypeAttribute(name, rpcName, type, LedgerCategory::DeletionBlocker);
67 }
68 friend class util::LedgerTypes;
69};
70
71} // namespace impl
72
78 using LedgerTypeAttribute = impl::LedgerTypeAttribute;
79 using LedgerTypeAttributeList = LedgerTypeAttribute[];
80
81 static constexpr LedgerTypeAttributeList const kLedgerTypes{
82 LedgerTypeAttribute::accountOwnedLedgerType(
83 JS(AccountRoot),
84 JS(account),
85 xrpl::ltACCOUNT_ROOT
86 ),
87 LedgerTypeAttribute::chainLedgerType(JS(Amendments), JS(amendments), xrpl::ltAMENDMENTS),
88 LedgerTypeAttribute::deletionBlockerLedgerType(JS(Check), JS(check), xrpl::ltCHECK),
89 LedgerTypeAttribute::accountOwnedLedgerType(
90 JS(DepositPreauth),
91 JS(deposit_preauth),
92 xrpl::ltDEPOSIT_PREAUTH
93 ),
94 // dir node belongs to account, but can not be filtered from account_objects
95 LedgerTypeAttribute::chainLedgerType(JS(DirectoryNode), JS(directory), xrpl::ltDIR_NODE),
96 LedgerTypeAttribute::deletionBlockerLedgerType(JS(Escrow), JS(escrow), xrpl::ltESCROW),
97 LedgerTypeAttribute::chainLedgerType(JS(FeeSettings), JS(fee), xrpl::ltFEE_SETTINGS),
98 LedgerTypeAttribute::chainLedgerType(JS(LedgerHashes), JS(hashes), xrpl::ltLEDGER_HASHES),
99 LedgerTypeAttribute::accountOwnedLedgerType(JS(Offer), JS(offer), xrpl::ltOFFER),
100 LedgerTypeAttribute::deletionBlockerLedgerType(
101 JS(PayChannel),
102 JS(payment_channel),
103 xrpl::ltPAYCHAN
104 ),
105 LedgerTypeAttribute::accountOwnedLedgerType(
106 JS(SignerList),
107 JS(signer_list),
108 xrpl::ltSIGNER_LIST
109 ),
110 LedgerTypeAttribute::deletionBlockerLedgerType(
111 JS(RippleState),
112 JS(state),
113 xrpl::ltRIPPLE_STATE
114 ),
115 LedgerTypeAttribute::accountOwnedLedgerType(JS(Ticket), JS(ticket), xrpl::ltTICKET),
116 LedgerTypeAttribute::accountOwnedLedgerType(
117 JS(NFTokenOffer),
118 JS(nft_offer),
119 xrpl::ltNFTOKEN_OFFER
120 ),
121 LedgerTypeAttribute::deletionBlockerLedgerType(
122 JS(NFTokenPage),
123 JS(nft_page),
124 xrpl::ltNFTOKEN_PAGE
125 ),
126 LedgerTypeAttribute::accountOwnedLedgerType(JS(AMM), JS(amm), xrpl::ltAMM),
127 LedgerTypeAttribute::deletionBlockerLedgerType(JS(Bridge), JS(bridge), xrpl::ltBRIDGE),
128 LedgerTypeAttribute::deletionBlockerLedgerType(
129 JS(XChainOwnedClaimID),
130 JS(xchain_owned_claim_id),
131 xrpl::ltXCHAIN_OWNED_CLAIM_ID
132 ),
133 LedgerTypeAttribute::deletionBlockerLedgerType(
134 JS(XChainOwnedCreateAccountClaimID),
135 JS(xchain_owned_create_account_claim_id),
136 xrpl::ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID
137 ),
138 LedgerTypeAttribute::accountOwnedLedgerType(JS(DID), JS(did), xrpl::ltDID),
139 LedgerTypeAttribute::accountOwnedLedgerType(JS(Oracle), JS(oracle), xrpl::ltORACLE),
140 LedgerTypeAttribute::accountOwnedLedgerType(
141 JS(Credential),
142 JS(credential),
143 xrpl::ltCREDENTIAL
144 ),
145 LedgerTypeAttribute::accountOwnedLedgerType(JS(Vault), JS(vault), xrpl::ltVAULT),
146 // loan broker is a pseudo-account object, like AMM and Vault
147 LedgerTypeAttribute::accountOwnedLedgerType(
148 JS(LoanBroker),
149 JS(loan_broker),
150 xrpl::ltLOAN_BROKER
151 ),
152 LedgerTypeAttribute::deletionBlockerLedgerType(JS(Loan), JS(loan), xrpl::ltLOAN),
153 LedgerTypeAttribute::chainLedgerType(JS(NegativeUNL), JS(nunl), xrpl::ltNEGATIVE_UNL),
154 LedgerTypeAttribute::deletionBlockerLedgerType(
155 JS(MPTokenIssuance),
156 JS(mpt_issuance),
157 xrpl::ltMPTOKEN_ISSUANCE
158 ),
159 LedgerTypeAttribute::deletionBlockerLedgerType(JS(MPToken), JS(mptoken), xrpl::ltMPTOKEN),
160 LedgerTypeAttribute::deletionBlockerLedgerType(
161 JS(PermissionedDomain),
162 JS(permissioned_domain),
163 xrpl::ltPERMISSIONED_DOMAIN
164 ),
165 LedgerTypeAttribute::accountOwnedLedgerType(JS(Delegate), JS(delegate), xrpl::ltDELEGATE),
166 };
167
168public:
173 static constexpr auto
175 {
176 std::array<char const*, std::size(kLedgerTypes)> res{};
177 std::ranges::transform(kLedgerTypes, std::begin(res), [](auto const& item) {
178 return item.rpcName_;
179 });
180 return res;
181 }
182
188 static constexpr auto
190 {
191 constexpr auto kFilter = [](auto const& item) {
192 return item.category_ == LedgerTypeAttribute::LedgerCategory::DeletionBlocker;
193 };
194
195 constexpr auto kDeletionBlockersCount =
196 std::count_if(std::begin(kLedgerTypes), std::end(kLedgerTypes), kFilter);
197 std::array<xrpl::LedgerEntryType, kDeletionBlockersCount> res{};
198 auto it = std::begin(res);
199 std::ranges::for_each(kLedgerTypes, [&](auto const& item) {
200 if (kFilter(item)) {
201 *it = item.type_;
202 ++it;
203 }
204 });
205 return res;
206 }
207
215 static xrpl::LedgerEntryType
216 getLedgerEntryTypeFromStr(std::string const& entryName);
217
225 static xrpl::LedgerEntryType
226 getAccountOwnedLedgerTypeFromStr(std::string const& entryName);
227
228private:
229 static std::optional<std::reference_wrapper<impl::LedgerTypeAttribute const>>
230 getLedgerTypeAttributeFromStr(std::string const& entryName);
231};
232
239inline xrpl::LedgerHeader
241{
242 return xrpl::deserializeHeader(data, /* hasHash = */ true);
243}
244
251inline std::string
252toString(xrpl::LedgerHeader const& info)
253{
254 return fmt::format(
255 "LedgerHeader {{Sequence: {}, Hash: {}, TxHash: {}, AccountHash: {}, ParentHash: {}}}",
256 info.seq,
257 xrpl::strHex(info.hash),
258 strHex(info.txHash),
259 xrpl::strHex(info.accountHash),
260 strHex(info.parentHash)
261 );
262}
263
264} // namespace util
A helper class that provides lists of different ledger type category.
Definition LedgerUtils.hpp:77
static constexpr auto getDeletionBlockerLedgerTypes()
Returns a list of all account deletion blocker's type as string.
Definition LedgerUtils.hpp:189
static xrpl::LedgerEntryType getLedgerEntryTypeFromStr(std::string const &entryName)
Returns the xrpl::LedgerEntryType from the given string.
Definition LedgerUtils.cpp:16
static constexpr auto getLedgerEntryTypeStrList()
Returns a list of all ledger entry type as string.
Definition LedgerUtils.hpp:174
static xrpl::LedgerEntryType getAccountOwnedLedgerTypeFromStr(std::string const &entryName)
Returns the xrpl::LedgerEntryType from the given string.
Definition LedgerUtils.cpp:25
Definition LedgerUtils.hpp:27
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:56
This namespace contains various utilities.
Definition AccountUtils.hpp:11
std::string toString(xrpl::LedgerHeader const &info)
A helper function that converts a xrpl::LedgerHeader to a string representation.
Definition LedgerUtils.hpp:252
xrpl::LedgerHeader deserializeHeader(xrpl::Slice data)
Deserializes a xrpl::LedgerHeader from xrpl::Slice of data.
Definition LedgerUtils.hpp:240