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& 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
86 NFTTransactionsData(ripple::uint256 const& tokenID, ripple::TxMeta const& meta, ripple::uint256 const& txHash)
87 : tokenID(tokenID), ledgerSequence(meta.getLgrSeq()), transactionIndex(meta.getIndex()), txHash(txHash)
88 {
89 }
90};
91
103struct NFTsData {
104 ripple::uint256 tokenID;
105 std::uint32_t ledgerSequence;
106 std::optional<std::uint32_t> transactionIndex;
107 ripple::AccountID owner;
108 std::optional<ripple::Blob> uri;
109 bool isBurned = false;
110 bool onlyUriChanged = false; // Whether only the URI was changed
111
125 ripple::uint256 const& tokenID,
126 ripple::AccountID const& owner,
127 ripple::Blob const& uri,
128 ripple::TxMeta const& meta
129 )
130 : tokenID(tokenID), ledgerSequence(meta.getLgrSeq()), transactionIndex(meta.getIndex()), owner(owner), uri(uri)
131 {
132 }
133
144 NFTsData(ripple::uint256 const& tokenID, ripple::AccountID const& owner, ripple::TxMeta const& meta, bool isBurned)
145 : tokenID(tokenID)
146 , ledgerSequence(meta.getLgrSeq())
147 , transactionIndex(meta.getIndex())
148 , owner(owner)
149 , isBurned(isBurned)
150 {
151 }
152
166 ripple::uint256 const& tokenID,
167 std::uint32_t const ledgerSequence,
168 ripple::AccountID const& owner,
169 ripple::Blob const& uri
170 )
171 : tokenID(tokenID), ledgerSequence(ledgerSequence), owner(owner), uri(uri)
172 {
173 }
174
183 NFTsData(ripple::uint256 const& tokenID, ripple::TxMeta const& meta, ripple::Blob const& uri)
184 : tokenID(tokenID)
185 , ledgerSequence(meta.getLgrSeq())
186 , transactionIndex(meta.getIndex())
187 , uri(uri)
188 , onlyUriChanged(true)
189 {
190 }
191};
192
197 ripple::uint192 mptID;
198 ripple::AccountID holder;
199};
200
207template <typename T>
208inline bool
209isOffer(T const& object)
210{
211 static constexpr short kOFFER_OFFSET = 0x006f;
212 static constexpr short kSHIFT = 8;
213
214 short offerBytes = (object[1] << kSHIFT) | object[2];
215 return offerBytes == kOFFER_OFFSET;
216}
217
224template <typename T>
225inline bool
226isOfferHex(T const& object)
227{
228 auto blob = ripple::strUnHex(4, object.begin(), object.begin() + 4);
229 if (blob)
230 return isOffer(*blob);
231 return false;
232}
233
240template <typename T>
241inline bool
242isDirNode(T const& object)
243{
244 static constexpr short kDIR_NODE_SPACE_KEY = 0x0064;
245 short const spaceKey = (object.data()[1] << 8) | object.data()[2];
246 return spaceKey == kDIR_NODE_SPACE_KEY;
247}
248
256template <typename T, typename R>
257inline bool
258isBookDir(T const& key, R const& object)
259{
260 if (!isDirNode(object))
261 return false;
262
263 ripple::STLedgerEntry const sle{ripple::SerialIter{object.data(), object.size()}, key};
264 return !sle[~ripple::sfOwner].has_value();
265}
266
273template <typename T>
274inline ripple::uint256
275getBook(T const& offer)
276{
277 ripple::SerialIter it{offer.data(), offer.size()};
278 ripple::SLE const sle{it, {}};
279 ripple::uint256 book = sle.getFieldH256(ripple::sfBookDirectory);
280
281 return book;
282}
283
290template <typename T>
291inline ripple::uint256
292getBookBase(T const& key)
293{
294 static constexpr size_t kEY_SIZE = 24;
295
296 ASSERT(key.size() == ripple::uint256::size(), "Invalid key size {}", key.size());
297
298 ripple::uint256 ret;
299 for (size_t i = 0; i < kEY_SIZE; ++i)
300 ret.data()[i] = key.data()[i];
301
302 return ret;
303}
304
311inline std::string
312uint256ToString(ripple::uint256 const& input)
313{
314 return {reinterpret_cast<char const*>(input.data()), ripple::uint256::size()};
315}
316
318static constexpr std::uint32_t kRIPPLE_EPOCH_START = 946684800;
ripple::uint256 getBookBase(T const &key)
Get the book base.
Definition DBHelpers.hpp:292
bool isDirNode(T const &object)
Check whether the supplied object is a dir node.
Definition DBHelpers.hpp:242
std::string uint256ToString(ripple::uint256 const &input)
Stringify a ripple::uint256.
Definition DBHelpers.hpp:312
ripple::uint256 getBook(T const &offer)
Get the book out of an offer object.
Definition DBHelpers.hpp:275
bool isOffer(T const &object)
Check whether the supplied object is an offer.
Definition DBHelpers.hpp:209
bool isBookDir(T const &key, R const &object)
Check whether the supplied object is a book dir.
Definition DBHelpers.hpp:258
bool isOfferHex(T const &object)
Check whether the supplied hex represents an offer object.
Definition DBHelpers.hpp:226
static constexpr std::uint32_t kRIPPLE_EPOCH_START
The ripple epoch start timestamp. Midnight on 1st January 2000.
Definition DBHelpers.hpp:318
Struct used to keep track of what to write to account_transactions/account_tx tables.
Definition DBHelpers.hpp:45
AccountTransactionsData(ripple::TxMeta &meta, ripple::uint256 const &txHash)
Construct a new AccountTransactionsData object.
Definition DBHelpers.hpp:57
Represents an MPT and holder pair.
Definition DBHelpers.hpp:196
Represents a link from a tx to an NFT that was targeted/modified/created by it.
Definition DBHelpers.hpp:73
NFTTransactionsData(ripple::uint256 const &tokenID, ripple::TxMeta const &meta, ripple::uint256 const &txHash)
Construct a new NFTTransactionsData object.
Definition DBHelpers.hpp:86
Represents an NFT state at a particular ledger.
Definition DBHelpers.hpp:103
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:165
NFTsData(ripple::uint256 const &tokenID, ripple::AccountID const &owner, ripple::TxMeta const &meta, bool isBurned)
Construct a new NFTsData object.
Definition DBHelpers.hpp:144
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:124
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:183