3#include "data/BackendInterface.hpp"
5#include "data/LedgerCacheInterface.hpp"
6#include "data/LedgerHeaderCache.hpp"
7#include "data/Types.hpp"
8#include "data/cassandra/Concepts.hpp"
9#include "data/cassandra/Handle.hpp"
10#include "data/cassandra/Types.hpp"
11#include "data/cassandra/impl/ExecutionStrategy.hpp"
12#include "util/Assert.hpp"
13#include "util/LedgerUtils.hpp"
14#include "util/Profiler.hpp"
15#include "util/log/Logger.hpp"
17#include <boost/asio/spawn.hpp>
18#include <boost/json/object.hpp>
19#include <boost/uuid/string_generator.hpp>
20#include <boost/uuid/uuid.hpp>
22#include <fmt/format.h>
23#include <xrpl/basics/Blob.h>
24#include <xrpl/basics/base_uint.h>
25#include <xrpl/basics/strHex.h>
26#include <xrpl/protocol/AccountID.h>
27#include <xrpl/protocol/Indexes.h>
28#include <xrpl/protocol/LedgerHeader.h>
29#include <xrpl/protocol/nft.h>
48class CacheBackendCassandraTest;
71 SettingsProviderType settingsProvider_;
73 std::atomic_uint32_t ledgerSequence_ = 0u;
74 friend class ::CacheBackendCassandraTest;
79 mutable ExecutionStrategyType executor_;
81 mutable FetchLedgerCacheType ledgerCache_{};
83 static constexpr std::size_t kTransactionCursorBindIndex = 1;
84 static constexpr std::size_t kTransactionLimitBindIndex = 2;
85 static constexpr std::size_t kMPTokenIssuanceTxCursorBindIndex = 1;
86 static constexpr std::size_t kMPTokenIssuanceTxLimitBindIndex = 2;
87 static constexpr std::size_t kAccountMPTokenIssuanceTxCursorBindIndex = 2;
88 static constexpr std::size_t kAccountMPTokenIssuanceTxLimitBindIndex = 3;
99 SettingsProviderType settingsProvider,
104 , settingsProvider_{std::move(settingsProvider)}
105 , schema_{settingsProvider_}
106 , handle_{settingsProvider_.getSettings()}
107 , executor_{settingsProvider_.getSettings(), handle_}
109 if (
auto const res = handle_.connect(); not res.has_value())
110 throw std::runtime_error(
"Could not connect to database: " + res.error());
113 if (
auto const res = handle_.execute(schema_.createKeyspace); not res.has_value()) {
116 if (res.error().code() != CASS_ERROR_SERVER_UNAUTHORIZED)
117 throw std::runtime_error(
"Could not create keyspace: " + res.error());
120 if (
auto const res = handle_.executeEach(schema_.createSchema); not res.has_value())
121 throw std::runtime_error(
"Could not create schema: " + res.error());
125 schema_.prepareStatements(handle_);
126 }
catch (std::runtime_error
const& ex) {
127 auto const error = fmt::format(
128 "Failed to prepare the statements: {}; readOnly: {}. ReadOnly should be turned off "
130 "node with write access to DB should be started first.",
134 LOG(log_.error()) << error;
135 throw std::runtime_error(error);
137 LOG(log_.info()) <<
"Created (revamped) CassandraBackend";
148 [[nodiscard]] std::chrono::milliseconds
151 return settingsProvider_.getInitialRetryDelay();
157 [[nodiscard]] std::chrono::milliseconds
160 return settingsProvider_.getMaxRetryDelay();
175 xrpl::AccountID
const& account,
176 std::uint32_t
const limit,
178 std::optional<TransactionsCursor>
const& txnCursor,
179 boost::asio::yield_context yield
184 return {.txns = {}, .cursor = {}};
186 Statement const statement = [
this, forward, &account] {
188 return schema_->selectAccountTxForward.bind(account);
190 return schema_->selectAccountTx.bind(account);
193 auto cursor = txnCursor;
195 statement.
bindAt(kTransactionCursorBindIndex, cursor->asTuple());
196 LOG(log_.debug()) <<
"account = " << xrpl::strHex(account)
197 <<
" tuple = " << cursor->ledgerSequence << cursor->transactionIndex;
199 auto const seq = forward ? rng->minSequence : rng->maxSequence;
200 auto const placeHolder = forward ? 0u : std::numeric_limits<std::uint32_t>::max();
203 kTransactionCursorBindIndex, std::make_tuple(placeHolder, placeHolder)
205 LOG(log_.debug()) <<
"account = " << xrpl::strHex(account) <<
" idx = " << seq
206 <<
" tuple = " << placeHolder;
212 statement.
bindAt(kTransactionLimitBindIndex,
Limit{limit});
213 auto const res = executor_.read(yield, statement);
214 auto const& results = res.value();
215 if (not results.hasRows()) {
216 LOG(log_.debug()) <<
"No rows returned";
220 std::vector<xrpl::uint256> hashes = {};
221 auto numRows = results.numRows();
222 LOG(log_.info()) <<
"num_rows = " << numRows;
224 for (
auto [hash,
data] :
extract<xrpl::uint256, std::tuple<uint32_t, uint32_t>>(results)) {
225 hashes.push_back(hash);
226 if (--numRows == 0) {
227 LOG(log_.debug()) <<
"Setting cursor";
233 LOG(log_.debug()) <<
"Txns = " << txns.size();
235 if (txns.size() == limit) {
236 LOG(log_.debug()) <<
"Returning cursor";
237 return {txns, cursor};
260 writeLedger(xrpl::LedgerHeader
const& ledgerHeader, std::string&& blob)
override
262 executor_.write(schema_->insertLedgerHeader, ledgerHeader.seq, std::move(blob));
264 executor_.write(schema_->insertLedgerHash, ledgerHeader.hash, ledgerHeader.seq);
266 ledgerSequence_ = ledgerHeader.seq;
275 std::optional<std::uint32_t>
278 if (
auto const res = executor_.read(yield, schema_->selectLatestLedger); res.has_value()) {
279 if (
auto const& rows = *res; rows) {
280 if (
auto const maybeRow = rows.template get<uint32_t>(); maybeRow.has_value())
283 LOG(log_.error()) <<
"Could not fetch latest ledger - no rows";
287 LOG(log_.error()) <<
"Could not fetch latest ledger - no result";
289 LOG(log_.error()) <<
"Could not fetch latest ledger: " << res.error();
302 std::optional<xrpl::LedgerHeader>
304 std::uint32_t
const sequence,
305 boost::asio::yield_context yield
308 if (
auto const lock = ledgerCache_.get(); lock.has_value() && lock->seq == sequence)
311 auto const res = executor_.read(yield, schema_->selectLedgerBySeq, sequence);
313 if (
auto const& result = res.value(); result) {
314 if (
auto const maybeValue = result.template get<std::vector<unsigned char>>();
321 LOG(log_.error()) <<
"Could not fetch ledger by sequence - no rows";
325 LOG(log_.error()) <<
"Could not fetch ledger by sequence - no result";
327 LOG(log_.error()) <<
"Could not fetch ledger by sequence: " << res.error();
340 std::optional<xrpl::LedgerHeader>
343 if (
auto const res = executor_.read(yield, schema_->selectLedgerByHash, hash); res) {
344 if (
auto const& result = res.value(); result) {
345 if (
auto const maybeValue = result.template get<uint32_t>(); maybeValue)
348 LOG(log_.error()) <<
"Could not fetch ledger by hash - no rows";
352 LOG(log_.error()) <<
"Could not fetch ledger by hash - no result";
354 LOG(log_.error()) <<
"Could not fetch ledger by hash: " << res.error();
366 std::optional<LedgerRange>
369 auto const res = executor_.read(yield, schema_->selectLedgerRange);
371 auto const& results = res.value();
372 if (not results.hasRows()) {
373 LOG(log_.debug()) <<
"Could not fetch ledger range - no rows";
385 }
else if (idx == 1) {
395 LOG(log_.debug()) <<
"After hardFetchLedgerRange range is " << range.
minSequence <<
":"
399 LOG(log_.error()) <<
"Could not fetch ledger range: " << res.error();
411 std::vector<TransactionAndMetadata>
413 std::uint32_t
const ledgerSequence,
414 boost::asio::yield_context yield
428 std::vector<xrpl::uint256>
430 std::uint32_t
const ledgerSequence,
431 boost::asio::yield_context yield
434 auto const start = std::chrono::system_clock::now();
436 executor_.read(yield, schema_->selectAllTransactionHashesInLedger, ledgerSequence);
439 LOG(log_.error()) <<
"Could not fetch all transaction hashes: " << res.error();
443 auto const& result = res.value();
444 if (not result.hasRows()) {
445 LOG(log_.warn()) <<
"Could not fetch all transaction hashes - no rows; ledger = "
446 << std::to_string(ledgerSequence);
450 std::vector<xrpl::uint256> hashes;
452 hashes.push_back(std::move(hash));
454 auto const end = std::chrono::system_clock::now();
458 << hashes.size() <<
" transaction hashes from database in "
459 << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
475 xrpl::uint256
const& tokenID,
476 std::uint32_t
const ledgerSequence,
477 boost::asio::yield_context yield
480 auto const res = executor_.read(yield, schema_->selectNFT, tokenID, ledgerSequence);
484 if (
auto const maybeRow = res->template get<uint32_t, xrpl::AccountID, bool>(); maybeRow) {
485 auto [seq, owner, isBurned] = *maybeRow;
486 auto result = std::make_optional<NFT>(tokenID, seq, owner, isBurned);
499 auto uriRes = executor_.read(yield, schema_->selectNFTURI, tokenID, ledgerSequence);
501 if (
auto const maybeUri = uriRes->template get<xrpl::Blob>(); maybeUri)
502 result->uri = *maybeUri;
508 LOG(log_.error()) <<
"Could not fetch NFT - no rows";
524 xrpl::uint256
const& tokenID,
525 std::uint32_t
const limit,
527 std::optional<TransactionsCursor>
const& cursorIn,
528 boost::asio::yield_context yield
533 return {.txns = {}, .cursor = {}};
535 Statement const statement = [
this, forward, &tokenID] {
537 return schema_->selectNFTTxForward.bind(tokenID);
539 return schema_->selectNFTTx.bind(tokenID);
542 auto cursor = cursorIn;
544 statement.
bindAt(kTransactionCursorBindIndex, cursor->asTuple());
545 LOG(log_.debug()) <<
"token_id = " << xrpl::strHex(tokenID)
546 <<
" tuple = " << cursor->ledgerSequence << cursor->transactionIndex;
548 auto const seq = forward ? rng->minSequence : rng->maxSequence;
549 auto const placeHolder = forward ? 0 : std::numeric_limits<std::uint32_t>::max();
552 kTransactionCursorBindIndex, std::make_tuple(placeHolder, placeHolder)
554 LOG(log_.debug()) <<
"token_id = " << xrpl::strHex(tokenID) <<
" idx = " << seq
555 <<
" tuple = " << placeHolder;
558 statement.
bindAt(kTransactionLimitBindIndex,
Limit{limit});
560 auto const res = executor_.read(yield, statement);
561 auto const& results = res.value();
562 if (not results.hasRows()) {
563 LOG(log_.debug()) <<
"No rows returned";
567 std::vector<xrpl::uint256> hashes = {};
568 auto numRows = results.numRows();
569 LOG(log_.info()) <<
"num_rows = " << numRows;
571 for (
auto [hash,
data] :
extract<xrpl::uint256, std::tuple<uint32_t, uint32_t>>(results)) {
572 hashes.push_back(hash);
573 if (--numRows == 0) {
574 LOG(log_.debug()) <<
"Setting cursor";
580 ++cursor->transactionIndex;
585 LOG(log_.debug()) <<
"NFT Txns = " << txns.size();
587 if (txns.size() == limit) {
588 LOG(log_.debug()) <<
"Returning cursor";
589 return {txns, cursor};
607 xrpl::uint192
const& mptIssuanceID,
608 std::uint32_t
const limit,
610 std::optional<TransactionsCursor>
const& cursorIn,
611 boost::asio::yield_context yield
614 auto const statement = [
this, forward, &mptIssuanceID] {
616 return schema_->selectMPTokenIssuanceTxForward.bind(mptIssuanceID);
618 return schema_->selectMPTokenIssuanceTx.bind(mptIssuanceID);
622 kMPTokenIssuanceTxCursorBindIndex,
623 kMPTokenIssuanceTxLimitBindIndex,
645 xrpl::uint192
const& mptIssuanceID,
646 xrpl::AccountID
const& account,
647 std::uint32_t
const limit,
649 std::optional<TransactionsCursor>
const& cursorIn,
650 boost::asio::yield_context yield
653 auto const statement = [
this, forward, &mptIssuanceID, &account] {
655 return schema_->selectAccountMPTokenIssuanceTxForward.bind(mptIssuanceID, account);
657 return schema_->selectAccountMPTokenIssuanceTx.bind(mptIssuanceID, account);
661 kAccountMPTokenIssuanceTxCursorBindIndex,
662 kAccountMPTokenIssuanceTxLimitBindIndex,
683 xrpl::uint192
const& mptID,
684 std::uint32_t
const limit,
685 std::optional<xrpl::AccountID>
const& cursorIn,
686 std::uint32_t
const ledgerSequence,
687 boost::asio::yield_context yield
690 auto const holderEntries = executor_.read(
692 schema_->selectMPTHolders,
694 cursorIn.value_or(xrpl::AccountID(0)),
698 auto const& holderResults = holderEntries.value();
699 if (not holderResults.hasRows()) {
700 LOG(log_.debug()) <<
"No rows returned";
704 std::vector<xrpl::uint256> mptKeys;
705 std::optional<xrpl::AccountID> cursor;
707 mptKeys.push_back(xrpl::keylet::mptoken(mptID, holder).key);
713 auto const it = std::remove_if(mptObjects.begin(), mptObjects.end(), [](
Blob const& mpt) {
717 mptObjects.erase(it, mptObjects.end());
719 ASSERT(mptKeys.size() <= limit,
"Number of keys can't exceed the limit");
720 if (mptKeys.size() == limit)
721 return {mptObjects, cursor};
723 return {mptObjects, {}};
736 xrpl::uint256
const& key,
737 std::uint32_t
const sequence,
738 boost::asio::yield_context yield
741 LOG(log_.debug()) <<
"Fetching ledger object for seq " << sequence
742 <<
", key = " << xrpl::to_string(key);
743 if (
auto const res = executor_.read(yield, schema_->selectObject, key, sequence); res) {
744 if (
auto const result = res->template get<Blob>(); result) {
748 LOG(log_.debug()) <<
"Could not fetch ledger object - no rows";
751 LOG(log_.error()) <<
"Could not fetch ledger object: " << res.error();
766 std::optional<std::uint32_t>
768 xrpl::uint256
const& key,
769 std::uint32_t
const sequence,
770 boost::asio::yield_context yield
773 LOG(log_.debug()) <<
"Fetching ledger object for seq " << sequence
774 <<
", key = " << xrpl::to_string(key);
775 if (
auto const res = executor_.read(yield, schema_->selectObject, key, sequence); res) {
776 if (
auto const result = res->template get<Blob, std::uint32_t>(); result) {
777 auto [_, seq] = *result;
780 LOG(log_.debug()) <<
"Could not fetch ledger object sequence - no rows";
782 LOG(log_.error()) <<
"Could not fetch ledger object sequence: " << res.error();
795 std::optional<TransactionAndMetadata>
796 fetchTransaction(xrpl::uint256
const& hash, boost::asio::yield_context yield)
const override
798 if (
auto const res = executor_.read(yield, schema_->selectTransaction, hash); res) {
799 if (
auto const maybeValue = res->template get<Blob, Blob, uint32_t, uint32_t>();
801 auto [transaction, meta, seq, date] = *maybeValue;
802 return std::make_optional<TransactionAndMetadata>(transaction, meta, seq, date);
805 LOG(log_.debug()) <<
"Could not fetch transaction - no rows";
807 LOG(log_.error()) <<
"Could not fetch transaction: " << res.error();
821 std::optional<xrpl::uint256>
824 std::uint32_t
const ledgerSequence,
825 boost::asio::yield_context yield
828 if (
auto const res = executor_.read(yield, schema_->selectSuccessor, key, ledgerSequence);
830 if (
auto const result = res->template get<xrpl::uint256>(); result) {
836 LOG(log_.debug()) <<
"Could not fetch successor - no rows";
838 LOG(log_.error()) <<
"Could not fetch successor: " << res.error();
851 std::vector<TransactionAndMetadata>
853 std::vector<xrpl::uint256>
const& hashes,
854 boost::asio::yield_context yield
860 auto const numHashes = hashes.size();
861 std::vector<TransactionAndMetadata> results;
862 results.reserve(numHashes);
864 std::vector<Statement> statements;
865 statements.reserve(numHashes);
867 auto const timeDiff =
util::timed([
this, yield, &results, &hashes, &statements] {
872 std::back_inserter(statements),
873 [
this](
auto const& hash) {
return schema_->selectTransaction.bind(hash); }
876 auto const entries = executor_.readEach(yield, statements);
878 std::cbegin(entries),
880 std::back_inserter(results),
882 if (
auto const maybeRow = res.template get<Blob, Blob, uint32_t, uint32_t>();
891 ASSERT(numHashes == results.size(),
"Number of hashes and results must match");
892 LOG(log_.debug()) <<
"Fetched " << numHashes <<
" transactions from database in "
893 << timeDiff <<
" milliseconds";
907 std::vector<xrpl::uint256>
const& keys,
908 std::uint32_t
const sequence,
909 boost::asio::yield_context yield
915 auto const numKeys = keys.size();
916 LOG(log_.trace()) <<
"Fetching " << numKeys <<
" objects";
918 std::vector<Blob> results;
919 results.reserve(numKeys);
921 std::vector<Statement> statements;
922 statements.reserve(numKeys);
928 std::back_inserter(statements),
929 [
this, &sequence](
auto const& key) {
return schema_->selectObject.bind(key, sequence); }
932 auto const entries = executor_.readEach(yield, statements);
934 std::cbegin(entries),
936 std::back_inserter(results),
937 [](
auto const& res) ->
Blob {
938 if (
auto const maybeValue = res.template get<Blob>(); maybeValue)
945 LOG(log_.trace()) <<
"Fetched " << numKeys <<
" objects";
956 std::vector<LedgerObject>
958 std::uint32_t
const ledgerSequence,
959 boost::asio::yield_context yield
962 auto const [keys, timeDiff] =
963 util::timed([
this, &ledgerSequence, yield] -> std::vector<xrpl::uint256> {
964 auto const res = executor_.read(yield, schema_->selectDiff, ledgerSequence);
966 LOG(log_.error()) <<
"Could not fetch ledger diff: " << res.error()
967 <<
"; ledger = " << ledgerSequence;
971 auto const& results = res.value();
974 <<
"Could not fetch ledger diff - no rows; ledger = " << ledgerSequence;
978 std::vector<xrpl::uint256> resultKeys;
980 resultKeys.push_back(key);
989 LOG(log_.debug()) <<
"Fetched " << keys.size() <<
" diff hashes from database in "
990 << timeDiff <<
" milliseconds";
993 std::vector<LedgerObject> results;
994 results.reserve(keys.size());
1000 std::back_inserter(results),
1001 [](
auto const& key,
auto const& obj) {
return LedgerObject{key, obj}; }
1014 std::optional<std::string>
1016 std::string
const& migratorName,
1017 boost::asio::yield_context yield
1020 auto const res = executor_.read(yield, schema_->selectMigratorStatus,
Text(migratorName));
1022 LOG(log_.error()) <<
"Could not fetch migrator status: " << res.error();
1026 auto const& results = res.value();
1032 return statusString;
1043 std::expected<std::vector<std::pair<boost::uuids::uuid, std::string>>, std::string>
1046 auto const readResult = executor_.read(yield, schema_->selectClioNodesData);
1048 return std::unexpected{readResult.error().message()};
1050 std::vector<std::pair<boost::uuids::uuid, std::string>> result;
1053 result.emplace_back(uuid, std::move(message));
1070 LOG(log_.trace()) <<
" Writing ledger object " << key.size() <<
":" << seq <<
" ["
1071 << blob.size() <<
" bytes]";
1074 executor_.write(schema_->insertDiff, seq, key);
1076 executor_.write(schema_->insertObject, std::move(key), seq, std::move(blob));
1087 writeSuccessor(std::string&& key, std::uint32_t
const seq, std::string&& successor)
override
1089 LOG(log_.trace()) <<
"Writing successor. key = " << key.size() <<
" bytes. "
1090 <<
" seq = " << std::to_string(seq) <<
" successor = " << successor.size()
1092 ASSERT(!key.empty(),
"Key must not be empty");
1093 ASSERT(!successor.empty(),
"Successor must not be empty");
1095 executor_.write(schema_->insertSuccessor, std::move(key), seq, std::move(successor));
1106 std::vector<Statement> statements;
1107 statements.reserve(
data.size() * 10);
1109 for (
auto& record :
data) {
1110 std::ranges::transform(
1111 record.accounts, std::back_inserter(statements), [
this, &record](
auto&& account) {
1112 return schema_->insertAccountTx.bind(
1113 std::forward<decltype(account)>(account),
1114 std::make_tuple(record.ledgerSequence, record.transactionIndex),
1121 executor_.write(std::move(statements));
1132 std::vector<Statement> statements;
1133 statements.reserve(record.
accounts.size());
1135 std::ranges::transform(
1136 record.
accounts, std::back_inserter(statements), [
this, &record](
auto&& account) {
1137 return schema_->insertAccountTx.bind(
1138 std::forward<decltype(account)>(account),
1139 std::make_tuple(record.ledgerSequence, record.transactionIndex),
1145 executor_.write(std::move(statements));
1156 std::vector<Statement> statements;
1157 statements.reserve(
data.size());
1159 std::ranges::transform(
data, std::back_inserter(statements), [
this](
auto const& record) {
1160 return schema_->insertNFTTx.bind(
1162 std::make_tuple(record.ledgerSequence, record.transactionIndex),
1167 executor_.write(std::move(statements));
1178 std::vector<MPTokenIssuanceTransactionsData>
const&
data
1181 std::vector<Statement> statements;
1182 statements.reserve(
data.size());
1184 std::ranges::transform(
data, std::back_inserter(statements), [
this](
auto const& record) {
1185 return schema_->insertMPTokenIssuanceTx.bind(
1186 record.mptIssuanceID,
1187 std::make_tuple(record.ledgerSequence, record.transactionIndex),
1192 executor_.write(std::move(statements));
1205 std::vector<MPTokenIssuanceTransactionsData>
const&
data
1208 std::size_t numStatements = 0u;
1209 for (
auto const& record :
data)
1210 numStatements += record.accounts.size();
1212 std::vector<Statement> statements;
1213 statements.reserve(numStatements);
1215 for (
auto const& record :
data) {
1216 std::ranges::transform(
1218 std::back_inserter(statements),
1219 [
this, &record](
auto const& account) {
1220 return schema_->insertAccountMPTokenIssuanceTx.bind(
1221 record.mptIssuanceID,
1223 std::make_tuple(record.ledgerSequence, record.transactionIndex),
1230 executor_.write(std::move(statements));
1245 std::uint32_t
const seq,
1246 std::uint32_t
const date,
1247 std::string&& transaction,
1248 std::string&& metadata
1251 LOG(log_.trace()) <<
"Writing txn to database";
1253 executor_.write(schema_->insertLedgerTransaction, seq, hash);
1255 schema_->insertTransaction,
1259 std::move(transaction),
1272 std::vector<Statement> statements;
1273 statements.reserve(
data.size() * 3);
1276 if (!record.onlyUriChanged) {
1277 statements.push_back(schema_->insertNFT.bind(
1278 record.tokenID, record.ledgerSequence, record.owner, record.isBurned
1287 statements.push_back(schema_->insertIssuerNFT.bind(
1288 xrpl::nft::getIssuer(record.tokenID),
1289 static_cast<uint32_t
>(xrpl::nft::getTaxon(record.tokenID)),
1292 statements.push_back(schema_->insertNFTURI.bind(
1293 record.tokenID, record.ledgerSequence, *record.uri
1298 statements.push_back(
1300 schema_->insertNFTURI.bind(record.tokenID, record.ledgerSequence, *record.uri)
1305 executor_.writeEach(std::move(statements));
1316 std::vector<Statement> statements;
1317 statements.reserve(
data.size());
1318 for (
auto [mptId, holder] :
data)
1319 statements.push_back(schema_->insertMPTHolder.bind(mptId, holder));
1321 executor_.write(std::move(statements));
1343 executor_.writeSync(
1344 schema_->insertMigratorStatus,
1359 executor_.writeSync(
1372 return executor_.isTooBusy();
1383 return executor_.stats();
1396 auto const res = executor_.writeSync(statement);
1397 auto maybeSuccess = res->template get<bool>();
1398 if (not maybeSuccess) {
1399 LOG(log_.error()) <<
"executeSyncUpdate - error getting result - no row";
1403 if (not *maybeSuccess) {
1404 LOG(log_.warn()) <<
"Update failed. Checking if DB state is what we expect";
1411 return rng && rng->maxSequence == ledgerSequence_;
1436 std::size_t
const cursorIdx,
1437 std::size_t
const limitIdx,
1438 std::uint32_t
const limit,
1440 std::optional<TransactionsCursor>
const& cursorIn,
1441 boost::asio::yield_context yield
1446 return {.txns = {}, .cursor = {}};
1448 auto cursor = cursorIn;
1449 if (cursor.has_value()) {
1450 statement.
bindAt(cursorIdx, cursor->asTuple());
1454 auto const ledgerSequence = forward ? rng->minSequence : rng->maxSequence;
1455 auto const transactionIndex = forward ? 0u : std::numeric_limits<std::uint32_t>::max();
1456 statement.
bindAt(cursorIdx, std::make_tuple(ledgerSequence, transactionIndex));
1461 auto const res = executor_.read(yield, statement);
1462 auto const& results = res.value();
1463 if (not results.hasRows()) {
1464 LOG(log_.debug()) <<
"No rows returned";
1468 std::vector<xrpl::uint256> hashes = {};
1469 auto numRows = results.numRows();
1471 for (
auto const& [hash,
data] :
1472 extract<xrpl::uint256, std::tuple<uint32_t, uint32_t>>(results)) {
1473 hashes.push_back(hash);
1475 if (--numRows == 0) {
1476 LOG(log_.debug()) <<
"Setting cursor";
1482 ++cursor->transactionIndex;
1487 LOG(log_.debug()) <<
"MPTokenIssuance Txns = " << txns.size();
1489 if (txns.size() == limit) {
1490 LOG(log_.debug()) <<
"Returning cursor";
1491 return {std::move(txns), cursor};
1494 return {std::move(txns), {}};
BackendInterface(LedgerCacheInterface &cache)
Construct a new backend interface instance.
Definition BackendInterface.hpp:225
std::optional< LedgerRange > hardFetchLedgerRangeNoThrow() const
Fetches the ledger range from DB retrying until no DatabaseError is thrown.
Definition BackendInterface.cpp:53
std::optional< LedgerRange > fetchLedgerRange() const
Fetch the current ledger range.
Definition BackendInterface.cpp:251
std::vector< Blob > fetchLedgerObjects(std::vector< xrpl::uint256 > const &keys, std::uint32_t sequence, boost::asio::yield_context yield) const
Fetches all ledger objects by their keys.
Definition BackendInterface.cpp:95
LedgerCacheInterface const & cache() const
Definition BackendInterface.hpp:256
A simple cache holding one xrpl::LedgerHeader to reduce DB lookups.
Definition LedgerHeaderCache.hpp:22
Cache for an entire ledger.
Definition LedgerCacheInterface.hpp:21
void writeMigratorStatus(std::string const &migratorName, std::string const &status) override
Write the migration status of a migrator to the database.
Definition CassandraBackendFamily.hpp:1341
std::optional< LedgerRange > hardFetchLedgerRange(boost::asio::yield_context yield) const override
Fetches the ledger range from DB.
Definition CassandraBackendFamily.hpp:367
std::optional< xrpl::LedgerHeader > fetchLedgerBySequence(std::uint32_t const sequence, boost::asio::yield_context yield) const override
Fetches a specific ledger by sequence number, using the ledger header cache first.
Definition CassandraBackendFamily.hpp:303
TransactionsAndCursor fetchAccountMPTokenIssuanceTransactions(xrpl::uint192 const &mptIssuanceID, xrpl::AccountID const &account, std::uint32_t const limit, bool const forward, std::optional< TransactionsCursor > const &cursorIn, boost::asio::yield_context yield) const override
Fetches transactions for a particular MPTokenIssuance ID involving a particular account.
Definition CassandraBackendFamily.hpp:644
std::optional< NFT > fetchNFT(xrpl::uint256 const &tokenID, std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Fetches a specific NFT.
Definition CassandraBackendFamily.hpp:474
void startWrites() const override
Starts a write transaction with the DB. No-op for Cassandra.
Definition CassandraBackendFamily.hpp:1328
void doWriteLedgerObject(std::string &&key, std::uint32_t const seq, std::string &&blob) override
Write a ledger object version to objects and, once the ledger range is known, record its key in diff.
Definition CassandraBackendFamily.hpp:1068
std::chrono::milliseconds initialRetryDelay() const override
Definition CassandraBackendFamily.hpp:149
std::optional< std::uint32_t > doFetchLedgerObjectSeq(xrpl::uint256 const &key, std::uint32_t const sequence, boost::asio::yield_context yield) const override
The database-specific implementation for fetching a ledger object sequence.
Definition CassandraBackendFamily.hpp:767
std::optional< xrpl::LedgerHeader > fetchLedgerByHash(xrpl::uint256 const &hash, boost::asio::yield_context yield) const override
Fetches a specific ledger by hash.
Definition CassandraBackendFamily.hpp:341
TransactionsAndCursor fetchAccountTransactions(xrpl::AccountID const &account, std::uint32_t const limit, bool forward, std::optional< TransactionsCursor > const &txnCursor, boost::asio::yield_context yield) const override
Fetches all transactions for a specific account.
Definition CassandraBackendFamily.hpp:174
std::optional< Blob > doFetchLedgerObject(xrpl::uint256 const &key, std::uint32_t const sequence, boost::asio::yield_context yield) const override
The database-specific implementation for fetching a ledger object.
Definition CassandraBackendFamily.hpp:735
void writeNFTs(std::vector< NFTsData > const &data) override
Writes NFTs to the database.
Definition CassandraBackendFamily.hpp:1270
void writeNFTTransactions(std::vector< NFTTransactionsData > const &data) override
Write NFTs transactions.
Definition CassandraBackendFamily.hpp:1154
void writeNodeMessage(boost::uuids::uuid const &uuid, std::string message) override
Write a node message. Used by ClusterCommunicationService.
Definition CassandraBackendFamily.hpp:1357
std::optional< std::uint32_t > fetchLatestLedgerSequence(boost::asio::yield_context yield) const override
Fetches the latest ledger sequence.
Definition CassandraBackendFamily.hpp:276
CassandraBackendFamily(SettingsProviderType settingsProvider, data::LedgerCacheInterface &cache, bool readOnly)
Create a new cassandra/scylla backend instance.
Definition CassandraBackendFamily.hpp:98
void writeAccountMPTokenIssuanceTransactions(std::vector< MPTokenIssuanceTransactionsData > const &data) override
Write MPTokenIssuance transaction index rows to the account_mptoken_issuance_transactions table.
Definition CassandraBackendFamily.hpp:1204
MPTHoldersAndCursor fetchMPTHolders(xrpl::uint192 const &mptID, std::uint32_t const limit, std::optional< xrpl::AccountID > const &cursorIn, std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Fetches a page of MPToken objects held for an MPTokenIssuance ID.
Definition CassandraBackendFamily.hpp:682
std::optional< xrpl::uint256 > doFetchSuccessorKey(xrpl::uint256 key, std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Database-specific implementation of fetching the successor key.
Definition CassandraBackendFamily.hpp:822
TransactionsAndCursor fetchNFTTransactions(xrpl::uint256 const &tokenID, std::uint32_t const limit, bool const forward, std::optional< TransactionsCursor > const &cursorIn, boost::asio::yield_context yield) const override
Fetches all transactions for a specific NFT.
Definition CassandraBackendFamily.hpp:523
bool isTooBusy() const override
Check whether the database is overwhelmed with requests.
Definition CassandraBackendFamily.hpp:1370
void writeMPTHolders(std::vector< MPTHolderData > const &data) override
Write accounts that started holding onto a MPT.
Definition CassandraBackendFamily.hpp:1314
void writeAccountTransaction(AccountTransactionsData record) override
Write a new account transaction.
Definition CassandraBackendFamily.hpp:1130
void writeSuccessor(std::string &&key, std::uint32_t const seq, std::string &&successor) override
Write a new successor.
Definition CassandraBackendFamily.hpp:1087
void waitForWritesToFinish() override
Wait for all pending writes to finish.
Definition CassandraBackendFamily.hpp:247
TransactionsAndCursor fetchMPTokenIssuanceTransactions(xrpl::uint192 const &mptIssuanceID, std::uint32_t const limit, bool const forward, std::optional< TransactionsCursor > const &cursorIn, boost::asio::yield_context yield) const override
Fetches transactions for a particular MPTokenIssuance ID.
Definition CassandraBackendFamily.hpp:606
void writeLedger(xrpl::LedgerHeader const &ledgerHeader, std::string &&blob) override
Write a ledger header and its hash-to-sequence mapping, and set the ledger that doFinishWrites() will...
Definition CassandraBackendFamily.hpp:260
std::optional< TransactionAndMetadata > fetchTransaction(xrpl::uint256 const &hash, boost::asio::yield_context yield) const override
Fetches a specific transaction.
Definition CassandraBackendFamily.hpp:796
TransactionsAndCursor fetchMPTokenIssuanceTransactionsImpl(Statement const &statement, std::size_t const cursorIdx, std::size_t const limitIdx, std::uint32_t const limit, bool const forward, std::optional< TransactionsCursor > const &cursorIn, boost::asio::yield_context yield) const
Shared implementation of the two MPTokenIssuance transaction-index fetchers.
Definition CassandraBackendFamily.hpp:1434
boost::json::object stats() const override
Get backend usage statistics collected by the execution strategy.
Definition CassandraBackendFamily.hpp:1381
std::optional< std::string > fetchMigratorStatus(std::string const &migratorName, boost::asio::yield_context yield) const override
Fetches the status of migrator by name.
Definition CassandraBackendFamily.hpp:1015
std::chrono::milliseconds maxRetryDelay() const override
Definition CassandraBackendFamily.hpp:158
std::expected< std::vector< std::pair< boost::uuids::uuid, std::string > >, std::string > fetchClioNodesData(boost::asio::yield_context yield) const override
Fetches the data of all nodes in the cluster.
Definition CassandraBackendFamily.hpp:1044
void writeAccountTransactions(std::vector< AccountTransactionsData > data) override
Write a new set of account transactions.
Definition CassandraBackendFamily.hpp:1104
std::vector< Blob > doFetchLedgerObjects(std::vector< xrpl::uint256 > const &keys, std::uint32_t const sequence, boost::asio::yield_context yield) const override
The database-specific implementation for fetching ledger objects.
Definition CassandraBackendFamily.hpp:906
CassandraBackendFamily(CassandraBackendFamily &&)=delete
Move constructor is deleted because handle_ is shared by reference with executor.
bool executeSyncUpdate(Statement statement)
Executes statements and tries to write to DB.
Definition CassandraBackendFamily.hpp:1394
void writeMPTokenIssuanceTransactions(std::vector< MPTokenIssuanceTransactionsData > const &data) override
Write MPTokenIssuance transaction index rows to the mptoken_issuance_transactions table.
Definition CassandraBackendFamily.hpp:1177
void writeTransaction(std::string &&hash, std::uint32_t const seq, std::uint32_t const date, std::string &&transaction, std::string &&metadata) override
Writes a new transaction.
Definition CassandraBackendFamily.hpp:1243
std::vector< xrpl::uint256 > fetchAllTransactionHashesInLedger(std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Fetches all transaction hashes from a specific ledger.
Definition CassandraBackendFamily.hpp:429
std::vector< TransactionAndMetadata > fetchTransactions(std::vector< xrpl::uint256 > const &hashes, boost::asio::yield_context yield) const override
Fetches multiple transactions.
Definition CassandraBackendFamily.hpp:852
std::vector< LedgerObject > fetchLedgerDiff(std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Fetch the objects changed in the given ledger.
Definition CassandraBackendFamily.hpp:957
std::vector< TransactionAndMetadata > fetchAllTransactionsInLedger(std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Fetches all transactions from a specific ledger.
Definition CassandraBackendFamily.hpp:412
Represents a handle to the cassandra database cluster.
Definition Handle.hpp:27
void bindAt(std::size_t const idx, Type &&value) const
Binds an argument to a specific index.
Definition Statement.hpp:76
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
The requirements of an execution strategy.
Definition Concepts.hpp:39
The requirements of a settings provider.
Definition Concepts.hpp:26
This namespace implements a wrapper for the Cassandra C++ driver.
Definition CassandraBackendFamily.hpp:50
impl::ResultExtractor< Types... > extract(Handle::ResultType const &result)
Extracts the results into series of std::tuple<Types...> by creating a simple wrapper with an STL inp...
Definition Handle.hpp:318
impl::Statement Statement
A simple or bound statement.
Definition Types.hpp:27
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:56
constexpr xrpl::uint256 kLastKey
The largest possible ledger object key; used as the end of the key space.
Definition Types.hpp:339
std::vector< unsigned char > Blob
Raw serialized data, e.g. a ledger object, transaction or metadata.
Definition Types.hpp:20
xrpl::LedgerHeader deserializeHeader(xrpl::Slice data)
Deserializes a xrpl::LedgerHeader from xrpl::Slice of data.
Definition LedgerUtils.hpp:76
auto timed(FnType &&func)
Profiler function to measure the time a function execution consumes.
Definition Profiler.hpp:21
Struct used to keep track of what to write to account_transactions/account_tx tables.
Definition DBHelpers.hpp:26
boost::container::flat_set< xrpl::AccountID > accounts
Accounts affected by the transaction.
Definition DBHelpers.hpp:27
Represents an NFT state at a particular ledger.
Definition DBHelpers.hpp:93
Struct to store ledger header cache entry and the sequence it belongs to.
Definition LedgerHeaderCache.hpp:29
Represents an object in the ledger.
Definition Types.hpp:25
Stores a range of sequences as a min and max pair.
Definition Types.hpp:246
std::uint32_t minSequence
Lowest ledger sequence in the range (inclusive).
Definition Types.hpp:247
std::uint32_t maxSequence
Highest ledger sequence in the range (inclusive).
Definition Types.hpp:248
Represents an array of MPTokens.
Definition Types.hpp:238
Represests a bundle of transactions with metadata and a cursor to the next page.
Definition Types.hpp:156
A strong type wrapper for int32_t.
Definition Types.hpp:38
A strong type wrapper for string.
Definition Types.hpp:49