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
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
209isDirNode(T const& object)
210{
211 static constexpr auto kMIN_SIZE_REQUIRED = 3;
212 if (std::size(object) < kMIN_SIZE_REQUIRED)
213 return false;
214
215 static constexpr short kDIR_NODE_SPACE_KEY = 0x0064;
216 short const spaceKey = (object.data()[1] << 8) | object.data()[2];
217 return spaceKey == kDIR_NODE_SPACE_KEY;
218}
219
227template <typename T, typename R>
228inline bool
229isBookDir(T const& key, R const& object)
230{
231 if (!isDirNode(object))
232 return false;
233
234 ripple::STLedgerEntry const sle{ripple::SerialIter{object.data(), object.size()}, key};
235 return !sle[~ripple::sfOwner].has_value();
236}
237
244template <typename T>
245inline ripple::uint256
246getBookBase(T const& key)
247{
248 static constexpr size_t kEY_SIZE = 24;
249
250 ASSERT(key.size() == ripple::uint256::size(), "Invalid key size {}", key.size());
251
252 ripple::uint256 ret;
253 for (size_t i = 0; i < kEY_SIZE; ++i)
254 ret.data()[i] = key.data()[i];
255
256 return ret;
257}
258
265inline std::string
266uint256ToString(ripple::uint256 const& input)
267{
268 return {reinterpret_cast<char const*>(input.data()), ripple::uint256::size()};
269}
270
272static constexpr std::uint32_t kRIPPLE_EPOCH_START = 946684800;
ripple::uint256 getBookBase(T const &key)
Get the book base.
Definition DBHelpers.hpp:246
bool isDirNode(T const &object)
Check whether the supplied object is a dir node.
Definition DBHelpers.hpp:209
std::string uint256ToString(ripple::uint256 const &input)
Stringify a ripple::uint256.
Definition DBHelpers.hpp:266
bool isBookDir(T const &key, R const &object)
Check whether the supplied object is a book dir.
Definition DBHelpers.hpp:229
static constexpr std::uint32_t kRIPPLE_EPOCH_START
The ripple epoch start timestamp. Midnight on 1st January 2000.
Definition DBHelpers.hpp:272
Struct used to keep track of what to write to account_transactions/account_tx tables.
Definition DBHelpers.hpp:45
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: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