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 <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::accountOwnedLedgerType(JS(vault), ripple::ltVAULT),
118 LedgerTypeAttribute::chainLedgerType(JS(nunl), ripple::ltNEGATIVE_UNL),
119 LedgerTypeAttribute::deletionBlockerLedgerType(JS(mpt_issuance), ripple::ltMPTOKEN_ISSUANCE),
120 LedgerTypeAttribute::deletionBlockerLedgerType(JS(mptoken), ripple::ltMPTOKEN),
121 LedgerTypeAttribute::deletionBlockerLedgerType(JS(permissioned_domain), ripple::ltPERMISSIONED_DOMAIN),
122 LedgerTypeAttribute::accountOwnedLedgerType(JS(delegate), ripple::ltDELEGATE),
123 };
124
125public:
130 static constexpr auto
132 {
133 std::array<char const*, std::size(kLEDGER_TYPES)> res{};
134 std::ranges::transform(kLEDGER_TYPES, std::begin(res), [](auto const& item) { return item.name_; });
135 return res;
136 }
137
143 static constexpr auto
145 {
146 constexpr auto kFILTER = [](auto const& item) {
147 return item.category_ != LedgerTypeAttribute::LedgerCategory::Chain;
148 };
149
150 constexpr auto kACCOUNT_OWNED_COUNT =
151 std::count_if(std::begin(kLEDGER_TYPES), std::end(kLEDGER_TYPES), kFILTER);
152 std::array<char const*, kACCOUNT_OWNED_COUNT> res{};
153 auto it = std::begin(res);
154 std::ranges::for_each(kLEDGER_TYPES, [&](auto const& item) {
155 if (kFILTER(item)) {
156 *it = item.name_;
157 ++it;
158 }
159 });
160 return res;
161 }
162
168 static constexpr auto
170 {
171 constexpr auto kFILTER = [](auto const& item) {
172 return item.category_ == LedgerTypeAttribute::LedgerCategory::DeletionBlocker;
173 };
174
175 constexpr auto kDELETION_BLOCKERS_COUNT =
176 std::count_if(std::begin(kLEDGER_TYPES), std::end(kLEDGER_TYPES), kFILTER);
177 std::array<ripple::LedgerEntryType, kDELETION_BLOCKERS_COUNT> res{};
178 auto it = std::begin(res);
179 std::ranges::for_each(kLEDGER_TYPES, [&](auto const& item) {
180 if (kFILTER(item)) {
181 *it = item.type_;
182 ++it;
183 }
184 });
185 return res;
186 }
187
194 static ripple::LedgerEntryType
195 getLedgerEntryTypeFromStr(std::string const& entryName);
196};
197
204inline ripple::LedgerHeader
206{
207 return ripple::deserializeHeader(data, /* hasHash = */ true);
208}
209
216inline std::string
217toString(ripple::LedgerHeader const& info)
218{
219 return fmt::format(
220 "LedgerHeader {{Sequence: {}, Hash: {}, TxHash: {}, AccountHash: {}, ParentHash: {}}}",
221 info.seq,
222 ripple::strHex(info.hash),
223 strHex(info.txHash),
224 ripple::strHex(info.accountHash),
225 strHex(info.parentHash)
226 );
227}
228
229} // namespace util
A helper class that provides lists of different ledger type category.
Definition LedgerUtils.hpp:86
static constexpr auto getDeletionBlockerLedgerTypes()
Returns a list of all account deletion blocker's type as string.
Definition LedgerUtils.hpp:169
static constexpr auto getLedgerEntryTypeStrList()
Returns a list of all ledger entry type as string.
Definition LedgerUtils.hpp:131
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:144
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:205
std::string toString(ripple::LedgerHeader const &info)
A helper function that converts a ripple::LedgerHeader to a string representation.
Definition LedgerUtils.hpp:217