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/PendingSaves.h>
24 #include <ripple/app/ledger/TransactionMaster.h>
25 #include <ripple/app/misc/Manifest.h>
26 #include <ripple/app/rdb/RelationalDBInterface.h>
27 #include <ripple/app/rdb/RelationalDBInterface_nodes.h>
28 #include <ripple/basics/BasicConfig.h>
29 #include <ripple/basics/StringUtilities.h>
30 #include <ripple/core/DatabaseCon.h>
31 #include <ripple/core/SociDB.h>
32 #include <ripple/json/to_string.h>
33 #include <boost/algorithm/string.hpp>
34 #include <boost/range/adaptor/transformed.hpp>
35 #include <soci/sqlite3/soci-sqlite3.h>
49 "Need to modify switch statement if enum is modified");
55 return "Transactions";
57 return "AccountTransactions";
71 auto lgr{std::make_unique<DatabaseCon>(
73 lgr->getSession() << boost::str(
74 boost::format(
"PRAGMA cache_size=-%d;") %
80 auto tx{std::make_unique<DatabaseCon>(
82 tx->getSession() << boost::str(
83 boost::format(
"PRAGMA cache_size=-%d;") %
95 (tx->getSession().prepare
96 << (
"PRAGMA table_info(AccountTransactions);"),
101 soci::into(dflt_value, ind),
109 return {std::move(lgr), std::move(tx),
false};
114 return {std::move(lgr), std::move(tx),
true};
117 return {std::move(lgr), {},
true};
125 boost::optional<LedgerIndex> m;
126 session << query, soci::into(m);
135 boost::optional<LedgerIndex> m;
136 session << query, soci::into(m);
143 session <<
"DELETE FROM " <<
to_string(type)
144 <<
" WHERE LedgerSeq == " << ledgerSeq <<
";";
149 soci::session& session,
153 session <<
"DELETE FROM " <<
to_string(type) <<
" WHERE LedgerSeq < "
161 session <<
"SELECT COUNT(*) AS rows "
169 RelationalDBInterface::CountMinMax
173 session <<
"SELECT COUNT(*) AS rows, "
174 "MIN(LedgerSeq) AS first, "
175 "MAX(LedgerSeq) AS last "
192 auto j = app.
journal(
"Ledger");
196 JLOG(j.trace()) <<
"saveValidatedLedger " << (
current ?
"" :
"fromAcquire ")
201 JLOG(j.fatal()) <<
"AH is zero: " <<
getJson({*ledger, {}});
209 JLOG(j.fatal()) <<
"saveAcceptedLedger: seq=" << seq
231 aLedger = std::make_shared<AcceptedLedger>(ledger, app);
238 JLOG(j.warn()) <<
"An accepted ledger was missing nodes";
247 static boost::format deleteLedger(
248 "DELETE FROM Ledgers WHERE LedgerSeq = %u;");
249 static boost::format deleteTrans1(
250 "DELETE FROM Transactions WHERE LedgerSeq = %u;");
251 static boost::format deleteTrans2(
252 "DELETE FROM AccountTransactions WHERE LedgerSeq = %u;");
253 static boost::format deleteAcctTrans(
254 "DELETE FROM AccountTransactions WHERE TransID = '%s';");
258 *db << boost::str(deleteLedger % seq);
265 soci::transaction tr(*db);
267 *db << boost::str(deleteTrans1 % seq);
268 *db << boost::str(deleteTrans2 % seq);
272 for (
auto const& [_, acceptedLedgerTx] : aLedger->getMap())
283 auto const& accts = acceptedLedgerTx->getAffected();
288 "INSERT INTO AccountTransactions "
289 "(TransID, Account, LedgerSeq, TxnSeq) VALUES ");
297 for (
auto const& account : accts)
317 JLOG(j.trace()) <<
"ActTx: " << sql;
322 JLOG(j.warn()) <<
"Transaction in ledger " << seq
323 <<
" affects no accounts";
324 JLOG(j.warn()) << acceptedLedgerTx->getTxn()->getJson(
330 acceptedLedgerTx->getTxn()->getMetaSQL(
331 seq, acceptedLedgerTx->getEscMeta()) +
342 R
"sql(INSERT OR REPLACE INTO Ledgers
343 (LedgerHash,LedgerSeq,PrevHash,TotalCoins,ClosingTime,PrevClosingTime,
344 CloseTimeRes,CloseFlags,AccountSetHash,TransSetHash)
346 (:ledgerHash,:ledgerSeq,:prevHash,:totalCoins,:closingTime,:prevClosingTime,
347 :closeTimeRes,:closeFlags,:accountSetHash,:transSetHash);)sql");
351 soci::transaction tr(*db);
356 auto const closeTime =
358 auto const parentCloseTime =
360 auto const closeTimeResolution =
366 *db << addLedger, soci::use(hash), soci::use(seq),
367 soci::use(parentHash), soci::use(drops), soci::use(closeTime),
368 soci::use(parentCloseTime), soci::use(closeTimeResolution),
369 soci::use(closeFlags), soci::use(accountHash),
389 soci::session& session,
394 boost::optional<std::string> hash, parentHash, accountHash, txHash;
395 boost::optional<std::uint64_t> seq, drops, closeTime, parentCloseTime,
396 closeTimeResolution, closeFlags;
400 "LedgerHash, PrevHash, AccountSetHash, TransSetHash, "
402 "ClosingTime, PrevClosingTime, CloseTimeRes, CloseFlags,"
403 "LedgerSeq FROM Ledgers " +
406 session << sql, soci::into(hash), soci::into(parentHash),
407 soci::into(accountHash), soci::into(txHash), soci::into(drops),
408 soci::into(closeTime), soci::into(parentCloseTime),
409 soci::into(closeTimeResolution), soci::into(closeFlags),
412 if (!session.got_data())
414 JLOG(j.
debug()) <<
"Ledger not found: " << sqlSuffix;
423 if (hash && !info.hash.parseHex(*hash))
425 JLOG(j.
debug()) <<
"Hash parse error for ledger: " << sqlSuffix;
429 if (parentHash && !info.parentHash.parseHex(*parentHash))
431 JLOG(j.
debug()) <<
"parentHash parse error for ledger: " << sqlSuffix;
435 if (accountHash && !info.accountHash.parseHex(*accountHash))
437 JLOG(j.
debug()) <<
"accountHash parse error for ledger: " << sqlSuffix;
441 if (txHash && !info.txHash.parseHex(*txHash))
443 JLOG(j.
debug()) <<
"txHash parse error for ledger: " << sqlSuffix;
447 info.seq = rangeCheckedCast<std::uint32_t>(seq.value_or(0));
448 info.drops =
drops.value_or(0);
449 info.closeTime = time_point{duration{closeTime.value_or(0)}};
450 info.parentCloseTime = time_point{duration{parentCloseTime.value_or(0)}};
451 info.closeFlags = closeFlags.value_or(0);
452 info.closeTimeResolution = duration{closeTimeResolution.value_or(0)};
459 soci::session& session,
464 s <<
"WHERE LedgerSeq = " << ledgerSeq;
472 s <<
"ORDER BY LedgerSeq DESC LIMIT 1";
478 soci::session& session,
484 " ORDER BY LedgerSeq ASC LIMIT 1";
490 soci::session& session,
496 " ORDER BY LedgerSeq DESC LIMIT 1";
502 soci::session& session,
507 s <<
"WHERE LedgerHash = '" << ledgerHash <<
"'";
517 "SELECT LedgerHash FROM Ledgers INDEXED BY SeqLedger WHERE LedgerSeq='";
518 sql.
append(beast::lexicalCastThrow<std::string>(ledgerIndex));
524 boost::optional<std::string> lh;
525 session << sql, soci::into(lh);
527 if (!session.got_data() || !lh)
535 if (!ret.parseHex(hash))
543 soci::session& session,
548 boost::optional<std::string> lhO, phO;
550 session <<
"SELECT LedgerHash,PrevHash FROM Ledgers "
551 "INDEXED BY SeqLedger WHERE LedgerSeq = :ls;",
552 soci::into(lhO), soci::into(phO), soci::use(ledgerIndex);
556 auto stream = j.
trace();
557 JLOG(stream) <<
"Don't have ledger " << ledgerIndex;
561 LedgerHashPair hashes;
562 if (!hashes.ledgerHash.parseHex(*lhO) || !hashes.parentHash.parseHex(*phO))
565 JLOG(stream) <<
"Error parse hashes for ledger " << ledgerIndex;
574 soci::session& session,
580 "SELECT LedgerSeq,LedgerHash,PrevHash FROM Ledgers WHERE LedgerSeq >= ";
581 sql.
append(beast::lexicalCastThrow<std::string>(minSeq));
582 sql.
append(
" AND LedgerSeq <= ");
583 sql.
append(beast::lexicalCastThrow<std::string>(maxSeq));
589 boost::optional<std::string> ph;
591 (session.prepare << sql,
600 LedgerHashPair& hashes = res[rangeCheckedCast<LedgerIndex>(ls)];
601 if (!hashes.ledgerHash.parseHex(lh))
603 JLOG(j.
warn()) <<
"Error parsed hash for ledger seq: " << ls;
607 JLOG(j.
warn()) <<
"Null prev hash for ledger seq: " << ls;
609 else if (!hashes.parentHash.parseHex(*ph))
611 JLOG(j.
warn()) <<
"Error parsed prev hash for ledger seq: " << ls;
619 soci::session& session,
627 "SELECT LedgerSeq, Status, RawTxn "
628 "FROM Transactions ORDER BY LedgerSeq DESC LIMIT %u,%u;") %
629 startIndex % quantity);
636 boost::optional<std::uint64_t> ledgerSeq;
637 boost::optional<std::string>
status;
638 soci::blob sociRawTxnBlob(session);
643 (session.prepare << sql,
644 soci::into(ledgerSeq),
646 soci::into(sociRawTxnBlob, rti));
651 if (soci::i_ok == rti)
652 convert(sociRawTxnBlob, rawTxn);
657 ledgerSeq,
status, rawTxn, app))
666 session <<
"SELECT COUNT(*) FROM Transactions;", soci::into(total);
698 RelationalDBInterface::AccountTxOptions
const& options,
714 else if (options.limit == UINT32_MAX)
716 numberOfResults = binary ? BINARY_PAGE_LENGTH : NONBINARY_PAGE_LENGTH;
718 else if (!options.bUnlimited)
721 binary ? BINARY_PAGE_LENGTH : NONBINARY_PAGE_LENGTH, options.limit);
725 numberOfResults = options.limit;
730 if (numberOfResults <= *limit_used)
733 numberOfResults -= *limit_used;
739 if (options.maxLedger)
741 maxClause = boost::str(
742 boost::format(
"AND AccountTransactions.LedgerSeq <= '%u'") %
746 if (options.minLedger)
748 minClause = boost::str(
749 boost::format(
"AND AccountTransactions.LedgerSeq >= '%u'") %
757 boost::format(
"SELECT %s FROM AccountTransactions "
758 "WHERE Account = '%s' %s %s LIMIT %u, %u;") %
759 selection % app.accountIDCache().toBase58(options.account) %
760 maxClause % minClause %
761 beast::lexicalCastThrow<std::string>(options.offset) %
762 beast::lexicalCastThrow<std::string>(numberOfResults));
767 "AccountTransactions INNER JOIN Transactions "
768 "ON Transactions.TransID = AccountTransactions.TransID "
769 "WHERE Account = '%s' %s %s "
770 "ORDER BY AccountTransactions.LedgerSeq %s, "
771 "AccountTransactions.TxnSeq %s, AccountTransactions.TransID %s "
773 selection % app.accountIDCache().toBase58(options.account) %
774 maxClause % minClause % (descending ?
"DESC" :
"ASC") %
775 (descending ?
"DESC" :
"ASC") % (descending ?
"DESC" :
"ASC") %
776 beast::lexicalCastThrow<std::string>(options.offset) %
777 beast::lexicalCastThrow<std::string>(numberOfResults));
778 JLOG(j.
trace()) <<
"txSQL query: " << sql;
803 soci::session& session,
805 LedgerMaster& ledgerMaster,
806 RelationalDBInterface::AccountTxOptions
const& options,
815 "AccountTransactions.LedgerSeq,Status,RawTxn,TxnMeta",
828 boost::optional<std::uint64_t> ledgerSeq;
829 boost::optional<std::string>
status;
830 soci::blob sociTxnBlob(session), sociTxnMetaBlob(session);
831 soci::indicator rti, tmi;
832 Blob rawTxn, txnMeta;
835 (session.prepare << sql,
836 soci::into(ledgerSeq),
838 soci::into(sociTxnBlob, rti),
839 soci::into(sociTxnMetaBlob, tmi));
844 if (soci::i_ok == rti)
849 if (soci::i_ok == tmi)
850 convert(sociTxnMetaBlob, txnMeta);
860 rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0));
863 <<
"Recovering ledger " << seq <<
", txn " << txn->getID();
873 std::make_shared<TxMeta>(
874 txn->getID(), txn->getLedger(), txnMeta));
879 if (!total && limit_used)
881 RelationalDBInterface::AccountTxOptions opt = options;
884 app,
"COUNT(*)", opt, limit_used, descending,
false,
false, j);
886 session << sql1, soci::into(total);
897 soci::session& session,
899 LedgerMaster& ledgerMaster,
900 RelationalDBInterface::AccountTxOptions
const& options,
905 session, app, ledgerMaster, options, limit_used,
false, j);
910 soci::session& session,
912 LedgerMaster& ledgerMaster,
913 RelationalDBInterface::AccountTxOptions
const& options,
918 session, app, ledgerMaster, options, limit_used,
true, j);
942 soci::session& session,
944 RelationalDBInterface::AccountTxOptions
const& options,
953 "AccountTransactions.LedgerSeq,Status,RawTxn,TxnMeta",
967 boost::optional<std::uint64_t> ledgerSeq;
968 boost::optional<std::string>
status;
969 soci::blob sociTxnBlob(session), sociTxnMetaBlob(session);
970 soci::indicator rti, tmi;
973 (session.prepare << sql,
974 soci::into(ledgerSeq),
976 soci::into(sociTxnBlob, rti),
977 soci::into(sociTxnMetaBlob, tmi));
983 if (soci::i_ok == rti)
986 if (soci::i_ok == tmi)
987 convert(sociTxnMetaBlob, txnMeta);
990 rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0));
992 ret.
emplace_back(std::move(rawTxn), std::move(txnMeta), seq);
996 if (!total && limit_used)
998 RelationalDBInterface::AccountTxOptions opt = options;
1001 app,
"COUNT(*)", opt, limit_used, descending,
true,
false, j);
1003 session << sql1, soci::into(total);
1009 return {ret, total};
1014 soci::session& session,
1016 RelationalDBInterface::AccountTxOptions
const& options,
1020 return getAccountTxsB(session, app, options, limit_used,
false, j);
1025 soci::session& session,
1027 RelationalDBInterface::AccountTxOptions
const& options,
1031 return getAccountTxsB(session, app, options, limit_used,
true, j);
1058 soci::session& session,
1059 AccountIDCache
const& idCache,
1064 RelationalDBInterface::AccountTxPageOptions
const& options,
1071 bool lookingForMarker = options.marker.has_value();
1075 if (options.limit == 0 || options.limit == UINT32_MAX ||
1076 (options.limit > page_length && !options.bAdmin))
1077 numberOfResults = page_length;
1079 numberOfResults = options.limit;
1081 if (numberOfResults < limit_used)
1082 return {options.marker, -1};
1083 numberOfResults -= limit_used;
1093 if (lookingForMarker)
1095 findLedger = options.marker->ledgerSeq;
1096 findSeq = options.marker->txnSeq;
1101 newmarker = options.marker;
1104 R
"(SELECT AccountTransactions.LedgerSeq,AccountTransactions.TxnSeq,
1105 Status,RawTxn,TxnMeta
1106 FROM AccountTransactions INNER JOIN Transactions
1107 ON Transactions.TransID = AccountTransactions.TransID
1108 AND AccountTransactions.Account = '%s' WHERE
1115 const char*
const order =
forward ?
"ASC" :
"DESC";
1117 if (findLedger == 0)
1121 prefix + (R
"(AccountTransactions.LedgerSeq BETWEEN '%u' AND '%u'
1122 ORDER BY AccountTransactions.LedgerSeq %s,
1123 AccountTransactions.TxnSeq %s
1125 idCache.toBase58(options.account) % options.minLedger %
1126 options.maxLedger % order % order % queryLimit);
1132 forward ? findLedger + 1 : options.minLedger;
1134 forward ? options.maxLedger : findLedger - 1;
1136 auto b58acct = idCache.toBase58(options.account);
1139 R
"(SELECT AccountTransactions.LedgerSeq,AccountTransactions.TxnSeq,
1140 Status,RawTxn,TxnMeta
1141 FROM AccountTransactions, Transactions WHERE
1142 (AccountTransactions.TransID = Transactions.TransID AND
1143 AccountTransactions.Account = '%s' AND
1144 AccountTransactions.LedgerSeq BETWEEN '%u' AND '%u')
1146 (AccountTransactions.TransID = Transactions.TransID AND
1147 AccountTransactions.Account = '%s' AND
1148 AccountTransactions.LedgerSeq = '%u' AND
1149 AccountTransactions.TxnSeq %s '%u')
1150 ORDER BY AccountTransactions.LedgerSeq %s,
1151 AccountTransactions.TxnSeq %s
1154 b58acct % minLedger % maxLedger % b58acct % findLedger % compare %
1155 findSeq % order % order % queryLimit);
1163 boost::optional<std::uint64_t> ledgerSeq;
1164 boost::optional<std::uint32_t> txnSeq;
1165 boost::optional<std::string>
status;
1166 soci::blob txnData(session);
1167 soci::blob txnMeta(session);
1168 soci::indicator dataPresent, metaPresent;
1170 soci::statement st =
1171 (session.prepare << sql,
1172 soci::into(ledgerSeq),
1175 soci::into(txnData, dataPresent),
1176 soci::into(txnMeta, metaPresent));
1182 if (lookingForMarker)
1184 if (findLedger == ledgerSeq.value_or(0) &&
1185 findSeq == txnSeq.value_or(0))
1187 lookingForMarker =
false;
1192 else if (numberOfResults == 0)
1195 rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0)),
1200 if (dataPresent == soci::i_ok)
1205 if (metaPresent == soci::i_ok)
1211 if (rawMeta.size() == 0)
1212 onUnsavedLedger(ledgerSeq.value_or(0));
1217 rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0)),
1220 std::move(rawMeta));
1234 return {newmarker, total};
1239 soci::session& session,
1262 soci::session& session,
1285 soci::session& session,
1292 "SELECT LedgerSeq,Status,RawTxn,TxnMeta "
1293 "FROM Transactions WHERE TransID='";
1299 boost::optional<std::uint64_t> ledgerSeq;
1300 boost::optional<std::string> status;
1301 Blob rawTxn, rawMeta;
1303 soci::blob sociRawTxnBlob(session), sociRawMetaBlob(session);
1304 soci::indicator txn, meta;
1306 session << sql, soci::into(ledgerSeq), soci::into(status),
1307 soci::into(sociRawTxnBlob, txn), soci::into(sociRawMetaBlob, meta);
1309 auto const got_data = session.got_data();
1311 if ((!got_data || txn != soci::i_ok || meta != soci::i_ok) && !
range)
1317 soci::indicator rti;
1320 <<
"SELECT COUNT(DISTINCT LedgerSeq) FROM Transactions WHERE "
1321 "LedgerSeq BETWEEN "
1322 <<
range->first() <<
" AND " <<
range->last() <<
";",
1323 soci::into(count, rti);
1325 if (!session.got_data() || rti != soci::i_ok)
1328 return count == (
range->last() -
range->first() + 1)
1333 convert(sociRawTxnBlob, rawTxn);
1334 convert(sociRawMetaBlob, rawMeta);
1343 return std::pair{std::move(txn),
nullptr};
1346 rangeCheckedCast<std::uint32_t>(ledgerSeq.value());
1348 auto txMeta = std::make_shared<TxMeta>(
id, inLedger, rawMeta);
1350 return std::pair{std::move(txn), std::move(txMeta)};
1354 JLOG(app.journal(
"Ledger").warn())
1355 <<
"Unable to deserialize transaction from raw SQL value. Error: "
1367 boost::filesystem::space_info
space =
1368 boost::filesystem::space(config.legacy(
"database_path"));
1372 JLOG(j.
fatal()) <<
"Remaining free disk space is less than 512MB";
1376 if (config.useTxTables())
1379 boost::filesystem::path dbPath = dbSetup.dataDir /
TxDBName;
1380 boost::system::error_code ec;
1382 boost::filesystem::file_size(dbPath, ec);
1386 <<
"Error checking transaction db file size: " << ec.message();
1390 static auto const pageSize = [&] {
1392 session <<
"PRAGMA page_size;", soci::into(ps);
1395 static auto const maxPages = [&] {
1397 session <<
"PRAGMA max_page_count;", soci::into(mp);
1401 session <<
"PRAGMA page_count;", soci::into(pageCount);
1404 safe_cast<std::uint64_t>(freePages) * pageSize;
1406 <<
"Transaction DB pathname: " << dbPath.string()
1407 <<
"; file size: " << dbSize.
value_or(-1) <<
" bytes"
1408 <<
"; SQLite page size: " << pageSize <<
" bytes"
1409 <<
"; Free pages: " << freePages <<
"; Free space: " << freeSpace
1411 <<
"Note that this does not take into account available disk "
1417 <<
"Free SQLite space for transaction db is less than "
1418 "512MB. To fix this, rippled must be executed with the "
1419 "vacuum parameter before restarting. "
1420 "Note that this activity can take multiple days, "
1421 "depending on database size.";