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