Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
LedgerUtils.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "rpc/JS.hpp"
23
24#include <fmt/format.h>
25#include <xrpl/basics/Slice.h>
26#include <xrpl/basics/StringUtilities.h>
27#include <xrpl/basics/strHex.h>
28#include <xrpl/protocol/LedgerFormats.h>
29#include <xrpl/protocol/LedgerHeader.h>
30#include <xrpl/protocol/jss.h>
31
32#include <algorithm>
33#include <array>
34#include <functional>
35#include <optional>
36#include <string>
37#include <unordered_set>
38#include <vector>
39
40namespace util {
41
42class LedgerTypes;
43
44namespace impl {
45
47 enum class LedgerCategory {
48 Invalid,
49 AccountOwned, // The ledger object is owned by account
50 Chain, // The ledger object is shared across the chain
51 DeletionBlocker // The ledger object is owned by account and it blocks deletion
52 };
53
54 ripple::LedgerEntryType type_ = ripple::ltANY;
55 char const* name_ = nullptr;
56 char const* rpcName_ = nullptr;
57 LedgerCategory category_ = LedgerCategory::Invalid;
58
59 constexpr LedgerTypeAttribute(
60 char const* name,
61 char const* rpcName,
62 ripple::LedgerEntryType type,
63 LedgerCategory category
64 )
65 : type_{type}, name_{name}, rpcName_{rpcName}, category_{category}
66 {
67 }
68
69public:
70 static constexpr LedgerTypeAttribute
71 chainLedgerType(char const* name, char const* rpcName, ripple::LedgerEntryType type)
72 {
73 return LedgerTypeAttribute(name, rpcName, type, LedgerCategory::Chain);
74 }
75
76 static constexpr LedgerTypeAttribute
77 accountOwnedLedgerType(char const* name, char const* rpcName, ripple::LedgerEntryType type)
78 {
79 return LedgerTypeAttribute(name, rpcName, type, LedgerCategory::AccountOwned);
80 }
81
82 static constexpr LedgerTypeAttribute
83 deletionBlockerLedgerType(char const* name, char const* rpcName, ripple::LedgerEntryType type)
84 {
85 return LedgerTypeAttribute(name, rpcName, type, LedgerCategory::DeletionBlocker);
86 }
87 friend class util::LedgerTypes;
88};
89
90} // namespace impl
91
98 using LedgerTypeAttributeList = LedgerTypeAttribute[];
99
100 static constexpr LedgerTypeAttributeList const kLEDGER_TYPES{
101 LedgerTypeAttribute::accountOwnedLedgerType(JS(AccountRoot), JS(account), ripple::ltACCOUNT_ROOT),
102 LedgerTypeAttribute::chainLedgerType(JS(Amendments), JS(amendments), ripple::ltAMENDMENTS),
103 LedgerTypeAttribute::deletionBlockerLedgerType(JS(Check), JS(check), ripple::ltCHECK),
104 LedgerTypeAttribute::accountOwnedLedgerType(JS(DepositPreauth), JS(deposit_preauth), ripple::ltDEPOSIT_PREAUTH),
105 // dir node belongs to account, but can not be filtered from account_objects
106 LedgerTypeAttribute::chainLedgerType(JS(DirectoryNode), JS(directory), ripple::ltDIR_NODE),
107 LedgerTypeAttribute::deletionBlockerLedgerType(JS(Escrow), JS(escrow), ripple::ltESCROW),
108 LedgerTypeAttribute::chainLedgerType(JS(FeeSettings), JS(fee), ripple::ltFEE_SETTINGS),
109 LedgerTypeAttribute::chainLedgerType(JS(LedgerHashes), JS(hashes), ripple::ltLEDGER_HASHES),
110 LedgerTypeAttribute::accountOwnedLedgerType(JS(Offer), JS(offer), ripple::ltOFFER),
111 LedgerTypeAttribute::deletionBlockerLedgerType(JS(PayChannel), JS(payment_channel), ripple::ltPAYCHAN),
112 LedgerTypeAttribute::accountOwnedLedgerType(JS(SignerList), JS(signer_list), ripple::ltSIGNER_LIST),
113 LedgerTypeAttribute::deletionBlockerLedgerType(JS(RippleState), JS(state), ripple::ltRIPPLE_STATE),
114 LedgerTypeAttribute::accountOwnedLedgerType(JS(Ticket), JS(ticket), ripple::ltTICKET),
115 LedgerTypeAttribute::accountOwnedLedgerType(JS(NFTokenOffer), JS(nft_offer), ripple::ltNFTOKEN_OFFER),
116 LedgerTypeAttribute::deletionBlockerLedgerType(JS(NFTokenPage), JS(nft_page), ripple::ltNFTOKEN_PAGE),
117 LedgerTypeAttribute::accountOwnedLedgerType(JS(AMM), JS(amm), ripple::ltAMM),
118 LedgerTypeAttribute::deletionBlockerLedgerType(JS(Bridge), JS(bridge), ripple::ltBRIDGE),
119 LedgerTypeAttribute::deletionBlockerLedgerType(
120 JS(XChainOwnedClaimID),
121 JS(xchain_owned_claim_id),
122 ripple::ltXCHAIN_OWNED_CLAIM_ID
123 ),
124 LedgerTypeAttribute::deletionBlockerLedgerType(
125 JS(XChainOwnedCreateAccountClaimID),
126 JS(xchain_owned_create_account_claim_id),
127 ripple::ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID
128 ),
129 LedgerTypeAttribute::accountOwnedLedgerType(JS(DID), JS(did), ripple::ltDID),
130 LedgerTypeAttribute::accountOwnedLedgerType(JS(Oracle), JS(oracle), ripple::ltORACLE),
131 LedgerTypeAttribute::accountOwnedLedgerType(JS(Credential), JS(credential), ripple::ltCREDENTIAL),
132 LedgerTypeAttribute::accountOwnedLedgerType(JS(Vault), JS(vault), ripple::ltVAULT),
133 LedgerTypeAttribute::chainLedgerType(JS(NegativeUNL), JS(nunl), ripple::ltNEGATIVE_UNL),
134 LedgerTypeAttribute::deletionBlockerLedgerType(
135 JS(MPTokenIssuance),
136 JS(mpt_issuance),
137 ripple::ltMPTOKEN_ISSUANCE
138 ),
139 LedgerTypeAttribute::deletionBlockerLedgerType(JS(MPToken), JS(mptoken), ripple::ltMPTOKEN),
140 LedgerTypeAttribute::deletionBlockerLedgerType(
141 JS(PermissionedDomain),
142 JS(permissioned_domain),
143 ripple::ltPERMISSIONED_DOMAIN
144 ),
145 LedgerTypeAttribute::accountOwnedLedgerType(JS(Delegate), JS(delegate), ripple::ltDELEGATE),
146 };
147
148public:
153 static constexpr auto
155 {
156 std::array<char const*, std::size(kLEDGER_TYPES)> res{};
157 std::ranges::transform(kLEDGER_TYPES, std::begin(res), [](auto const& item) { return item.rpcName_; });
158 return res;
159 }
160
166 static constexpr auto
168 {
169 constexpr auto kFILTER = [](auto const& item) {
170 return item.category_ == LedgerTypeAttribute::LedgerCategory::DeletionBlocker;
171 };
172
173 constexpr auto kDELETION_BLOCKERS_COUNT =
174 std::count_if(std::begin(kLEDGER_TYPES), std::end(kLEDGER_TYPES), kFILTER);
175 std::array<ripple::LedgerEntryType, kDELETION_BLOCKERS_COUNT> res{};
176 auto it = std::begin(res);
177 std::ranges::for_each(kLEDGER_TYPES, [&](auto const& item) {
178 if (kFILTER(item)) {
179 *it = item.type_;
180 ++it;
181 }
182 });
183 return res;
184 }
185
192 static ripple::LedgerEntryType
193 getLedgerEntryTypeFromStr(std::string const& entryName);
194
202 static ripple::LedgerEntryType
203 getAccountOwnedLedgerTypeFromStr(std::string const& entryName);
204
205private:
206 static std::optional<std::reference_wrapper<impl::LedgerTypeAttribute const>>
207 getLedgerTypeAttributeFromStr(std::string const& entryName);
208};
209
216inline ripple::LedgerHeader
218{
219 return ripple::deserializeHeader(data, /* hasHash = */ true);
220}
221
228inline std::string
229toString(ripple::LedgerHeader const& info)
230{
231 return fmt::format(
232 "LedgerHeader {{Sequence: {}, Hash: {}, TxHash: {}, AccountHash: {}, ParentHash: {}}}",
233 info.seq,
234 ripple::strHex(info.hash),
235 strHex(info.txHash),
236 ripple::strHex(info.accountHash),
237 strHex(info.parentHash)
238 );
239}
240
241} // namespace util
A helper class that provides lists of different ledger type category.
Definition LedgerUtils.hpp:96
static constexpr auto getDeletionBlockerLedgerTypes()
Returns a list of all account deletion blocker's type as string.
Definition LedgerUtils.hpp:167
static constexpr auto getLedgerEntryTypeStrList()
Returns a list of all ledger entry type as string.
Definition LedgerUtils.hpp:154
static ripple::LedgerEntryType getLedgerEntryTypeFromStr(std::string const &entryName)
Returns the ripple::LedgerEntryType from the given string.
Definition LedgerUtils.cpp:35
static ripple::LedgerEntryType getAccountOwnedLedgerTypeFromStr(std::string const &entryName)
Returns the ripple::LedgerEntryType from the given string.
Definition LedgerUtils.cpp:44
Definition LedgerUtils.hpp:46
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:70
This namespace contains various utilities.
Definition AccountUtils.hpp:30
ripple::LedgerHeader deserializeHeader(ripple::Slice data)
Deserializes a ripple::LedgerHeader from ripple::Slice of data.
Definition LedgerUtils.hpp:217
std::string toString(ripple::LedgerHeader const &info)
A helper function that converts a ripple::LedgerHeader to a string representation.
Definition LedgerUtils.hpp:229