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>
45class CacheBackendCassandraTest;
68 SettingsProviderType settingsProvider_;
70 std::atomic_uint32_t ledgerSequence_ = 0u;
71 friend class ::CacheBackendCassandraTest;
76 mutable ExecutionStrategyType executor_;
78 mutable FetchLedgerCacheType ledgerCache_{};
80 static constexpr std::size_t kTransactionCursorBindIndex = 1;
81 static constexpr std::size_t kTransactionLimitBindIndex = 2;
82 static constexpr std::size_t kMPTokenIssuanceTxCursorBindIndex = 1;
83 static constexpr std::size_t kMPTokenIssuanceTxLimitBindIndex = 2;
84 static constexpr std::size_t kAccountMPTokenIssuanceTxCursorBindIndex = 2;
85 static constexpr std::size_t kAccountMPTokenIssuanceTxLimitBindIndex = 3;
96 SettingsProviderType settingsProvider,
101 , settingsProvider_{std::move(settingsProvider)}
102 , schema_{settingsProvider_}
103 , handle_{settingsProvider_.getSettings()}
104 , executor_{settingsProvider_.getSettings(), handle_}
106 if (
auto const res = handle_.connect(); not res.has_value())
107 throw std::runtime_error(
"Could not connect to database: " + res.error());
110 if (
auto const res = handle_.execute(schema_.createKeyspace); not res.has_value()) {
113 if (res.error().code() != CASS_ERROR_SERVER_UNAUTHORIZED)
114 throw std::runtime_error(
"Could not create keyspace: " + res.error());
117 if (
auto const res = handle_.executeEach(schema_.createSchema); not res.has_value())
118 throw std::runtime_error(
"Could not create schema: " + res.error());
122 schema_.prepareStatements(handle_);
123 }
catch (std::runtime_error
const& ex) {
124 auto const error = fmt::format(
125 "Failed to prepare the statements: {}; readOnly: {}. ReadOnly should be turned off "
127 "node with write access to DB should be started first.",
131 LOG(log_.error()) << error;
132 throw std::runtime_error(error);
134 LOG(log_.info()) <<
"Created (revamped) CassandraBackend";
144 xrpl::AccountID
const& account,
145 std::uint32_t
const limit,
147 std::optional<TransactionsCursor>
const& txnCursor,
148 boost::asio::yield_context yield
153 return {.txns = {}, .cursor = {}};
155 Statement
const statement = [
this, forward, &account]() {
157 return schema_->selectAccountTxForward.bind(account);
159 return schema_->selectAccountTx.bind(account);
162 auto cursor = txnCursor;
164 statement.
bindAt(kTransactionCursorBindIndex, cursor->asTuple());
165 LOG(log_.debug()) <<
"account = " << xrpl::strHex(account)
166 <<
" tuple = " << cursor->ledgerSequence << cursor->transactionIndex;
168 auto const seq = forward ? rng->minSequence : rng->maxSequence;
169 auto const placeHolder = forward ? 0u : std::numeric_limits<std::uint32_t>::max();
172 kTransactionCursorBindIndex, std::make_tuple(placeHolder, placeHolder)
174 LOG(log_.debug()) <<
"account = " << xrpl::strHex(account) <<
" idx = " << seq
175 <<
" tuple = " << placeHolder;
181 statement.
bindAt(kTransactionLimitBindIndex,
Limit{limit});
182 auto const res = executor_.read(yield, statement);
183 auto const& results = res.value();
184 if (not results.hasRows()) {
185 LOG(log_.debug()) <<
"No rows returned";
189 std::vector<xrpl::uint256> hashes = {};
190 auto numRows = results.numRows();
191 LOG(log_.info()) <<
"num_rows = " << numRows;
193 for (
auto [hash,
data] :
extract<xrpl::uint256, std::tuple<uint32_t, uint32_t>>(results)) {
194 hashes.push_back(hash);
195 if (--numRows == 0) {
196 LOG(log_.debug()) <<
"Setting cursor";
202 LOG(log_.debug()) <<
"Txns = " << txns.size();
204 if (txns.size() == limit) {
205 LOG(log_.debug()) <<
"Returning cursor";
206 return {txns, cursor};
219 writeLedger(xrpl::LedgerHeader
const& ledgerHeader, std::string&& blob)
override
221 executor_.write(schema_->insertLedgerHeader, ledgerHeader.seq, std::move(blob));
223 executor_.write(schema_->insertLedgerHash, ledgerHeader.hash, ledgerHeader.seq);
225 ledgerSequence_ = ledgerHeader.seq;
228 std::optional<std::uint32_t>
231 if (
auto const res = executor_.read(yield, schema_->selectLatestLedger); res.has_value()) {
232 if (
auto const& rows = *res; rows) {
233 if (
auto const maybeRow = rows.template get<uint32_t>(); maybeRow.has_value())
236 LOG(log_.error()) <<
"Could not fetch latest ledger - no rows";
240 LOG(log_.error()) <<
"Could not fetch latest ledger - no result";
242 LOG(log_.error()) <<
"Could not fetch latest ledger: " << res.error();
248 std::optional<xrpl::LedgerHeader>
250 std::uint32_t
const sequence,
251 boost::asio::yield_context yield
254 if (
auto const lock = ledgerCache_.get(); lock.has_value() && lock->seq == sequence)
257 auto const res = executor_.read(yield, schema_->selectLedgerBySeq, sequence);
259 if (
auto const& result = res.value(); result) {
260 if (
auto const maybeValue = result.template get<std::vector<unsigned char>>();
267 LOG(log_.error()) <<
"Could not fetch ledger by sequence - no rows";
271 LOG(log_.error()) <<
"Could not fetch ledger by sequence - no result";
273 LOG(log_.error()) <<
"Could not fetch ledger by sequence: " << res.error();
279 std::optional<xrpl::LedgerHeader>
282 if (
auto const res = executor_.read(yield, schema_->selectLedgerByHash, hash); res) {
283 if (
auto const& result = res.value(); result) {
284 if (
auto const maybeValue = result.template get<uint32_t>(); maybeValue)
287 LOG(log_.error()) <<
"Could not fetch ledger by hash - no rows";
291 LOG(log_.error()) <<
"Could not fetch ledger by hash - no result";
293 LOG(log_.error()) <<
"Could not fetch ledger by hash: " << res.error();
299 std::optional<LedgerRange>
302 auto const res = executor_.read(yield, schema_->selectLedgerRange);
304 auto const& results = res.value();
305 if (not results.hasRows()) {
306 LOG(log_.debug()) <<
"Could not fetch ledger range - no rows";
317 range.maxSequence = range.minSequence = seq;
318 }
else if (idx == 1) {
319 range.maxSequence = seq;
325 if (range.minSequence > range.maxSequence)
326 std::swap(range.minSequence, range.maxSequence);
328 LOG(log_.debug()) <<
"After hardFetchLedgerRange range is " << range.minSequence <<
":"
329 << range.maxSequence;
332 LOG(log_.error()) <<
"Could not fetch ledger range: " << res.error();
337 std::vector<TransactionAndMetadata>
339 std::uint32_t
const ledgerSequence,
340 boost::asio::yield_context yield
347 std::vector<xrpl::uint256>
349 std::uint32_t
const ledgerSequence,
350 boost::asio::yield_context yield
353 auto start = std::chrono::system_clock::now();
355 executor_.read(yield, schema_->selectAllTransactionHashesInLedger, ledgerSequence);
358 LOG(log_.error()) <<
"Could not fetch all transaction hashes: " << res.error();
362 auto const& result = res.value();
363 if (not result.hasRows()) {
364 LOG(log_.warn()) <<
"Could not fetch all transaction hashes - no rows; ledger = "
365 << std::to_string(ledgerSequence);
369 std::vector<xrpl::uint256> hashes;
371 hashes.push_back(std::move(hash));
373 auto end = std::chrono::system_clock::now();
377 << hashes.size() <<
" transaction hashes from database in "
378 << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
386 xrpl::uint256
const& tokenID,
387 std::uint32_t
const ledgerSequence,
388 boost::asio::yield_context yield
391 auto const res = executor_.read(yield, schema_->selectNFT, tokenID, ledgerSequence);
395 if (
auto const maybeRow = res->template get<uint32_t, xrpl::AccountID, bool>(); maybeRow) {
396 auto [seq, owner, isBurned] = *maybeRow;
397 auto result = std::make_optional<NFT>(tokenID, seq, owner, isBurned);
410 auto uriRes = executor_.read(yield, schema_->selectNFTURI, tokenID, ledgerSequence);
412 if (
auto const maybeUri = uriRes->template get<xrpl::Blob>(); maybeUri)
413 result->uri = *maybeUri;
419 LOG(log_.error()) <<
"Could not fetch NFT - no rows";
425 xrpl::uint256
const& tokenID,
426 std::uint32_t
const limit,
428 std::optional<TransactionsCursor>
const& cursorIn,
429 boost::asio::yield_context yield
434 return {.txns = {}, .cursor = {}};
436 Statement
const statement = [
this, forward, &tokenID]() {
438 return schema_->selectNFTTxForward.bind(tokenID);
440 return schema_->selectNFTTx.bind(tokenID);
443 auto cursor = cursorIn;
445 statement.
bindAt(kTransactionCursorBindIndex, cursor->asTuple());
446 LOG(log_.debug()) <<
"token_id = " << xrpl::strHex(tokenID)
447 <<
" tuple = " << cursor->ledgerSequence << cursor->transactionIndex;
449 auto const seq = forward ? rng->minSequence : rng->maxSequence;
450 auto const placeHolder = forward ? 0 : std::numeric_limits<std::uint32_t>::max();
453 kTransactionCursorBindIndex, std::make_tuple(placeHolder, placeHolder)
455 LOG(log_.debug()) <<
"token_id = " << xrpl::strHex(tokenID) <<
" idx = " << seq
456 <<
" tuple = " << placeHolder;
459 statement.
bindAt(kTransactionLimitBindIndex,
Limit{limit});
461 auto const res = executor_.read(yield, statement);
462 auto const& results = res.value();
463 if (not results.hasRows()) {
464 LOG(log_.debug()) <<
"No rows returned";
468 std::vector<xrpl::uint256> hashes = {};
469 auto numRows = results.numRows();
470 LOG(log_.info()) <<
"num_rows = " << numRows;
472 for (
auto [hash,
data] :
extract<xrpl::uint256, std::tuple<uint32_t, uint32_t>>(results)) {
473 hashes.push_back(hash);
474 if (--numRows == 0) {
475 LOG(log_.debug()) <<
"Setting cursor";
481 ++cursor->transactionIndex;
486 LOG(log_.debug()) <<
"NFT Txns = " << txns.size();
488 if (txns.size() == limit) {
489 LOG(log_.debug()) <<
"Returning cursor";
490 return {txns, cursor};
498 xrpl::uint192
const& mptIssuanceID,
499 std::uint32_t
const limit,
501 std::optional<TransactionsCursor>
const& cursorIn,
502 boost::asio::yield_context yield
505 auto const statement = [
this, forward, &mptIssuanceID]() {
507 return schema_->selectMPTokenIssuanceTxForward.bind(mptIssuanceID);
509 return schema_->selectMPTokenIssuanceTx.bind(mptIssuanceID);
513 kMPTokenIssuanceTxCursorBindIndex,
514 kMPTokenIssuanceTxLimitBindIndex,
524 xrpl::uint192
const& mptIssuanceID,
525 xrpl::AccountID
const& account,
526 std::uint32_t
const limit,
528 std::optional<TransactionsCursor>
const& cursorIn,
529 boost::asio::yield_context yield
532 auto const statement = [
this, forward, &mptIssuanceID, &account]() {
534 return schema_->selectAccountMPTokenIssuanceTxForward.bind(mptIssuanceID, account);
536 return schema_->selectAccountMPTokenIssuanceTx.bind(mptIssuanceID, account);
540 kAccountMPTokenIssuanceTxCursorBindIndex,
541 kAccountMPTokenIssuanceTxLimitBindIndex,
551 xrpl::uint192
const& mptID,
552 std::uint32_t
const limit,
553 std::optional<xrpl::AccountID>
const& cursorIn,
554 std::uint32_t
const ledgerSequence,
555 boost::asio::yield_context yield
558 auto const holderEntries = executor_.read(
560 schema_->selectMPTHolders,
562 cursorIn.value_or(xrpl::AccountID(0)),
566 auto const& holderResults = holderEntries.value();
567 if (not holderResults.hasRows()) {
568 LOG(log_.debug()) <<
"No rows returned";
572 std::vector<xrpl::uint256> mptKeys;
573 std::optional<xrpl::AccountID> cursor;
575 mptKeys.push_back(xrpl::keylet::mptoken(mptID, holder).key);
581 auto it = std::remove_if(mptObjects.begin(), mptObjects.end(), [](Blob
const& mpt) {
585 mptObjects.erase(it, mptObjects.end());
587 ASSERT(mptKeys.size() <= limit,
"Number of keys can't exceed the limit");
588 if (mptKeys.size() == limit)
589 return {mptObjects, cursor};
591 return {mptObjects, {}};
596 xrpl::uint256
const& key,
597 std::uint32_t
const sequence,
598 boost::asio::yield_context yield
601 LOG(log_.debug()) <<
"Fetching ledger object for seq " << sequence
602 <<
", key = " << xrpl::to_string(key);
603 if (
auto const res = executor_.read(yield, schema_->selectObject, key, sequence); res) {
604 if (
auto const result = res->template get<Blob>(); result) {
608 LOG(log_.debug()) <<
"Could not fetch ledger object - no rows";
611 LOG(log_.error()) <<
"Could not fetch ledger object: " << res.error();
617 std::optional<std::uint32_t>
619 xrpl::uint256
const& key,
620 std::uint32_t
const sequence,
621 boost::asio::yield_context yield
624 LOG(log_.debug()) <<
"Fetching ledger object for seq " << sequence
625 <<
", key = " << xrpl::to_string(key);
626 if (
auto const res = executor_.read(yield, schema_->selectObject, key, sequence); res) {
627 if (
auto const result = res->template get<Blob, std::uint32_t>(); result) {
628 auto [_, seq] = *result;
631 LOG(log_.debug()) <<
"Could not fetch ledger object sequence - no rows";
633 LOG(log_.error()) <<
"Could not fetch ledger object sequence: " << res.error();
639 std::optional<TransactionAndMetadata>
640 fetchTransaction(xrpl::uint256
const& hash, boost::asio::yield_context yield)
const override
642 if (
auto const res = executor_.read(yield, schema_->selectTransaction, hash); res) {
643 if (
auto const maybeValue = res->template get<Blob, Blob, uint32_t, uint32_t>();
645 auto [transaction, meta, seq, date] = *maybeValue;
646 return std::make_optional<TransactionAndMetadata>(transaction, meta, seq, date);
649 LOG(log_.debug()) <<
"Could not fetch transaction - no rows";
651 LOG(log_.error()) <<
"Could not fetch transaction: " << res.error();
657 std::optional<xrpl::uint256>
660 std::uint32_t
const ledgerSequence,
661 boost::asio::yield_context yield
664 if (
auto const res = executor_.read(yield, schema_->selectSuccessor, key, ledgerSequence);
666 if (
auto const result = res->template get<xrpl::uint256>(); result) {
667 if (*result == kLastKey)
672 LOG(log_.debug()) <<
"Could not fetch successor - no rows";
674 LOG(log_.error()) <<
"Could not fetch successor: " << res.error();
680 std::vector<TransactionAndMetadata>
682 std::vector<xrpl::uint256>
const& hashes,
683 boost::asio::yield_context yield
689 auto const numHashes = hashes.size();
690 std::vector<TransactionAndMetadata> results;
691 results.reserve(numHashes);
693 std::vector<Statement> statements;
694 statements.reserve(numHashes);
696 auto const timeDiff =
util::timed([
this, yield, &results, &hashes, &statements]() {
701 std::back_inserter(statements),
702 [
this](
auto const& hash) {
return schema_->selectTransaction.bind(hash); }
705 auto const entries = executor_.readEach(yield, statements);
707 std::cbegin(entries),
709 std::back_inserter(results),
711 if (
auto const maybeRow = res.template get<Blob, Blob, uint32_t, uint32_t>();
720 ASSERT(numHashes == results.size(),
"Number of hashes and results must match");
721 LOG(log_.debug()) <<
"Fetched " << numHashes <<
" transactions from database in "
722 << timeDiff <<
" milliseconds";
728 std::vector<xrpl::uint256>
const& keys,
729 std::uint32_t
const sequence,
730 boost::asio::yield_context yield
736 auto const numKeys = keys.size();
737 LOG(log_.trace()) <<
"Fetching " << numKeys <<
" objects";
739 std::vector<Blob> results;
740 results.reserve(numKeys);
742 std::vector<Statement> statements;
743 statements.reserve(numKeys);
749 std::back_inserter(statements),
750 [
this, &sequence](
auto const& key) {
return schema_->selectObject.bind(key, sequence); }
753 auto const entries = executor_.readEach(yield, statements);
755 std::cbegin(entries),
757 std::back_inserter(results),
758 [](
auto const& res) -> Blob {
759 if (
auto const maybeValue = res.template get<Blob>(); maybeValue)
766 LOG(log_.trace()) <<
"Fetched " << numKeys <<
" objects";
770 std::vector<LedgerObject>
772 std::uint32_t
const ledgerSequence,
773 boost::asio::yield_context yield
776 auto const [keys, timeDiff] =
777 util::timed([
this, &ledgerSequence, yield]() -> std::vector<xrpl::uint256> {
778 auto const res = executor_.read(yield, schema_->selectDiff, ledgerSequence);
780 LOG(log_.error()) <<
"Could not fetch ledger diff: " << res.error()
781 <<
"; ledger = " << ledgerSequence;
785 auto const& results = res.value();
788 <<
"Could not fetch ledger diff - no rows; ledger = " << ledgerSequence;
792 std::vector<xrpl::uint256> resultKeys;
794 resultKeys.push_back(key);
803 LOG(log_.debug()) <<
"Fetched " << keys.size() <<
" diff hashes from database in "
804 << timeDiff <<
" milliseconds";
807 std::vector<LedgerObject> results;
808 results.reserve(keys.size());
814 std::back_inserter(results),
815 [](
auto const& key,
auto const& obj) {
return LedgerObject{key, obj}; }
821 std::optional<std::string>
823 std::string
const& migratorName,
824 boost::asio::yield_context yield
827 auto const res = executor_.read(yield, schema_->selectMigratorStatus,
Text(migratorName));
829 LOG(log_.error()) <<
"Could not fetch migrator status: " << res.error();
833 auto const& results = res.value();
844 std::expected<std::vector<std::pair<boost::uuids::uuid, std::string>>, std::string>
847 auto const readResult = executor_.read(yield, schema_->selectClioNodesData);
849 return std::unexpected{readResult.error().message()};
851 std::vector<std::pair<boost::uuids::uuid, std::string>> result;
854 result.emplace_back(uuid, std::move(message));
863 LOG(log_.trace()) <<
" Writing ledger object " << key.size() <<
":" << seq <<
" ["
864 << blob.size() <<
" bytes]";
867 executor_.write(schema_->insertDiff, seq, key);
869 executor_.write(schema_->insertObject, std::move(key), seq, std::move(blob));
873 writeSuccessor(std::string&& key, std::uint32_t
const seq, std::string&& successor)
override
875 LOG(log_.trace()) <<
"Writing successor. key = " << key.size() <<
" bytes. "
876 <<
" seq = " << std::to_string(seq) <<
" successor = " << successor.size()
878 ASSERT(!key.empty(),
"Key must not be empty");
879 ASSERT(!successor.empty(),
"Successor must not be empty");
881 executor_.write(schema_->insertSuccessor, std::move(key), seq, std::move(successor));
887 std::vector<Statement> statements;
888 statements.reserve(
data.size() * 10);
890 for (
auto& record :
data) {
891 std::ranges::transform(
892 record.accounts, std::back_inserter(statements), [
this, &record](
auto&& account) {
893 return schema_->insertAccountTx.bind(
894 std::forward<decltype(account)>(account),
895 std::make_tuple(record.ledgerSequence, record.transactionIndex),
902 executor_.write(std::move(statements));
908 std::vector<Statement> statements;
909 statements.reserve(record.accounts.size());
911 std::ranges::transform(
912 record.accounts, std::back_inserter(statements), [
this, &record](
auto&& account) {
913 return schema_->insertAccountTx.bind(
914 std::forward<decltype(account)>(account),
915 std::make_tuple(record.ledgerSequence, record.transactionIndex),
921 executor_.write(std::move(statements));
927 std::vector<Statement> statements;
928 statements.reserve(
data.size());
930 std::ranges::transform(
data, std::back_inserter(statements), [
this](
auto const& record) {
931 return schema_->insertNFTTx.bind(
933 std::make_tuple(record.ledgerSequence, record.transactionIndex),
938 executor_.write(std::move(statements));
943 std::vector<MPTokenIssuanceTransactionsData>
const&
data
946 std::vector<Statement> statements;
947 statements.reserve(
data.size());
949 std::ranges::transform(
data, std::back_inserter(statements), [
this](
auto const& record) {
950 return schema_->insertMPTokenIssuanceTx.bind(
951 record.mptIssuanceID,
952 std::make_tuple(record.ledgerSequence, record.transactionIndex),
957 executor_.write(std::move(statements));
962 std::vector<MPTokenIssuanceTransactionsData>
const&
data
965 std::size_t numStatements = 0u;
966 for (
auto const& record :
data)
967 numStatements += record.accounts.size();
969 std::vector<Statement> statements;
970 statements.reserve(numStatements);
972 for (
auto const& record :
data) {
973 std::ranges::transform(
975 std::back_inserter(statements),
976 [
this, &record](
auto const& account) {
977 return schema_->insertAccountMPTokenIssuanceTx.bind(
978 record.mptIssuanceID,
980 std::make_tuple(record.ledgerSequence, record.transactionIndex),
987 executor_.write(std::move(statements));
993 std::uint32_t
const seq,
994 std::uint32_t
const date,
995 std::string&& transaction,
996 std::string&& metadata
999 LOG(log_.trace()) <<
"Writing txn to database";
1001 executor_.write(schema_->insertLedgerTransaction, seq, hash);
1003 schema_->insertTransaction,
1007 std::move(transaction),
1015 std::vector<Statement> statements;
1016 statements.reserve(
data.size() * 3);
1019 if (!record.onlyUriChanged) {
1020 statements.push_back(schema_->insertNFT.bind(
1021 record.tokenID, record.ledgerSequence, record.owner, record.isBurned
1030 statements.push_back(schema_->insertIssuerNFT.bind(
1031 xrpl::nft::getIssuer(record.tokenID),
1032 static_cast<uint32_t
>(xrpl::nft::getTaxon(record.tokenID)),
1035 statements.push_back(schema_->insertNFTURI.bind(
1036 record.tokenID, record.ledgerSequence, *record.uri
1041 statements.push_back(
1043 schema_->insertNFTURI.bind(record.tokenID, record.ledgerSequence, *record.uri)
1048 executor_.writeEach(std::move(statements));
1054 std::vector<Statement> statements;
1055 statements.reserve(
data.size());
1056 for (
auto [mptId, holder] :
data)
1057 statements.push_back(schema_->insertMPTHolder.bind(mptId, holder));
1059 executor_.write(std::move(statements));
1072 executor_.writeSync(
1073 schema_->insertMigratorStatus,
1082 executor_.writeSync(
1090 return executor_.isTooBusy();
1096 return executor_.stats();
1109 auto const res = executor_.writeSync(statement);
1110 auto maybeSuccess = res->template get<bool>();
1111 if (not maybeSuccess) {
1112 LOG(log_.error()) <<
"executeSyncUpdate - error getting result - no row";
1116 if (not *maybeSuccess) {
1117 LOG(log_.warn()) <<
"Update failed. Checking if DB state is what we expect";
1124 return rng && rng->maxSequence == ledgerSequence_;
1148 Statement
const& statement,
1149 std::size_t
const cursorIdx,
1150 std::size_t
const limitIdx,
1151 std::uint32_t
const limit,
1153 std::optional<TransactionsCursor>
const& cursorIn,
1154 boost::asio::yield_context yield
1159 return {.txns = {}, .cursor = {}};
1161 auto cursor = cursorIn;
1162 if (cursor.has_value()) {
1163 statement.
bindAt(cursorIdx, cursor->asTuple());
1167 auto const ledgerSequence = forward ? rng->minSequence : rng->maxSequence;
1168 auto const transactionIndex = forward ? 0u : std::numeric_limits<std::uint32_t>::max();
1169 statement.
bindAt(cursorIdx, std::make_tuple(ledgerSequence, transactionIndex));
1174 auto const res = executor_.read(yield, statement);
1175 auto const& results = res.value();
1176 if (not results.hasRows()) {
1177 LOG(log_.debug()) <<
"No rows returned";
1181 std::vector<xrpl::uint256> hashes = {};
1182 auto numRows = results.numRows();
1184 for (
auto const& [hash,
data] :
1185 extract<xrpl::uint256, std::tuple<uint32_t, uint32_t>>(results)) {
1186 hashes.push_back(hash);
1188 if (--numRows == 0) {
1189 LOG(log_.debug()) <<
"Setting cursor";
1195 ++cursor->transactionIndex;
1200 LOG(log_.debug()) <<
"MPTokenIssuance Txns = " << txns.size();
1202 if (txns.size() == limit) {
1203 LOG(log_.debug()) <<
"Returning cursor";
1204 return {std::move(txns), cursor};
1207 return {std::move(txns), {}};
BackendInterface(LedgerCacheInterface &cache)
Construct a new backend interface instance.
Definition BackendInterface.hpp:139
std::optional< LedgerRange > hardFetchLedgerRangeNoThrow() const
Fetches the ledger range from DB retrying until no DatabaseTimeout 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:151
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
Mark the migration status of a migrator as Migrated in the database.
Definition CassandraBackendFamily.hpp:1070
std::optional< LedgerRange > hardFetchLedgerRange(boost::asio::yield_context yield) const override
Fetches the ledger range from DB.
Definition CassandraBackendFamily.hpp:300
std::optional< xrpl::LedgerHeader > fetchLedgerBySequence(std::uint32_t const sequence, boost::asio::yield_context yield) const override
Fetches a specific ledger by sequence number.
Definition CassandraBackendFamily.hpp:249
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:523
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:385
void startWrites() const override
Starts a write transaction with the DB. No-op for cassandra.
Definition CassandraBackendFamily.hpp:1063
void doWriteLedgerObject(std::string &&key, std::uint32_t const seq, std::string &&blob) override
Writes a ledger object to the database.
Definition CassandraBackendFamily.hpp:861
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:618
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:280
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:143
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:595
void writeNFTs(std::vector< NFTsData > const &data) override
Writes NFTs to the database.
Definition CassandraBackendFamily.hpp:1013
void writeNFTTransactions(std::vector< NFTTransactionsData > const &data) override
Write NFTs transactions.
Definition CassandraBackendFamily.hpp:925
void writeNodeMessage(boost::uuids::uuid const &uuid, std::string message) override
Write a node message. Used by ClusterCommunicationService.
Definition CassandraBackendFamily.hpp:1080
std::optional< std::uint32_t > fetchLatestLedgerSequence(boost::asio::yield_context yield) const override
Fetches the latest ledger sequence.
Definition CassandraBackendFamily.hpp:229
CassandraBackendFamily(SettingsProviderType settingsProvider, data::LedgerCacheInterface &cache, bool readOnly)
Create a new cassandra/scylla backend instance.
Definition CassandraBackendFamily.hpp:95
void writeAccountMPTokenIssuanceTransactions(std::vector< MPTokenIssuanceTransactionsData > const &data) override
Write MPTokenIssuance transaction index rows to the account_mptoken_issuance_transactions table.
Definition CassandraBackendFamily.hpp:961
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 all holders' balances for a MPTIssuanceID.
Definition CassandraBackendFamily.hpp:550
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:658
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:424
bool isTooBusy() const override
Definition CassandraBackendFamily.hpp:1088
void writeMPTHolders(std::vector< MPTHolderData > const &data) override
Write accounts that started holding onto a MPT.
Definition CassandraBackendFamily.hpp:1052
void writeAccountTransaction(AccountTransactionsData record) override
Write a new account transaction.
Definition CassandraBackendFamily.hpp:906
void writeSuccessor(std::string &&key, std::uint32_t const seq, std::string &&successor) override
Write a new successor.
Definition CassandraBackendFamily.hpp:873
void waitForWritesToFinish() override
Wait for all pending writes to finish.
Definition CassandraBackendFamily.hpp:213
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:497
void writeLedger(xrpl::LedgerHeader const &ledgerHeader, std::string &&blob) override
Writes to a specific ledger.
Definition CassandraBackendFamily.hpp:219
std::optional< TransactionAndMetadata > fetchTransaction(xrpl::uint256 const &hash, boost::asio::yield_context yield) const override
Fetches a specific transaction.
Definition CassandraBackendFamily.hpp:640
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:1147
boost::json::object stats() const override
Definition CassandraBackendFamily.hpp:1094
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:822
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:845
void writeAccountTransactions(std::vector< AccountTransactionsData > data) override
Write a new set of account transactions.
Definition CassandraBackendFamily.hpp:885
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:727
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:1107
void writeMPTokenIssuanceTransactions(std::vector< MPTokenIssuanceTransactionsData > const &data) override
Write MPTokenIssuance transaction index rows to the mptoken_issuance_transactions table.
Definition CassandraBackendFamily.hpp:942
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:991
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:348
std::vector< TransactionAndMetadata > fetchTransactions(std::vector< xrpl::uint256 > const &hashes, boost::asio::yield_context yield) const override
Fetches multiple transactions.
Definition CassandraBackendFamily.hpp:681
std::vector< LedgerObject > fetchLedgerDiff(std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Returns the difference between ledgers.
Definition CassandraBackendFamily.hpp:771
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:338
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:78
The requirements of an execution strategy.
Definition Concepts.hpp:35
The requirements of a settings provider.
Definition Concepts.hpp:24
This namespace implements a wrapper for the Cassandra C++ driver.
Definition CassandraBackendFamily.hpp:47
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:314
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:56
xrpl::LedgerHeader deserializeHeader(xrpl::Slice data)
Deserializes a xrpl::LedgerHeader from xrpl::Slice of data.
Definition LedgerUtils.hpp:240
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
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:22
Stores a range of sequences as a min and max pair.
Definition Types.hpp:243
Represents an array of MPTokens.
Definition Types.hpp:235
Represests a bundle of transactions with metadata and a cursor to the next page.
Definition Types.hpp:153
A strong type wrapper for int32_t.
Definition Types.hpp:38
A strong type wrapper for string.
Definition Types.hpp:49