22#include "data/BackendInterface.hpp"
24#include "data/LedgerCacheInterface.hpp"
25#include "data/LedgerHeaderCache.hpp"
26#include "data/Types.hpp"
27#include "data/cassandra/Concepts.hpp"
28#include "data/cassandra/Handle.hpp"
29#include "data/cassandra/Schema.hpp"
30#include "data/cassandra/SettingsProvider.hpp"
31#include "data/cassandra/Types.hpp"
32#include "data/cassandra/impl/ExecutionStrategy.hpp"
33#include "util/Assert.hpp"
34#include "util/LedgerUtils.hpp"
35#include "util/Profiler.hpp"
36#include "util/log/Logger.hpp"
38#include <boost/asio/spawn.hpp>
39#include <boost/json/object.hpp>
40#include <boost/uuid/string_generator.hpp>
41#include <boost/uuid/uuid.hpp>
44#include <xrpl/basics/Blob.h>
45#include <xrpl/basics/base_uint.h>
46#include <xrpl/basics/strHex.h>
47#include <xrpl/protocol/AccountID.h>
48#include <xrpl/protocol/Indexes.h>
49#include <xrpl/protocol/LedgerHeader.h>
50#include <xrpl/protocol/nft.h>
66class CacheBackendCassandraTest;
80 SomeSettingsProvider SettingsProviderType,
81 SomeExecutionStrategy ExecutionStrategyType,
82 typename FetchLedgerCacheType = FetchLedgerCache>
86 SettingsProviderType settingsProvider_;
88 std::atomic_uint32_t ledgerSequence_ = 0u;
89 friend class ::CacheBackendCassandraTest;
95 mutable ExecutionStrategyType executor_;
97 mutable FetchLedgerCacheType ledgerCache_{};
109 , settingsProvider_{std::move(settingsProvider)}
110 , schema_{settingsProvider_}
111 , handle_{settingsProvider_.getSettings()}
112 , executor_{settingsProvider_.getSettings(), handle_}
114 if (
auto const res = handle_.
connect(); not res)
115 throw std::runtime_error(
"Could not connect to database: " + res.error());
118 if (
auto const res = handle_.
execute(schema_.createKeyspace); not res) {
121 if (res.error().code() != CASS_ERROR_SERVER_UNAUTHORIZED)
122 throw std::runtime_error(
"Could not create keyspace: " + res.error());
125 if (
auto const res = handle_.
executeEach(schema_.createSchema); not res)
126 throw std::runtime_error(
"Could not create schema: " + res.error());
131 }
catch (std::runtime_error
const& ex) {
132 auto const error = fmt::format(
133 "Failed to prepare the statements: {}; readOnly: {}. ReadOnly should be turned off or another Clio "
134 "node with write access to DB should be started first.",
138 LOG(log_.
error()) << error;
139 throw std::runtime_error(error);
141 LOG(log_.
info()) <<
"Created (revamped) CassandraBackend";
151 ripple::AccountID
const& account,
152 std::uint32_t
const limit,
154 std::optional<TransactionsCursor>
const& cursorIn,
155 boost::asio::yield_context yield
160 return {.txns = {}, .cursor = {}};
162 Statement const statement = [
this, forward, &account]() {
164 return schema_->selectAccountTxForward.bind(account);
166 return schema_->selectAccountTx.bind(account);
169 auto cursor = cursorIn;
171 statement.
bindAt(1, cursor->asTuple());
172 LOG(log_.
debug()) <<
"account = " << ripple::strHex(account) <<
" tuple = " << cursor->ledgerSequence
173 << cursor->transactionIndex;
175 auto const seq = forward ? rng->minSequence : rng->maxSequence;
176 auto const placeHolder = forward ? 0u : std::numeric_limits<std::uint32_t>::max();
178 statement.
bindAt(1, std::make_tuple(placeHolder, placeHolder));
179 LOG(log_.
debug()) <<
"account = " << ripple::strHex(account) <<
" idx = " << seq
180 <<
" tuple = " << placeHolder;
187 auto const res = executor_.read(yield, statement);
188 auto const& results = res.value();
189 if (not results.hasRows()) {
190 LOG(log_.
debug()) <<
"No rows returned";
194 std::vector<ripple::uint256> hashes = {};
195 auto numRows = results.numRows();
196 LOG(log_.
info()) <<
"num_rows = " << numRows;
198 for (
auto [hash,
data] :
extract<ripple::uint256, std::tuple<uint32_t, uint32_t>>(results)) {
199 hashes.push_back(hash);
200 if (--numRows == 0) {
201 LOG(log_.
debug()) <<
"Setting cursor";
207 LOG(log_.
debug()) <<
"Txns = " << txns.size();
209 if (txns.size() == limit) {
210 LOG(log_.
debug()) <<
"Returning cursor";
211 return {txns, cursor};
229 executor_.writeSync(schema_->updateLedgerRange, ledgerSequence_,
false, ledgerSequence_);
232 if (not executeSyncUpdate(schema_->updateLedgerRange.bind(ledgerSequence_,
true, ledgerSequence_ - 1))) {
233 LOG(log_.
warn()) <<
"Update failed for ledger " << ledgerSequence_;
237 LOG(log_.
info()) <<
"Committed ledger " << ledgerSequence_;
242 writeLedger(ripple::LedgerHeader
const& ledgerHeader, std::string&& blob)
override
244 executor_.write(schema_->insertLedgerHeader, ledgerHeader.seq, std::move(blob));
246 executor_.write(schema_->insertLedgerHash, ledgerHeader.hash, ledgerHeader.seq);
248 ledgerSequence_ = ledgerHeader.seq;
251 std::optional<std::uint32_t>
254 if (
auto const res = executor_.read(yield, schema_->selectLatestLedger); res) {
255 if (
auto const& result = res.value(); result) {
256 if (
auto const maybeValue = result.template get<uint32_t>(); maybeValue)
259 LOG(log_.
error()) <<
"Could not fetch latest ledger - no rows";
263 LOG(log_.
error()) <<
"Could not fetch latest ledger - no result";
265 LOG(log_.
error()) <<
"Could not fetch latest ledger: " << res.error();
271 std::optional<ripple::LedgerHeader>
274 if (
auto const lock = ledgerCache_.get(); lock.has_value() && lock->seq == sequence)
277 auto const res = executor_.read(yield, schema_->selectLedgerBySeq, sequence);
279 if (
auto const& result = res.value(); result) {
280 if (
auto const maybeValue = result.template get<std::vector<unsigned char>>(); maybeValue) {
286 LOG(log_.
error()) <<
"Could not fetch ledger by sequence - no rows";
290 LOG(log_.
error()) <<
"Could not fetch ledger by sequence - no result";
292 LOG(log_.
error()) <<
"Could not fetch ledger by sequence: " << res.error();
298 std::optional<ripple::LedgerHeader>
301 if (
auto const res = executor_.read(yield, schema_->selectLedgerByHash, hash); res) {
302 if (
auto const& result = res.value(); result) {
303 if (
auto const maybeValue = result.template get<uint32_t>(); maybeValue)
306 LOG(log_.
error()) <<
"Could not fetch ledger by hash - no rows";
310 LOG(log_.
error()) <<
"Could not fetch ledger by hash - no result";
312 LOG(log_.
error()) <<
"Could not fetch ledger by hash: " << res.error();
318 std::optional<LedgerRange>
321 auto const res = executor_.read(yield, schema_->selectLedgerRange);
323 auto const& results = res.value();
324 if (not results.hasRows()) {
325 LOG(log_.
debug()) <<
"Could not fetch ledger range - no rows";
336 range.maxSequence = range.minSequence = seq;
337 }
else if (idx == 1) {
338 range.maxSequence = seq;
344 if (range.minSequence > range.maxSequence)
345 std::swap(range.minSequence, range.maxSequence);
347 LOG(log_.
debug()) <<
"After hardFetchLedgerRange range is " << range.minSequence <<
":"
348 << range.maxSequence;
351 LOG(log_.
error()) <<
"Could not fetch ledger range: " << res.error();
356 std::vector<TransactionAndMetadata>
363 std::vector<ripple::uint256>
367 auto start = std::chrono::system_clock::now();
368 auto const res = executor_.read(yield, schema_->selectAllTransactionHashesInLedger, ledgerSequence);
371 LOG(log_.
error()) <<
"Could not fetch all transaction hashes: " << res.error();
375 auto const& result = res.value();
376 if (not result.hasRows()) {
377 LOG(log_.
warn()) <<
"Could not fetch all transaction hashes - no rows; ledger = "
378 << std::to_string(ledgerSequence);
382 std::vector<ripple::uint256> hashes;
384 hashes.push_back(std::move(hash));
386 auto end = std::chrono::system_clock::now();
387 LOG(log_.
debug()) <<
"Fetched " << hashes.size() <<
" transaction hashes from database in "
388 << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
395 fetchNFT(ripple::uint256
const& tokenID, std::uint32_t
const ledgerSequence, boost::asio::yield_context yield)
398 auto const res = executor_.read(yield, schema_->selectNFT, tokenID, ledgerSequence);
402 if (
auto const maybeRow = res->template get<uint32_t, ripple::AccountID, bool>(); maybeRow) {
403 auto [seq, owner, isBurned] = *maybeRow;
404 auto result = std::make_optional<NFT>(tokenID, seq, owner, isBurned);
417 auto uriRes = executor_.read(yield, schema_->selectNFTURI, tokenID, ledgerSequence);
419 if (
auto const maybeUri = uriRes->template get<ripple::Blob>(); maybeUri)
420 result->uri = *maybeUri;
426 LOG(log_.
error()) <<
"Could not fetch NFT - no rows";
432 ripple::uint256
const& tokenID,
433 std::uint32_t
const limit,
435 std::optional<TransactionsCursor>
const& cursorIn,
436 boost::asio::yield_context yield
441 return {.txns = {}, .cursor = {}};
443 Statement const statement = [
this, forward, &tokenID]() {
445 return schema_->selectNFTTxForward.bind(tokenID);
447 return schema_->selectNFTTx.bind(tokenID);
450 auto cursor = cursorIn;
452 statement.
bindAt(1, cursor->asTuple());
453 LOG(log_.
debug()) <<
"token_id = " << ripple::strHex(tokenID) <<
" tuple = " << cursor->ledgerSequence
454 << cursor->transactionIndex;
456 auto const seq = forward ? rng->minSequence : rng->maxSequence;
457 auto const placeHolder = forward ? 0 : std::numeric_limits<std::uint32_t>::max();
459 statement.
bindAt(1, std::make_tuple(placeHolder, placeHolder));
460 LOG(log_.
debug()) <<
"token_id = " << ripple::strHex(tokenID) <<
" idx = " << seq
461 <<
" tuple = " << placeHolder;
466 auto const res = executor_.read(yield, statement);
467 auto const& results = res.value();
468 if (not results.hasRows()) {
469 LOG(log_.
debug()) <<
"No rows returned";
473 std::vector<ripple::uint256> hashes = {};
474 auto numRows = results.numRows();
475 LOG(log_.
info()) <<
"num_rows = " << numRows;
477 for (
auto [hash,
data] :
extract<ripple::uint256, std::tuple<uint32_t, uint32_t>>(results)) {
478 hashes.push_back(hash);
479 if (--numRows == 0) {
480 LOG(log_.
debug()) <<
"Setting cursor";
486 ++cursor->transactionIndex;
491 LOG(log_.
debug()) <<
"NFT Txns = " << txns.size();
493 if (txns.size() == limit) {
494 LOG(log_.
debug()) <<
"Returning cursor";
495 return {txns, cursor};
503 ripple::AccountID
const& issuer,
504 std::optional<std::uint32_t>
const& taxon,
505 std::uint32_t
const ledgerSequence,
506 std::uint32_t
const limit,
507 std::optional<ripple::uint256>
const& cursorIn,
508 boost::asio::yield_context yield
513 Statement const idQueryStatement = [&taxon, &issuer, &cursorIn, &limit,
this]() {
514 if (taxon.has_value()) {
515 auto r = schema_->selectNFTIDsByIssuerTaxon.bind(issuer);
517 r.bindAt(2, cursorIn.value_or(ripple::uint256(0)));
518 r.bindAt(3,
Limit{limit});
522 auto r = schema_->selectNFTIDsByIssuer.bind(issuer);
526 cursorIn.has_value() ? ripple::nft::toUInt32(ripple::nft::getTaxon(*cursorIn)) : 0,
527 cursorIn.value_or(ripple::uint256(0))
530 r.bindAt(2,
Limit{limit});
535 auto const res = executor_.read(yield, idQueryStatement);
537 auto const& idQueryResults = res.value();
538 if (not idQueryResults.hasRows()) {
539 LOG(log_.
debug()) <<
"No rows returned";
543 std::vector<ripple::uint256> nftIDs;
545 nftIDs.push_back(nftID);
550 if (nftIDs.size() == limit)
551 ret.cursor = nftIDs.back();
553 std::vector<Statement> selectNFTStatements;
554 selectNFTStatements.reserve(nftIDs.size());
559 std::back_inserter(selectNFTStatements),
560 [&](
auto const& nftID) {
return schema_->selectNFT.bind(nftID, ledgerSequence); }
563 auto const nftInfos = executor_.readEach(yield, selectNFTStatements);
565 std::vector<Statement> selectNFTURIStatements;
566 selectNFTURIStatements.reserve(nftIDs.size());
571 std::back_inserter(selectNFTURIStatements),
572 [&](
auto const& nftID) {
return schema_->selectNFTURI.bind(nftID, ledgerSequence); }
575 auto const nftUris = executor_.readEach(yield, selectNFTURIStatements);
577 for (
auto i = 0u; i < nftIDs.size(); i++) {
578 if (
auto const maybeRow = nftInfos[i].
template get<uint32_t, ripple::AccountID, bool>(); maybeRow) {
579 auto [seq, owner, isBurned] = *maybeRow;
580 NFT nft(nftIDs[i], seq, owner, isBurned);
581 if (
auto const maybeUri = nftUris[i].
template get<ripple::Blob>(); maybeUri)
583 ret.nfts.push_back(nft);
591 ripple::uint192
const& mptID,
592 std::uint32_t
const limit,
593 std::optional<ripple::AccountID>
const& cursorIn,
594 std::uint32_t
const ledgerSequence,
595 boost::asio::yield_context yield
598 auto const holderEntries = executor_.read(
599 yield, schema_->selectMPTHolders, mptID, cursorIn.value_or(ripple::AccountID(0)),
Limit{limit}
602 auto const& holderResults = holderEntries.value();
603 if (not holderResults.hasRows()) {
604 LOG(log_.
debug()) <<
"No rows returned";
608 std::vector<ripple::uint256> mptKeys;
609 std::optional<ripple::AccountID> cursor;
611 mptKeys.push_back(ripple::keylet::mptoken(mptID, holder).key);
617 auto it = std::remove_if(mptObjects.begin(), mptObjects.end(), [](Blob
const& mpt) { return mpt.empty(); });
619 mptObjects.erase(it, mptObjects.end());
621 ASSERT(mptKeys.size() <= limit,
"Number of keys can't exceed the limit");
622 if (mptKeys.size() == limit)
623 return {mptObjects, cursor};
625 return {mptObjects, {}};
629 doFetchLedgerObject(ripple::uint256
const& key, std::uint32_t
const sequence, boost::asio::yield_context yield)
632 LOG(log_.
debug()) <<
"Fetching ledger object for seq " << sequence <<
", key = " << ripple::to_string(key);
633 if (
auto const res = executor_.read(yield, schema_->selectObject, key, sequence); res) {
634 if (
auto const result = res->template get<Blob>(); result) {
638 LOG(log_.
debug()) <<
"Could not fetch ledger object - no rows";
641 LOG(log_.
error()) <<
"Could not fetch ledger object: " << res.error();
647 std::optional<std::uint32_t>
651 LOG(log_.
debug()) <<
"Fetching ledger object for seq " << sequence <<
", key = " << ripple::to_string(key);
652 if (
auto const res = executor_.read(yield, schema_->selectObject, key, sequence); res) {
653 if (
auto const result = res->template get<Blob, std::uint32_t>(); result) {
654 auto [_, seq] = result.value();
657 LOG(log_.
debug()) <<
"Could not fetch ledger object sequence - no rows";
659 LOG(log_.
error()) <<
"Could not fetch ledger object sequence: " << res.error();
665 std::optional<TransactionAndMetadata>
666 fetchTransaction(ripple::uint256
const& hash, boost::asio::yield_context yield)
const override
668 if (
auto const res = executor_.read(yield, schema_->selectTransaction, hash); res) {
669 if (
auto const maybeValue = res->template get<Blob, Blob, uint32_t, uint32_t>(); maybeValue) {
670 auto [transaction, meta, seq, date] = *maybeValue;
671 return std::make_optional<TransactionAndMetadata>(transaction, meta, seq, date);
674 LOG(log_.
debug()) <<
"Could not fetch transaction - no rows";
676 LOG(log_.
error()) <<
"Could not fetch transaction: " << res.error();
682 std::optional<ripple::uint256>
683 doFetchSuccessorKey(ripple::uint256 key, std::uint32_t
const ledgerSequence, boost::asio::yield_context yield)
686 if (
auto const res = executor_.read(yield, schema_->selectSuccessor, key, ledgerSequence); res) {
687 if (
auto const result = res->template get<ripple::uint256>(); result) {
688 if (*result == kLAST_KEY)
693 LOG(log_.
debug()) <<
"Could not fetch successor - no rows";
695 LOG(log_.
error()) <<
"Could not fetch successor: " << res.error();
701 std::vector<TransactionAndMetadata>
702 fetchTransactions(std::vector<ripple::uint256>
const& hashes, boost::asio::yield_context yield)
const override
707 auto const numHashes = hashes.size();
708 std::vector<TransactionAndMetadata> results;
709 results.reserve(numHashes);
711 std::vector<Statement> statements;
712 statements.reserve(numHashes);
714 auto const timeDiff =
util::timed([
this, yield, &results, &hashes, &statements]() {
719 std::back_inserter(statements),
720 [
this](
auto const& hash) {
return schema_->selectTransaction.bind(hash); }
723 auto const entries = executor_.readEach(yield, statements);
725 std::cbegin(entries),
727 std::back_inserter(results),
729 if (
auto const maybeRow = res.template get<Blob, Blob, uint32_t, uint32_t>(); maybeRow)
737 ASSERT(numHashes == results.size(),
"Number of hashes and results must match");
738 LOG(log_.
debug()) <<
"Fetched " << numHashes <<
" transactions from database in " << timeDiff
745 std::vector<ripple::uint256>
const& keys,
746 std::uint32_t
const sequence,
747 boost::asio::yield_context yield
753 auto const numKeys = keys.size();
754 LOG(log_.
trace()) <<
"Fetching " << numKeys <<
" objects";
756 std::vector<Blob> results;
757 results.reserve(numKeys);
759 std::vector<Statement> statements;
760 statements.reserve(numKeys);
766 std::back_inserter(statements),
767 [
this, &sequence](
auto const& key) {
return schema_->selectObject.bind(key, sequence); }
770 auto const entries = executor_.readEach(yield, statements);
772 std::cbegin(entries),
774 std::back_inserter(results),
775 [](
auto const& res) -> Blob {
776 if (
auto const maybeValue = res.template get<Blob>(); maybeValue)
783 LOG(log_.
trace()) <<
"Fetched " << numKeys <<
" objects";
787 std::vector<ripple::uint256>
788 fetchAccountRoots(std::uint32_t number, std::uint32_t pageSize, std::uint32_t seq, boost::asio::yield_context yield)
791 std::vector<ripple::uint256> liveAccounts;
792 std::optional<ripple::AccountID> lastItem;
794 while (liveAccounts.size() < number) {
795 Statement const statement = lastItem ? schema_->selectAccountFromToken.bind(*lastItem,
Limit{pageSize})
796 : schema_->selectAccountFromBeginning.bind(
Limit{pageSize});
798 auto const res = executor_.read(yield, statement);
800 auto const& results = res.value();
801 if (not results.hasRows()) {
802 LOG(log_.
debug()) <<
"No rows returned";
806 std::vector<ripple::uint256> fullAccounts;
808 fullAccounts.push_back(ripple::keylet::account(account).key);
813 for (
auto i = 0u; i < fullAccounts.size(); i++) {
814 if (not objs[i].empty()) {
815 if (liveAccounts.size() < number) {
816 liveAccounts.push_back(fullAccounts[i]);
823 LOG(log_.
error()) <<
"Could not fetch account from account_tx: " << res.error();
831 std::vector<LedgerObject>
832 fetchLedgerDiff(std::uint32_t
const ledgerSequence, boost::asio::yield_context yield)
const override
834 auto const [keys, timeDiff] =
util::timed([
this, &ledgerSequence, yield]() -> std::vector<ripple::uint256> {
835 auto const res = executor_.read(yield, schema_->selectDiff, ledgerSequence);
837 LOG(log_.
error()) <<
"Could not fetch ledger diff: " << res.error() <<
"; ledger = " << ledgerSequence;
841 auto const& results = res.value();
843 LOG(log_.
error()) <<
"Could not fetch ledger diff - no rows; ledger = " << ledgerSequence;
847 std::vector<ripple::uint256> resultKeys;
849 resultKeys.push_back(key);
858 LOG(log_.
debug()) <<
"Fetched " << keys.size() <<
" diff hashes from database in " << timeDiff
862 std::vector<LedgerObject> results;
863 results.reserve(keys.size());
869 std::back_inserter(results),
870 [](
auto const& key,
auto const& obj) {
return LedgerObject{key, obj}; }
876 std::optional<std::string>
879 auto const res = executor_.read(yield, schema_->selectMigratorStatus,
Text(migratorName));
881 LOG(log_.
error()) <<
"Could not fetch migrator status: " << res.error();
885 auto const& results = res.value();
896 std::expected<std::vector<std::pair<boost::uuids::uuid, std::string>>, std::string>
899 auto const readResult = executor_.read(yield, schema_->selectClioNodesData);
901 return std::unexpected{readResult.error().message()};
903 std::vector<std::pair<boost::uuids::uuid, std::string>> result;
906 result.emplace_back(uuid, std::move(message));
915 LOG(log_.
trace()) <<
" Writing ledger object " << key.size() <<
":" << seq <<
" [" << blob.size() <<
" bytes]";
918 executor_.write(schema_->insertDiff, seq, key);
920 executor_.write(schema_->insertObject, std::move(key), seq, std::move(blob));
924 writeSuccessor(std::string&& key, std::uint32_t
const seq, std::string&& successor)
override
926 LOG(log_.
trace()) <<
"Writing successor. key = " << key.size() <<
" bytes. "
927 <<
" seq = " << std::to_string(seq) <<
" successor = " << successor.size() <<
" bytes.";
928 ASSERT(!key.empty(),
"Key must not be empty");
929 ASSERT(!successor.empty(),
"Successor must not be empty");
931 executor_.write(schema_->insertSuccessor, std::move(key), seq, std::move(successor));
937 std::vector<Statement> statements;
938 statements.reserve(
data.size() * 10);
940 for (
auto& record :
data) {
941 std::ranges::transform(record.accounts, std::back_inserter(statements), [
this, &record](
auto&& account) {
942 return schema_->insertAccountTx.bind(
943 std::forward<decltype(account)>(account),
944 std::make_tuple(record.ledgerSequence, record.transactionIndex),
950 executor_.write(std::move(statements));
956 std::vector<Statement> statements;
957 statements.reserve(record.accounts.size());
959 std::ranges::transform(record.accounts, std::back_inserter(statements), [
this, &record](
auto&& account) {
960 return schema_->insertAccountTx.bind(
961 std::forward<decltype(account)>(account),
962 std::make_tuple(record.ledgerSequence, record.transactionIndex),
967 executor_.write(std::move(statements));
973 std::vector<Statement> statements;
974 statements.reserve(
data.size());
976 std::ranges::transform(
data, std::back_inserter(statements), [
this](
auto const& record) {
977 return schema_->insertNFTTx.bind(
978 record.tokenID, std::make_tuple(record.ledgerSequence, record.transactionIndex), record.txHash
982 executor_.write(std::move(statements));
988 std::uint32_t
const seq,
989 std::uint32_t
const date,
990 std::string&& transaction,
991 std::string&& metadata
994 LOG(log_.
trace()) <<
"Writing txn to database";
996 executor_.write(schema_->insertLedgerTransaction, seq, hash);
998 schema_->insertTransaction, std::move(hash), seq, date, std::move(transaction), std::move(metadata)
1005 std::vector<Statement> statements;
1006 statements.reserve(
data.size() * 3);
1009 if (!record.onlyUriChanged) {
1010 statements.push_back(
1011 schema_->insertNFT.bind(record.tokenID, record.ledgerSequence, record.owner, record.isBurned)
1020 statements.push_back(schema_->insertIssuerNFT.bind(
1021 ripple::nft::getIssuer(record.tokenID),
1022 static_cast<uint32_t
>(ripple::nft::getTaxon(record.tokenID)),
1025 statements.push_back(
1026 schema_->insertNFTURI.bind(record.tokenID, record.ledgerSequence, record.uri.value())
1031 statements.push_back(
1032 schema_->insertNFTURI.bind(record.tokenID, record.ledgerSequence, record.uri.value())
1037 executor_.writeEach(std::move(statements));
1043 std::vector<Statement> statements;
1044 statements.reserve(
data.size());
1045 for (
auto [mptId, holder] :
data)
1046 statements.push_back(schema_->insertMPTHolder.bind(mptId, holder));
1048 executor_.write(std::move(statements));
1061 executor_.writeSync(
1069 executor_.writeSync(schema_->updateClioNodeMessage,
data::cassandra::Text{std::move(message)}, uuid);
1075 return executor_.isTooBusy();
1081 return executor_.stats();
1088 auto const res = executor_.writeSync(statement);
1089 auto maybeSuccess = res->template get<bool>();
1090 if (not maybeSuccess) {
1091 LOG(log_.
error()) <<
"executeSyncUpdate - error getting result - no row";
1095 if (not maybeSuccess.value()) {
1096 LOG(log_.
warn()) <<
"Update failed. Checking if DB state is what we expect";
1103 return rng && rng->maxSequence == ledgerSequence_;
1110using CassandraBackend = BasicCassandraBackend<SettingsProvider, impl::DefaultExecutionStrategy<>>;
The interface to the database used by Clio.
Definition BackendInterface.hpp:140
std::vector< Blob > fetchLedgerObjects(std::vector< ripple::uint256 > const &keys, std::uint32_t sequence, boost::asio::yield_context yield) const
Fetches all ledger objects by their keys.
Definition BackendInterface.cpp:119
std::optional< LedgerRange > hardFetchLedgerRangeNoThrow() const
Fetches the ledger range from DB retrying until no DatabaseTimeout is thrown.
Definition BackendInterface.cpp:77
std::optional< LedgerRange > fetchLedgerRange() const
Fetch the current ledger range.
Definition BackendInterface.cpp:267
LedgerCacheInterface const & cache() const
Definition BackendInterface.hpp:165
Cache for an entire ledger.
Definition LedgerCacheInterface.hpp:38
Implements BackendInterface for Cassandra/ScyllaDB.
Definition CassandraBackend.hpp:83
bool isTooBusy() const override
Definition CassandraBackend.hpp:1073
std::optional< std::uint32_t > fetchLatestLedgerSequence(boost::asio::yield_context yield) const override
Fetches the latest ledger sequence.
Definition CassandraBackend.hpp:252
MPTHoldersAndCursor fetchMPTHolders(ripple::uint192 const &mptID, std::uint32_t const limit, std::optional< ripple::AccountID > const &cursorIn, std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Fetches all holders' balances for a MPTIssuanceID.
Definition CassandraBackend.hpp:590
std::vector< ripple::uint256 > fetchAllTransactionHashesInLedger(std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Fetches all transaction hashes from a specific ledger.
Definition CassandraBackend.hpp:364
void writeMPTHolders(std::vector< MPTHolderData > const &data) override
Write accounts that started holding onto a MPT.
Definition CassandraBackend.hpp:1041
std::optional< Blob > doFetchLedgerObject(ripple::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 CassandraBackend.hpp:629
std::optional< LedgerRange > hardFetchLedgerRange(boost::asio::yield_context yield) const override
Fetches the ledger range from DB.
Definition CassandraBackend.hpp:319
void writeSuccessor(std::string &&key, std::uint32_t const seq, std::string &&successor) override
Write a new successor.
Definition CassandraBackend.hpp:924
void waitForWritesToFinish() override
Wait for all pending writes to finish.
Definition CassandraBackend.hpp:218
std::optional< ripple::LedgerHeader > fetchLedgerByHash(ripple::uint256 const &hash, boost::asio::yield_context yield) const override
Fetches a specific ledger by hash.
Definition CassandraBackend.hpp:299
std::vector< TransactionAndMetadata > fetchAllTransactionsInLedger(std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Fetches all transactions from a specific ledger.
Definition CassandraBackend.hpp:357
std::optional< ripple::LedgerHeader > fetchLedgerBySequence(std::uint32_t const sequence, boost::asio::yield_context yield) const override
Fetches a specific ledger by sequence number.
Definition CassandraBackend.hpp:272
std::vector< Blob > doFetchLedgerObjects(std::vector< ripple::uint256 > const &keys, std::uint32_t const sequence, boost::asio::yield_context yield) const override
The database-specific implementation for fetching ledger objects.
Definition CassandraBackend.hpp:744
TransactionsAndCursor fetchAccountTransactions(ripple::AccountID const &account, std::uint32_t const limit, bool forward, std::optional< TransactionsCursor > const &cursorIn, boost::asio::yield_context yield) const override
Fetches all transactions for a specific account.
Definition CassandraBackend.hpp:150
void writeAccountTransaction(AccountTransactionsData record) override
Write a new account transaction.
Definition CassandraBackend.hpp:954
void writeAccountTransactions(std::vector< AccountTransactionsData > data) override
Write a new set of account transactions.
Definition CassandraBackend.hpp:935
BasicCassandraBackend(SettingsProviderType settingsProvider, data::LedgerCacheInterface &cache, bool readOnly)
Create a new cassandra/scylla backend instance.
Definition CassandraBackend.hpp:107
std::optional< NFT > fetchNFT(ripple::uint256 const &tokenID, std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Fetches a specific NFT.
Definition CassandraBackend.hpp:395
std::vector< ripple::uint256 > fetchAccountRoots(std::uint32_t number, std::uint32_t pageSize, std::uint32_t seq, boost::asio::yield_context yield) const override
Fetch the specified number of account root object indexes by page, the accounts need to exist for seq...
Definition CassandraBackend.hpp:788
void writeNFTs(std::vector< NFTsData > const &data) override
Writes NFTs to the database.
Definition CassandraBackend.hpp:1003
std::vector< LedgerObject > fetchLedgerDiff(std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Returns the difference between ledgers.
Definition CassandraBackend.hpp:832
void writeLedger(ripple::LedgerHeader const &ledgerHeader, std::string &&blob) override
Writes to a specific ledger.
Definition CassandraBackend.hpp:242
std::optional< std::uint32_t > doFetchLedgerObjectSeq(ripple::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 CassandraBackend.hpp:648
void writeNodeMessage(boost::uuids::uuid const &uuid, std::string message) override
Write a node message. Used by ClusterCommunicationService.
Definition CassandraBackend.hpp:1067
std::optional< TransactionAndMetadata > fetchTransaction(ripple::uint256 const &hash, boost::asio::yield_context yield) const override
Fetches a specific transaction.
Definition CassandraBackend.hpp:666
std::vector< TransactionAndMetadata > fetchTransactions(std::vector< ripple::uint256 > const &hashes, boost::asio::yield_context yield) const override
Fetches multiple transactions.
Definition CassandraBackend.hpp:702
std::optional< std::string > fetchMigratorStatus(std::string const &migratorName, boost::asio::yield_context yield) const override
Fetches the status of migrator by name.
Definition CassandraBackend.hpp:877
void startWrites() const override
Starts a write transaction with the DB. No-op for cassandra.
Definition CassandraBackend.hpp:1052
void doWriteLedgerObject(std::string &&key, std::uint32_t const seq, std::string &&blob) override
Writes a ledger object to the database.
Definition CassandraBackend.hpp:913
TransactionsAndCursor fetchNFTTransactions(ripple::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 CassandraBackend.hpp:431
boost::json::object stats() const override
Definition CassandraBackend.hpp:1079
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 CassandraBackend.hpp:897
NFTsAndCursor fetchNFTsByIssuer(ripple::AccountID const &issuer, std::optional< std::uint32_t > const &taxon, std::uint32_t const ledgerSequence, std::uint32_t const limit, std::optional< ripple::uint256 > const &cursorIn, boost::asio::yield_context yield) const override
Fetches all NFTs issued by a given address.
Definition CassandraBackend.hpp:502
void writeNFTTransactions(std::vector< NFTTransactionsData > const &data) override
Write NFTs transactions.
Definition CassandraBackend.hpp:971
bool doFinishWrites() override
The implementation should wait for all pending writes to finish.
Definition CassandraBackend.hpp:224
std::optional< ripple::uint256 > doFetchSuccessorKey(ripple::uint256 key, std::uint32_t const ledgerSequence, boost::asio::yield_context yield) const override
Database-specific implementation of fetching the successor key.
Definition CassandraBackend.hpp:683
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 CassandraBackend.hpp:986
void writeMigratorStatus(std::string const &migratorName, std::string const &status) override
Mark the migration status of a migrator as Migrated in the database.
Definition CassandraBackend.hpp:1059
Represents a handle to the cassandra database cluster.
Definition Handle.hpp:46
MaybeErrorType connect() const
Synchronous version of the above.
Definition Handle.cpp:55
MaybeErrorType executeEach(std::vector< StatementType > const &statements) const
Synchronous version of the above.
Definition Handle.cpp:109
ResultOrErrorType execute(std::string_view query, Args &&... args) const
Synchronous version of the above.
Definition Handle.hpp:185
Manages the DB schema and provides access to prepared statements.
Definition Schema.hpp:55
void prepareStatements(Handle const &handle)
Recreates the prepared statements.
Definition Schema.hpp:848
Definition Statement.hpp:47
void bindAt(std::size_t const idx, Type &&value) const
Binds an argument to a specific index.
Definition Statement.hpp:93
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:224
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:229
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:214
Pump trace(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::TRC severity.
Definition Logger.cpp:209
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:219
This namespace implements a wrapper for the Cassandra C++ driver.
Definition Concepts.hpp:37
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:329
This namespace implements the data access layer and related components.
Definition AmendmentCenter.cpp:70
ripple::LedgerHeader deserializeHeader(ripple::Slice data)
Deserializes a ripple::LedgerHeader from ripple::Slice of data.
Definition LedgerUtils.hpp:204
auto timed(FnType &&func)
Profiler function to measure the time a function execution consumes.
Definition Profiler.hpp:40
Struct used to keep track of what to write to account_transactions/account_tx tables.
Definition DBHelpers.hpp:45
Represents an NFT state at a particular ledger.
Definition DBHelpers.hpp:103
Struct to store ledger header cache entry and the sequence it belongs to.
Definition LedgerHeaderCache.hpp:48
Represents an object in the ledger.
Definition Types.hpp:41
Stores a range of sequences as a min and max pair.
Definition Types.hpp:247
Represents an array of MPTokens.
Definition Types.hpp:239
Represents a NFToken.
Definition Types.hpp:172
Represents a bundle of NFTs with a cursor to the next page.
Definition Types.hpp:231
Represests a bundle of transactions with metadata and a cursor to the next page.
Definition Types.hpp:164
A strong type wrapper for int32_t.
Definition Types.hpp:56
A strong type wrapper for string.
Definition Types.hpp:67