20 #include <ripple/app/ledger/AcceptedLedger.h>
21 #include <ripple/app/ledger/LedgerMaster.h>
22 #include <ripple/app/ledger/LedgerToJson.h>
23 #include <ripple/app/ledger/TransactionMaster.h>
24 #include <ripple/app/misc/Manifest.h>
25 #include <ripple/app/misc/impl/AccountTxPaging.h>
26 #include <ripple/app/rdb/RelationalDBInterface_nodes.h>
27 #include <ripple/app/rdb/RelationalDBInterface_postgres.h>
28 #include <ripple/app/rdb/backend/RelationalDBInterfaceSqlite.h>
29 #include <ripple/basics/BasicConfig.h>
30 #include <ripple/basics/StringUtilities.h>
31 #include <ripple/core/DatabaseCon.h>
32 #include <ripple/core/SociDB.h>
33 #include <ripple/json/to_string.h>
34 #include <ripple/nodestore/DatabaseShard.h>
35 #include <boost/algorithm/string.hpp>
36 #include <boost/range/adaptor/transformed.hpp>
37 #include <soci/sqlite3/soci-sqlite3.h>
58 <<
"AccountTransactions database "
59 "should not have a primary key";
60 Throw<std::exception>();
100 bool current)
override;
253 return lgrdb_->checkoutDb();
263 return txdb_->checkoutDb();
318 firstIndex, callback);
339 firstIndex, callback);
360 firstIndex, callback);
381 firstIndex, callback);
391 auto [lgr, tx, res] =
393 txdb_ = std::move(tx);
618 res.numberOfRows += r.numberOfRows;
619 if (res.minLedgerSequence == 0)
620 res.minLedgerSequence = r.minLedgerSequence;
621 res.maxLedgerSequence = r.maxLedgerSequence;
706 session, ledgerFirstIndex,
j_))
732 session, ledgerFirstIndex,
j_))
821 while (minSeq <= maxSeq)
824 if (shardMaxSeq > maxSeq)
825 shardMaxSeq = maxSeq;
828 res.
insert(r.begin(), r.end());
831 minSeq = shardMaxSeq + 1;
854 txs.
insert(txs.
end(), tx.begin(), tx.end());
895 if (opt.maxLedger && index > seqToShardIndex(opt.maxLedger))
897 auto [r, total] = ripple::getOldestAccountTxs(
898 session, app_, ledgerMaster, opt, limit_used, j_);
899 ret.insert(ret.end(), r.begin(), r.end());
910 if (opt.offset <= total)
921 RelationalDBInterface::AccountTxs
922 RelationalDBInterfaceSqliteImp::getNewestAccountTxs(
928 if (existsTransaction())
930 auto db = checkoutTransaction();
940 iterateTransactionBack(
944 if (opt.minLedger && index < seqToShardIndex(opt.minLedger))
946 auto [r, total] = ripple::getNewestAccountTxs(
947 session, app_, ledgerMaster, opt, limit_used, j_);
948 ret.insert(ret.end(), r.begin(), r.end());
959 if (opt.offset <= total)
970 RelationalDBInterface::MetaTxsList
971 RelationalDBInterfaceSqliteImp::getOldestAccountTxsB(
975 if (existsTransaction())
977 auto db = checkoutTransaction();
985 iterateTransactionForward(
989 if (opt.maxLedger && index > seqToShardIndex(opt.maxLedger))
991 auto [r, total] = ripple::getOldestAccountTxsB(
992 session, app_, opt, limit_used, j_);
993 ret.insert(ret.end(), r.begin(), r.end());
1004 if (opt.offset <= total)
1007 opt.offset -= total;
1015 RelationalDBInterface::MetaTxsList
1016 RelationalDBInterfaceSqliteImp::getNewestAccountTxsB(
1020 if (existsTransaction())
1022 auto db = checkoutTransaction();
1030 iterateTransactionBack(
1034 if (opt.minLedger && index < seqToShardIndex(opt.minLedger))
1036 auto [r, total] = ripple::getNewestAccountTxsB(
1037 session, app_, opt, limit_used, j_);
1038 ret.insert(ret.end(), r.begin(), r.end());
1043 limit_used += total;
1049 if (opt.offset <= total)
1052 opt.offset -= total;
1061 RelationalDBInterface::AccountTxs,
1063 RelationalDBInterfaceSqliteImp::oldestAccountTxPage(
1068 auto onUnsavedLedger =
1072 auto onTransaction = [&ret, &app](
1081 if (existsTransaction())
1083 auto db = checkoutTransaction();
1093 return {ret, newmarker};
1099 iterateTransactionForward(
1103 if (opt.maxLedger != UINT32_MAX &&
1104 index > seqToShardIndex(opt.minLedger))
1106 auto [marker, total] = ripple::oldestAccountTxPage(
1114 opt.marker = marker;
1117 limit_used += total;
1121 return {ret, opt.
marker};
1127 RelationalDBInterfaceSqliteImp::newestAccountTxPage(
1132 auto onUnsavedLedger =
1136 auto onTransaction = [&ret, &app](
1145 if (existsTransaction())
1147 auto db = checkoutTransaction();
1157 return {ret, newmarker};
1163 iterateTransactionBack(
1167 if (opt.minLedger && index < seqToShardIndex(opt.minLedger))
1169 auto [marker, total] = ripple::newestAccountTxPage(
1177 opt.marker = marker;
1180 limit_used += total;
1184 return {ret, opt.
marker};
1190 RelationalDBInterfaceSqliteImp::oldestAccountTxPageB(
1195 auto onUnsavedLedger =
1198 auto onTransaction = [&ret](
1203 ret.
emplace_back(std::move(rawTxn), std::move(rawMeta), ledgerIndex);
1207 if (existsTransaction())
1209 auto db = checkoutTransaction();
1219 return {ret, newmarker};
1225 iterateTransactionForward(
1229 if (opt.maxLedger != UINT32_MAX &&
1230 index > seqToShardIndex(opt.minLedger))
1232 auto [marker, total] = ripple::oldestAccountTxPage(
1240 opt.marker = marker;
1243 limit_used += total;
1247 return {ret, opt.
marker};
1253 RelationalDBInterfaceSqliteImp::newestAccountTxPageB(
1258 auto onUnsavedLedger =
1261 auto onTransaction = [&ret](
1266 ret.
emplace_back(std::move(rawTxn), std::move(rawMeta), ledgerIndex);
1270 if (existsTransaction())
1272 auto db = checkoutTransaction();
1282 return {ret, newmarker};
1288 iterateTransactionBack(
1292 if (opt.minLedger && index < seqToShardIndex(opt.minLedger))
1294 auto [marker, total] = ripple::newestAccountTxPage(
1302 opt.marker = marker;
1305 limit_used += total;
1309 return {ret, opt.
marker};
1313 RelationalDBInterfaceSqliteImp::getTransaction(
1319 if (existsTransaction())
1321 auto db = checkoutTransaction();
1327 iterateTransactionBack(
1332 uint32_t low =
std::max(
range->lower(), firstLedgerSeq(index));
1333 uint32_t high =
std::min(
range->upper(), lastLedgerSeq(index));
1339 return res.
index() == 1 &&
1340 std::get<TxSearched>(res) != TxSearched::unknown;
1347 RelationalDBInterfaceSqliteImp::ledgerDbHasSpace(
Config const& config)
1352 auto db = checkoutLedger();
1357 return iterateLedgerBack(
1364 RelationalDBInterfaceSqliteImp::transactionDbHasSpace(
Config const& config)
1367 if (existsTransaction())
1369 auto db = checkoutTransaction();
1374 return iterateTransactionBack(
1381 RelationalDBInterfaceSqliteImp::getKBUsedAll()
1391 iterateLedgerBack({}, [&](soci::session& session,
std::uint32_t index) {
1399 RelationalDBInterfaceSqliteImp::getKBUsedLedger()
1409 iterateLedgerBack({}, [&](soci::session& session,
std::uint32_t index) {
1417 RelationalDBInterfaceSqliteImp::getKBUsedTransaction()
1420 if (existsTransaction())
1427 iterateTransactionBack(
1441 return std::make_unique<RelationalDBInterfaceSqliteImp>(
1442 app, config, jobQueue);
void deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq) override
deleteTransactionByLedgerSeq Deletes transactions from ledger with given sequence.
std::optional< LedgerInfo > getLedgerInfoByIndex(soci::session &session, LedgerIndex ledgerSeq, beast::Journal j)
getLedgerInfoByIndex Returns ledger by its sequence.
void deleteBeforeLedgerSeq(soci::session &session, TableType type, LedgerIndex ledgerSeq)
deleteBeforeLedgerSeq Deletes all entries in given table for the ledgers with given sequence and all ...
int getKBUsedAll() override
getKBUsedAll Returns space used by all databases.
@ ledgerMaster
ledger master data for signing
bool iterateLedgerForward(std::optional< std::uint32_t > firstIndex, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)
iterateLedgerForward Checkouts ledger databases for all shards in ascending order starting from given...
int getKBUsedLedger() override
getKBUsedLedger Returns space used by ledger database.
std::pair< std::vector< std::shared_ptr< Transaction > >, int > getTxHistory(soci::session &session, Application &app, LedgerIndex startIndex, int quantity, bool count)
getTxHistory Returns given number of most recent transactions starting from given number of entry.
bool iterateLedgerBack(std::optional< std::uint32_t > firstIndex, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)
iterateLedgerBack Checkouts ledger databases for all shards in descending order starting from given s...
uint256 getHashByIndex(soci::session &session, LedgerIndex ledgerIndex)
getHashByIndex Returns hash of ledger with given sequence.
std::unique_ptr< RelationalDBInterface > getRelationalDBInterfaceSqlite(Application &app, Config const &config, JobQueue &jobQueue)
std::pair< RelationalDBInterface::AccountTxs, int > getOldestAccountTxs(soci::session &session, Application &app, LedgerMaster &ledgerMaster, RelationalDBInterface::AccountTxOptions const &options, std::optional< int > const &limit_used, beast::Journal j)
getOldestAccountTxs Returns oldest transactions for given account which match given criteria starting...
std::pair< MetaTxsList, std::optional< AccountTxMarker > > newestAccountTxPageB(AccountTxPageOptions const &options) override
newestAccountTxPageB Returns newest transactions in binary form for given account which match given c...
size_t getKBUsedDB(soci::session &s)
size_t getKBUsedAll(soci::session &s)
std::pair< std::vector< RelationalDBInterface::txnMetaLedgerType >, int > getOldestAccountTxsB(soci::session &session, Application &app, RelationalDBInterface::AccountTxOptions const &options, std::optional< int > const &limit_used, beast::Journal j)
getOldestAccountTxsB Returns oldest transactions in binary form for given account which match given c...
MetaTxsList getNewestAccountTxsB(AccountTxOptions const &options) override
getNewestAccountTxsB Returns newest transactions in binary form for given account which match given c...
virtual std::uint32_t seqToShardIndex(std::uint32_t seq) const =0
Calculates the shard index for a given ledger sequence.
std::unique_ptr< DatabaseCon > lgrdb_
void deleteAccountTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) override
deleteAccountTransactionsBeforeLedgerSeq Deletes all account transactions with given ledger sequence ...
std::pair< AccountTxs, std::optional< AccountTxMarker > > oldestAccountTxPage(AccountTxPageOptions const &options) override
oldestAccountTxPage Returns oldest transactions for given account which match given criteria starting...
std::pair< MetaTxsList, std::optional< AccountTxMarker > > oldestAccountTxPageB(AccountTxPageOptions const &options) override
oldestAccountTxPageB Returns oldest transactions in binary form for given account which match given c...
DatabaseCon::Setup setup_DatabaseCon(Config const &c, std::optional< beast::Journal > j=std::nullopt)
RelationalDBInterface::CountMinMax getRowsMinMax(soci::session &session, TableType type)
getRowsMinMax Returns minumum ledger sequence, maximum ledger sequence and total number of rows in gi...
std::pair< std::optional< RelationalDBInterface::AccountTxMarker >, int > newestAccountTxPage(soci::session &session, AccountIDCache const &idCache, std::function< void(std::uint32_t)> const &onUnsavedLedger, std::function< void(std::uint32_t, std::string const &, Blob &&, Blob &&)> const &onTransaction, RelationalDBInterface::AccountTxPageOptions const &options, int limit_used, std::uint32_t page_length)
newestAccountTxPage Searches newest transactions for given account which match given criteria startin...
std::optional< LedgerInfo > getLedgerInfoByHash(soci::session &session, uint256 const &ledgerHash, beast::Journal j)
getLedgerInfoByHash Returns info of ledger with given hash.
virtual std::uint32_t lastLedgerSeq(std::uint32_t shardIndex) const =0
Calculates the last ledger sequence for a given shard index.
virtual NodeStore::DatabaseShard * getShardStore()=0
std::optional< LedgerIndex > getAccountTransactionsMinLedgerSeq() override
getAccountTransactionsMinLedgerSeq Returns minimum ledger sequence among records in the AccountTransa...
std::optional< AccountTxMarker > marker
auto checkoutTransaction()
checkoutTransaction Checkouts and returns node transaction DB.
std::unique_ptr< DatabaseCon > txdb_
bool iterateTransactionBack(std::optional< std::uint32_t > firstIndex, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)
iterateTransactionForward Checkouts transaction databases for all shards in descending order starting...
uint256 getHashByIndex(LedgerIndex ledgerIndex) override
getHashByIndex Returns hash of ledger with given sequence.
bool transactionDbHasSpace(Config const &config) override
transactionDbHasSpace Checks if transaction database has available space.
std::optional< LedgerInfo > getLimitedNewestLedgerInfo(soci::session &session, LedgerIndex ledgerFirstIndex, beast::Journal j)
getLimitedNewestLedgerInfo Returns info of newest ledger from ledgers with sequences greather or equa...
std::optional< LedgerInfo > getLimitedOldestLedgerInfo(soci::session &session, LedgerIndex ledgerFirstIndex, beast::Journal j)
getLimitedOldestLedgerInfo Returns info of oldest ledger from ledgers with sequences greather or equa...
virtual AccountIDCache const & accountIDCache() const =0
bool makeLedgerDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeLedgerDBs Opens node ledger and transaction databases, and saves its descriptors into internal va...
std::pair< RelationalDBInterface::AccountTxs, int > getNewestAccountTxs(soci::session &session, Application &app, LedgerMaster &ledgerMaster, RelationalDBInterface::AccountTxOptions const &options, std::optional< int > const &limit_used, beast::Journal j)
getNewestAccountTxs Returns newest transactions for given account which match given criteria starting...
Integers of any length that is a multiple of 32-bits.
LedgerIndex lastLedgerSeq(std::uint32_t shardIndex)
lastLedgerSeq Returns last ledger sequence for given shard.
bool doTransaction(LedgerIndex ledgerSeq, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)
doTransaction Checkouts transaction database for shard containing given ledger and calls given callba...
MetaTxsList getOldestAccountTxsB(AccountTxOptions const &options) override
getOldestAccountTxsB Returns oldest transactions in binary form for given account which match given c...
static bool saveValidatedLedger(Application &app, std::shared_ptr< Ledger const > const &ledger, bool current)
int getKBUsedTransaction() override
getKBUsedTransaction Returns space used by transaction database.
void deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) override
deleteTransactionsBeforeLedgerSeq Deletes all transactions with given ledger sequence and all sequenc...
virtual LedgerMaster & getLedgerMaster()=0
std::optional< LedgerInfo > getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex) override
getLimitedOldestLedgerInfo Returns info of oldest ledger from ledgers with sequences greater or equal...
bool existsLedger()
checkoutLedger Checks if node ledger DB exists.
std::size_t getTransactionCount() override
getTransactionCount Returns number of transactions.
void saveLedgerAsync(Application &app, std::uint32_t seq)
auto checkoutLedger()
checkoutTransaction Checkouts and returns node ledger DB.
std::optional< LedgerInfo > getLedgerInfoByHash(uint256 const &ledgerHash) override
getLedgerInfoByHash Returns info of ledger with given hash.
std::optional< LedgerInfo > getNewestLedgerInfo() override
getNewestLedgerInfo Returns info of newest saved ledger.
std::optional< LedgerIndex > getMinLedgerSeq() override
getMinLedgerSeq Returns minimum ledger sequence in Ledgers table.
void deleteBeforeLedgerSeq(LedgerIndex ledgerSeq) override
deleteBeforeLedgerSeq Deletes all ledgers with given sequence and all sequences below it.
void deleteByLedgerSeq(soci::session &session, TableType type, LedgerIndex ledgerSeq)
deleteByLedgerSeq Deletes all entries in given table for the ledger with given sequence.
std::variant< AccountTx, TxSearched > getTransaction(uint256 const &id, std::optional< ClosedInterval< uint32_t >> const &range, error_code_i &ec) override
getTransaction Returns transaction with given hash.
@ current
This was a new validation and was added.
A generic endpoint for log messages.
virtual bool callForTransactionSQL(LedgerIndex ledgerSeq, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)=0
callForTransactionSQL Checkouts transaction database for shard containing given ledger and calls give...
virtual std::uint32_t firstLedgerSeq(std::uint32_t shardIndex) const =0
Calculates the first ledger sequence for a given shard index.
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
RelationalDBInterface::CountMinMax getLedgerCountMinMax() override
getLedgerCountMinMax Returns minumum ledger sequence, maximum ledger sequence and total number of sav...
bool iterateTransactionForward(std::optional< std::uint32_t > firstIndex, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)
iterateTransactionForward Checkouts transaction databases for all shards in ascending order starting ...
std::optional< LedgerInfo > getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex) override
getLimitedNewestLedgerInfo Returns info of newest ledger from ledgers with sequences greater or equal...
A pool of threads to perform work.
LedgerIndex firstLedgerSeq(std::uint32_t shardIndex)
firstLedgerSeq Returns first ledger sequence for given shard.
std::pair< std::vector< RelationalDBInterface::txnMetaLedgerType >, int > getNewestAccountTxsB(soci::session &session, Application &app, RelationalDBInterface::AccountTxOptions const &options, std::optional< int > const &limit_used, beast::Journal j)
getNewestAccountTxsB Returns newest transactions in binary form for given account which match given c...
bool existsTransaction()
checkoutTransaction Checks if node transaction DB exists.
std::vector< std::shared_ptr< Transaction > > getTxHistory(LedgerIndex startIndex) override
getTxHistory Returns most recent 20 transactions starting from given number or entry.
std::optional< LedgerInfo > getLedgerInfoByIndex(LedgerIndex ledgerSeq) override
getLedgerInfoByIndex Returns ledger by its sequence.
std::uint32_t seqToShardIndex(LedgerIndex ledgerSeq)
seqToShardIndex Converts ledgers sequence to shard index.
T emplace_back(T... args)
std::variant< RelationalDBInterface::AccountTx, TxSearched > getTransaction(soci::session &session, Application &app, uint256 const &id, std::optional< ClosedInterval< uint32_t >> const &range, error_code_i &ec)
getTransaction Returns transaction with given hash.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::size_t getRows(soci::session &session, TableType type)
getRows Returns number of rows in given table.
bool doLedger(LedgerIndex ledgerSeq, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)
doLedger Checkouts ledger database for shard containing given ledger and calls given callback functio...
virtual beast::Journal journal(std::string const &name)=0
std::vector< txnMetaLedgerType > MetaTxsList
std::optional< LedgerHashPair > getHashesByIndex(soci::session &session, LedgerIndex ledgerIndex, beast::Journal j)
getHashesByIndex Returns hash of the ledger and hash of parent ledger for the ledger of given sequenc...
std::optional< LedgerHashPair > getHashesByIndex(LedgerIndex ledgerIndex) override
getHashesByIndex Returns hash of the ledger and hash of parent ledger for the ledger of given sequenc...
AccountTxs getNewestAccountTxs(AccountTxOptions const &options) override
getNewestAccountTxs Returns newest transactions for given account which match given criteria starting...
virtual bool iterateLedgerSQLsBack(std::optional< std::uint32_t > maxShardIndex, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)=0
iterateLedgerSQLsBack Checkouts ledger databases for all shards in descending order starting from giv...
void convertBlobsToTxResult(RelationalDBInterface::AccountTxs &to, std::uint32_t ledger_index, std::string const &status, Blob const &rawTxn, Blob const &rawMeta, Application &app)
virtual bool callForLedgerSQL(LedgerIndex ledgerSeq, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)=0
callForLedgerSQL Checkouts ledger database for shard containing given ledger and calls given callback...
DatabasePairValid makeLedgerDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeLedgerDBs Opens ledger and transactions databases.
std::pair< AccountTxs, std::optional< AccountTxMarker > > newestAccountTxPage(AccountTxPageOptions const &options) override
newestAccountTxPage Returns newest transactions for given account which match given criteria starting...
bool ledgerDbHasSpace(Config const &config) override
ledgerDbHasSpace Checks if ledger database has available space.
std::pair< std::optional< RelationalDBInterface::AccountTxMarker >, int > oldestAccountTxPage(soci::session &session, AccountIDCache const &idCache, std::function< void(std::uint32_t)> const &onUnsavedLedger, std::function< void(std::uint32_t, std::string const &, Blob &&, Blob &&)> const &onTransaction, RelationalDBInterface::AccountTxPageOptions const &options, int limit_used, std::uint32_t page_length)
oldestAccountTxPage Searches oldest transactions for given account which match given criteria startin...
std::size_t getAccountTransactionCount() override
getAccountTransactionCount Returns number of account transactions.
std::optional< LedgerIndex > getMaxLedgerSeq() override
getMaxLedgerSeq Returns maximum ledger sequence in Ledgers table.
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
bool dbHasSpace(soci::session &session, Config const &config, beast::Journal j)
dbHasSpace Checks if given database has available space.
std::optional< LedgerIndex > getMinLedgerSeq(soci::session &session, TableType type)
getMinLedgerSeq Returns minimum ledger sequence in given table.
AccountTxs getOldestAccountTxs(AccountTxOptions const &options) override
getOldestAccountTxs Returns oldest transactions for given account which match given criteria starting...
bool saveValidatedLedger(std::shared_ptr< Ledger const > const &ledger, bool current) override
saveValidatedLedger Saves ledger into database.
std::optional< LedgerInfo > getNewestLedgerInfo(soci::session &session, beast::Journal j)
getNewestLedgerInfo Returns info of newest saved ledger.
std::optional< LedgerIndex > getTransactionsMinLedgerSeq() override
getTransactionsMinLedgerSeq Returns minimum ledger sequence among records in the Transactions table.
virtual bool iterateLedgerSQLsForward(std::optional< std::uint32_t > minShardIndex, std::function< bool(soci::session &session, std::uint32_t index)> const &callback)=0
iterateLedgerSQLsForward Checkouts ledger databases for all shards in ascending order starting from g...
std::optional< LedgerIndex > getMaxLedgerSeq(soci::session &session, TableType type)
getMaxLedgerSeq Returns maximum ledger sequence in given table.
RelationalDBInterfaceSqliteImp(Application &app, Config const &config, JobQueue &jobQueue)
std::vector< AccountTx > AccountTxs