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 ripple::LedgerEntryType type_ = ripple::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 ripple::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, ripple::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, ripple::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, ripple::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 kLEDGER_TYPES{
82 LedgerTypeAttribute::accountOwnedLedgerType(
83 JS(AccountRoot),
84 JS(account),
85 ripple::ltACCOUNT_ROOT
86 ),
87 LedgerTypeAttribute::chainLedgerType(JS(Amendments), JS(amendments), ripple::ltAMENDMENTS),
88 LedgerTypeAttribute::deletionBlockerLedgerType(JS(Check), JS(check), ripple::ltCHECK),
89 LedgerTypeAttribute::accountOwnedLedgerType(
90 JS(DepositPreauth),
91 JS(deposit_preauth),
92 ripple::ltDEPOSIT_PREAUTH
93 ),
94 // dir node belongs to account, but can not be filtered from account_objects
95 LedgerTypeAttribute::chainLedgerType(JS(DirectoryNode), JS(directory), ripple::ltDIR_NODE),
96 LedgerTypeAttribute::deletionBlockerLedgerType(JS(Escrow), JS(escrow), ripple::ltESCROW),
97 LedgerTypeAttribute::chainLedgerType(JS(FeeSettings), JS(fee), ripple::ltFEE_SETTINGS),
98 LedgerTypeAttribute::chainLedgerType(JS(LedgerHashes), JS(hashes), ripple::ltLEDGER_HASHES),
99 LedgerTypeAttribute::accountOwnedLedgerType(JS(Offer), JS(offer), ripple::ltOFFER),
100 LedgerTypeAttribute::deletionBlockerLedgerType(
101 JS(PayChannel),
102 JS(payment_channel),
103 ripple::ltPAYCHAN
104 ),
105 LedgerTypeAttribute::accountOwnedLedgerType(
106 JS(SignerList),
107 JS(signer_list),
108 ripple::ltSIGNER_LIST
109 ),
110 LedgerTypeAttribute::deletionBlockerLedgerType(
111 JS(RippleState),
112 JS(state),
113 ripple::ltRIPPLE_STATE
114 ),
115 LedgerTypeAttribute::accountOwnedLedgerType(JS(Ticket), JS(ticket), ripple::ltTICKET),
116 LedgerTypeAttribute::accountOwnedLedgerType(
117 JS(NFTokenOffer),
118 JS(nft_offer),
119 ripple::ltNFTOKEN_OFFER
120 ),
121 LedgerTypeAttribute::deletionBlockerLedgerType(
122 JS(NFTokenPage),
123 JS(nft_page),
124 ripple::ltNFTOKEN_PAGE
125 ),
126 LedgerTypeAttribute::accountOwnedLedgerType(JS(AMM), JS(amm), ripple::ltAMM),
127 LedgerTypeAttribute::deletionBlockerLedgerType(JS(Bridge), JS(bridge), ripple::ltBRIDGE),
128 LedgerTypeAttribute::deletionBlockerLedgerType(
129 JS(XChainOwnedClaimID),
130 JS(xchain_owned_claim_id),
131 ripple::ltXCHAIN_OWNED_CLAIM_ID
132 ),
133 LedgerTypeAttribute::deletionBlockerLedgerType(
134 JS(XChainOwnedCreateAccountClaimID),
135 JS(xchain_owned_create_account_claim_id),
136 ripple::ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID
137 ),
138 LedgerTypeAttribute::accountOwnedLedgerType(JS(DID), JS(did), ripple::ltDID),
139 LedgerTypeAttribute::accountOwnedLedgerType(JS(Oracle), JS(oracle), ripple::ltORACLE),
140 LedgerTypeAttribute::accountOwnedLedgerType(
141 JS(Credential),
142 JS(credential),
143 ripple::ltCREDENTIAL
144 ),
145 LedgerTypeAttribute::accountOwnedLedgerType(JS(Vault), JS(vault), ripple::ltVAULT),
146 LedgerTypeAttribute::chainLedgerType(JS(NegativeUNL), JS(nunl), ripple::ltNEGATIVE_UNL),
147 LedgerTypeAttribute::deletionBlockerLedgerType(
148 JS(MPTokenIssuance),
149 JS(mpt_issuance),
150 ripple::ltMPTOKEN_ISSUANCE
151 ),
152 LedgerTypeAttribute::deletionBlockerLedgerType(JS(MPToken), JS(mptoken), ripple::ltMPTOKEN),
153 LedgerTypeAttribute::deletionBlockerLedgerType(
154 JS(PermissionedDomain),
155 JS(permissioned_domain),
156 ripple::ltPERMISSIONED_DOMAIN
157 ),
158 LedgerTypeAttribute::accountOwnedLedgerType(JS(Delegate), JS(delegate), ripple::ltDELEGATE),
159 };
160
161public:
166 static constexpr auto
168 {
169 std::array<char const*, std::size(kLEDGER_TYPES)> res{};
170 std::ranges::transform(kLEDGER_TYPES, std::begin(res), [](auto const& item) {
171 return item.rpcName_;
172 });
173 return res;
174 }
175
181 static constexpr auto
183 {
184 constexpr auto kFILTER = [](auto const& item) {
185 return item.category_ == LedgerTypeAttribute::LedgerCategory::DeletionBlocker;
186 };
187
188 constexpr auto kDELETION_BLOCKERS_COUNT =
189 std::count_if(std::begin(kLEDGER_TYPES), std::end(kLEDGER_TYPES), kFILTER);
190 std::array<ripple::LedgerEntryType, kDELETION_BLOCKERS_COUNT> res{};
191 auto it = std::begin(res);
192 std::ranges::for_each(kLEDGER_TYPES, [&](auto const& item) {
193 if (kFILTER(item)) {
194 *it = item.type_;
195 ++it;
196 }
197 });
198 return res;
199 }
200
208 static ripple::LedgerEntryType
209 getLedgerEntryTypeFromStr(std::string const& entryName);
210
218 static ripple::LedgerEntryType
219 getAccountOwnedLedgerTypeFromStr(std::string const& entryName);
220
221private:
222 static std::optional<std::reference_wrapper<impl::LedgerTypeAttribute const>>
223 getLedgerTypeAttributeFromStr(std::string const& entryName);
224};
225
232inline ripple::LedgerHeader
234{
235 return ripple::deserializeHeader(data, /* hasHash = */ true);
236}
237
244inline std::string
245toString(ripple::LedgerHeader const& info)
246{
247 return fmt::format(
248 "LedgerHeader {{Sequence: {}, Hash: {}, TxHash: {}, AccountHash: {}, ParentHash: {}}}",
249 info.seq,
250 ripple::strHex(info.hash),
251 strHex(info.txHash),
252 ripple::strHex(info.accountHash),
253 strHex(info.parentHash)
254 );
255}
256
257} // 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:182
static constexpr auto getLedgerEntryTypeStrList()
Returns a list of all ledger entry type as string.
Definition LedgerUtils.hpp:167
static ripple::LedgerEntryType getLedgerEntryTypeFromStr(std::string const &entryName)
Returns the ripple::LedgerEntryType from the given string.
Definition LedgerUtils.cpp:16
static ripple::LedgerEntryType getAccountOwnedLedgerTypeFromStr(std::string const &entryName)
Returns the ripple::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
ripple::LedgerHeader deserializeHeader(ripple::Slice data)
Deserializes a ripple::LedgerHeader from ripple::Slice of data.
Definition LedgerUtils.hpp:233
std::string toString(ripple::LedgerHeader const &info)
A helper function that converts a ripple::LedgerHeader to a string representation.
Definition LedgerUtils.hpp:245