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/core.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 <string>
35#include <unordered_set>
36#include <vector>
37
38namespace util {
39
40class LedgerTypes;
41
42namespace impl {
44 enum class LedgerCategory {
45 Invalid,
46 AccountOwned, // The ledger object is owned by account
47 Chain, // The ledger object is shared across the chain
48 DeletionBlocker // The ledger object is owned by account and it blocks deletion
49 };
50
51 ripple::LedgerEntryType type_ = ripple::ltANY;
52 char const* name_ = nullptr;
53 LedgerCategory category_ = LedgerCategory::Invalid;
54
55 constexpr LedgerTypeAttribute(char const* name, ripple::LedgerEntryType type, LedgerCategory category)
56 : type_(type), name_(name), category_(category)
57 {
58 }
59
60public:
61 static constexpr LedgerTypeAttribute
62 chainLedgerType(char const* name, ripple::LedgerEntryType type)
63 {
64 return LedgerTypeAttribute(name, type, LedgerCategory::Chain);
65 }
66
67 static constexpr LedgerTypeAttribute
68 accountOwnedLedgerType(char const* name, ripple::LedgerEntryType type)
69 {
70 return LedgerTypeAttribute(name, type, LedgerCategory::AccountOwned);
71 }
72
73 static constexpr LedgerTypeAttribute
74 deletionBlockerLedgerType(char const* name, ripple::LedgerEntryType type)
75 {
76 return LedgerTypeAttribute(name, type, LedgerCategory::DeletionBlocker);
77 }
78 friend class util::LedgerTypes;
79};
80} // namespace impl
81
88 using LedgerTypeAttributeList = LedgerTypeAttribute[];
89
90 static constexpr LedgerTypeAttributeList const kLEDGER_TYPES{
91 LedgerTypeAttribute::accountOwnedLedgerType(JS(account), ripple::ltACCOUNT_ROOT),
92 LedgerTypeAttribute::chainLedgerType(JS(amendments), ripple::ltAMENDMENTS),
93 LedgerTypeAttribute::deletionBlockerLedgerType(JS(check), ripple::ltCHECK),
94 LedgerTypeAttribute::accountOwnedLedgerType(JS(deposit_preauth), ripple::ltDEPOSIT_PREAUTH),
95 // dir node belongs to account, but can not be filtered from account_objects
96 LedgerTypeAttribute::chainLedgerType(JS(directory), ripple::ltDIR_NODE),
97 LedgerTypeAttribute::deletionBlockerLedgerType(JS(escrow), ripple::ltESCROW),
98 LedgerTypeAttribute::chainLedgerType(JS(fee), ripple::ltFEE_SETTINGS),
99 LedgerTypeAttribute::chainLedgerType(JS(hashes), ripple::ltLEDGER_HASHES),
100 LedgerTypeAttribute::accountOwnedLedgerType(JS(offer), ripple::ltOFFER),
101 LedgerTypeAttribute::deletionBlockerLedgerType(JS(payment_channel), ripple::ltPAYCHAN),
102 LedgerTypeAttribute::accountOwnedLedgerType(JS(signer_list), ripple::ltSIGNER_LIST),
103 LedgerTypeAttribute::deletionBlockerLedgerType(JS(state), ripple::ltRIPPLE_STATE),
104 LedgerTypeAttribute::accountOwnedLedgerType(JS(ticket), ripple::ltTICKET),
105 LedgerTypeAttribute::accountOwnedLedgerType(JS(nft_offer), ripple::ltNFTOKEN_OFFER),
106 LedgerTypeAttribute::deletionBlockerLedgerType(JS(nft_page), ripple::ltNFTOKEN_PAGE),
107 LedgerTypeAttribute::accountOwnedLedgerType(JS(amm), ripple::ltAMM),
108 LedgerTypeAttribute::deletionBlockerLedgerType(JS(bridge), ripple::ltBRIDGE),
109 LedgerTypeAttribute::deletionBlockerLedgerType(JS(xchain_owned_claim_id), ripple::ltXCHAIN_OWNED_CLAIM_ID),
110 LedgerTypeAttribute::deletionBlockerLedgerType(
111 JS(xchain_owned_create_account_claim_id),
112 ripple::ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID
113 ),
114 LedgerTypeAttribute::accountOwnedLedgerType(JS(did), ripple::ltDID),
115 LedgerTypeAttribute::accountOwnedLedgerType(JS(oracle), ripple::ltORACLE),
116 LedgerTypeAttribute::accountOwnedLedgerType(JS(credential), ripple::ltCREDENTIAL),
117 LedgerTypeAttribute::chainLedgerType(JS(nunl), ripple::ltNEGATIVE_UNL),
118 LedgerTypeAttribute::deletionBlockerLedgerType(JS(mpt_issuance), ripple::ltMPTOKEN_ISSUANCE),
119 LedgerTypeAttribute::deletionBlockerLedgerType(JS(mptoken), ripple::ltMPTOKEN),
120 LedgerTypeAttribute::deletionBlockerLedgerType(JS(permissioned_domain), ripple::ltPERMISSIONED_DOMAIN),
121 };
122
123public:
128 static constexpr auto
130 {
131 std::array<char const*, std::size(kLEDGER_TYPES)> res{};
132 std::ranges::transform(kLEDGER_TYPES, std::begin(res), [](auto const& item) { return item.name_; });
133 return res;
134 }
135
141 static constexpr auto
143 {
144 constexpr auto kFILTER = [](auto const& item) {
145 return item.category_ != LedgerTypeAttribute::LedgerCategory::Chain;
146 };
147
148 constexpr auto kACCOUNT_OWNED_COUNT =
149 std::count_if(std::begin(kLEDGER_TYPES), std::end(kLEDGER_TYPES), kFILTER);
150 std::array<char const*, kACCOUNT_OWNED_COUNT> res{};
151 auto it = std::begin(res);
152 std::ranges::for_each(kLEDGER_TYPES, [&](auto const& item) {
153 if (kFILTER(item)) {
154 *it = item.name_;
155 ++it;
156 }
157 });
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};
195
202inline ripple::LedgerHeader
204{
205 return ripple::deserializeHeader(data, /* hasHash = */ true);
206}
207
214inline std::string
215toString(ripple::LedgerHeader const& info)
216{
217 return fmt::format(
218 "LedgerHeader {{Sequence: {}, Hash: {}, TxHash: {}, AccountHash: {}, ParentHash: {}}}",
219 info.seq,
220 ripple::strHex(info.hash),
221 strHex(info.txHash),
222 ripple::strHex(info.accountHash),
223 strHex(info.parentHash)
224 );
225}
226
227} // namespace util
A helper class that provides lists of different ledger type catagory.
Definition LedgerUtils.hpp:86
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:129
static ripple::LedgerEntryType getLedgerEntryTypeFromStr(std::string const &entryName)
Returns the ripple::LedgerEntryType from the given string.
Definition LedgerUtils.cpp:31
static constexpr auto getAccountOwnedLedgerTypeStrList()
Returns a list of all account owned ledger entry type as string.
Definition LedgerUtils.hpp:142
Definition LedgerUtils.hpp:43
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:203
std::string toString(ripple::LedgerHeader const &info)
A helper function that converts a ripple::LedgerHeader to a string representation.
Definition LedgerUtils.hpp:215