Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
DBHelpers.hpp
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2022, 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
21#pragma once
22
23#include "util/Assert.hpp"
24
25#include <boost/container/flat_set.hpp>
26#include <xrpl/basics/Blob.h>
27#include <xrpl/basics/Log.h>
28#include <xrpl/basics/StringUtilities.h>
29#include <xrpl/basics/base_uint.h>
30#include <xrpl/protocol/AccountID.h>
31#include <xrpl/protocol/SField.h>
32#include <xrpl/protocol/STAccount.h>
33#include <xrpl/protocol/STLedgerEntry.h>
34#include <xrpl/protocol/Serializer.h>
35#include <xrpl/protocol/TxMeta.h>
36
37#include <cstddef>
38#include <cstdint>
39#include <optional>
40#include <string>
41
46 boost::container::flat_set<ripple::AccountID> accounts;
47 std::uint32_t ledgerSequence{};
48 std::uint32_t transactionIndex{};
49 ripple::uint256 txHash;
50
57 AccountTransactionsData(ripple::TxMeta const& meta, ripple::uint256 const& txHash)
58 : accounts(meta.getAffectedAccounts())
59 , ledgerSequence(meta.getLgrSeq())
60 , transactionIndex(meta.getIndex())
61 , txHash(txHash)
62 {
63 }
64
65 AccountTransactionsData() = default;
66};
67
74 ripple::uint256 tokenID;
75 std::uint32_t ledgerSequence;
76 std::uint32_t transactionIndex;
77 ripple::uint256 txHash;
78
87 ripple::uint256 const& tokenID,
88 ripple::TxMeta const& meta,
89 ripple::uint256 const& txHash
90 )
91 : tokenID(tokenID)
92 , ledgerSequence(meta.getLgrSeq())
93 , transactionIndex(meta.getIndex())
94 , txHash(txHash)
95 {
96 }
97};
98
112struct NFTsData {
113 ripple::uint256 tokenID;
114 std::uint32_t ledgerSequence;
115 std::optional<std::uint32_t> transactionIndex;
116 ripple::AccountID owner;
117 std::optional<ripple::Blob> uri;
118 bool isBurned = false;
119 bool onlyUriChanged = false; // Whether only the URI was changed
120
135 ripple::uint256 const& tokenID,
136 ripple::AccountID const& owner,
137 ripple::Blob const& uri,
138 ripple::TxMeta const& meta
139 )
140 : tokenID(tokenID)
141 , ledgerSequence(meta.getLgrSeq())
142 , transactionIndex(meta.getIndex())
143 , owner(owner)
144 , uri(uri)
145 {
146 }
147
159 ripple::uint256 const& tokenID,
160 ripple::AccountID const& owner,
161 ripple::TxMeta const& meta,
162 bool isBurned
163 )
164 : tokenID(tokenID)
165 , ledgerSequence(meta.getLgrSeq())
166 , transactionIndex(meta.getIndex())
167 , owner(owner)
168 , isBurned(isBurned)
169 {
170 }
171
186 ripple::uint256 const& tokenID,
187 std::uint32_t const ledgerSequence,
188 ripple::AccountID const& owner,
189 ripple::Blob const& uri
190 )
191 : tokenID(tokenID), ledgerSequence(ledgerSequence), owner(owner), uri(uri)
192 {
193 }
194
203 NFTsData(ripple::uint256 const& tokenID, ripple::TxMeta const& meta, ripple::Blob const& uri)
204 : tokenID(tokenID)
205 , ledgerSequence(meta.getLgrSeq())
206 , transactionIndex(meta.getIndex())
207 , uri(uri)
208 , onlyUriChanged(true)
209 {
210 }
211};
212
217 ripple::uint192 mptID;
218 ripple::AccountID holder;
219};
220
227template <typename T>
228inline bool
229isDirNode(T const& object)
230{
231 static constexpr auto kMIN_SIZE_REQUIRED = 3;
232 if (std::size(object) < kMIN_SIZE_REQUIRED)
233 return false;
234
235 static constexpr short kDIR_NODE_SPACE_KEY = 0x0064;
236 short const spaceKey = (object.data()[1] << 8) | object.data()[2];
237 return spaceKey == kDIR_NODE_SPACE_KEY;
238}
239
247template <typename T, typename R>
248inline bool
249isBookDir(T const& key, R const& object)
250{
251 if (!isDirNode(object))
252 return false;
253
254 ripple::STLedgerEntry const sle{ripple::SerialIter{object.data(), object.size()}, key};
255 return !sle[~ripple::sfOwner].has_value();
256}
257
264template <typename T>
265inline ripple::uint256
266getBookBase(T const& key)
267{
268 static constexpr size_t kEY_SIZE = 24;
269
270 ASSERT(key.size() == ripple::uint256::size(), "Invalid key size {}", key.size());
271
272 ripple::uint256 ret;
273 for (size_t i = 0; i < kEY_SIZE; ++i)
274 ret.data()[i] = key.data()[i];
275
276 return ret;
277}
278
285inline std::string
286uint256ToString(ripple::uint256 const& input)
287{
288 return {reinterpret_cast<char const*>(input.data()), ripple::uint256::size()};
289}
290
292static constexpr std::uint32_t kRIPPLE_EPOCH_START = 946684800;
ripple::uint256 getBookBase(T const &key)
Get the book base.
Definition DBHelpers.hpp:266
bool isDirNode(T const &object)
Check whether the supplied object is a dir node.
Definition DBHelpers.hpp:229
std::string uint256ToString(ripple::uint256 const &input)
Stringify a ripple::uint256.
Definition DBHelpers.hpp:286
bool isBookDir(T const &key, R const &object)
Check whether the supplied object is a book dir.
Definition DBHelpers.hpp:249
static constexpr std::uint32_t kRIPPLE_EPOCH_START
The ripple epoch start timestamp. Midnight on 1st January 2000.
Definition DBHelpers.hpp:292
AccountTransactionsData(ripple::TxMeta const &meta, ripple::uint256 const &txHash)
Construct a new AccountTransactionsData object.
Definition DBHelpers.hpp:57
Represents an MPT and holder pair.
Definition DBHelpers.hpp:216
NFTTransactionsData(ripple::uint256 const &tokenID, ripple::TxMeta const &meta, ripple::uint256 const &txHash)
Construct a new NFTTransactionsData object.
Definition DBHelpers.hpp:86
NFTsData(ripple::uint256 const &tokenID, std::uint32_t const ledgerSequence, ripple::AccountID const &owner, ripple::Blob const &uri)
Construct a new NFTsData object.
Definition DBHelpers.hpp:185
NFTsData(ripple::uint256 const &tokenID, ripple::AccountID const &owner, ripple::TxMeta const &meta, bool isBurned)
Construct a new NFTsData object.
Definition DBHelpers.hpp:158
NFTsData(ripple::uint256 const &tokenID, ripple::AccountID const &owner, ripple::Blob const &uri, ripple::TxMeta const &meta)
Construct a new NFTsData object.
Definition DBHelpers.hpp:134
NFTsData(ripple::uint256 const &tokenID, ripple::TxMeta const &meta, ripple::Blob const &uri)
Construct a new NFTsData object with only the URI changed.
Definition DBHelpers.hpp:203