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;
320 else if (
auto const sleTxn = acceptedLedgerTx->getTxn();
325 JLOG(j.warn()) <<
"Transaction in ledger " << seq
326 <<
" affects no accounts";
332 acceptedLedgerTx->getTxn()->getMetaSQL(
333 seq, acceptedLedgerTx->getEscMeta()) +
344 R
"sql(INSERT OR REPLACE INTO Ledgers
345 (LedgerHash,LedgerSeq,PrevHash,TotalCoins,ClosingTime,PrevClosingTime,
346 CloseTimeRes,CloseFlags,AccountSetHash,TransSetHash)
348 (:ledgerHash,:ledgerSeq,:prevHash,:totalCoins,:closingTime,:prevClosingTime,
349 :closeTimeRes,:closeFlags,:accountSetHash,:transSetHash);)sql");
353 soci::transaction tr(*db);
358 auto const closeTime =
360 auto const parentCloseTime =
362 auto const closeTimeResolution =
368 *db << addLedger, soci::use(hash), soci::use(seq),
369 soci::use(parentHash), soci::use(drops), soci::use(closeTime),
370 soci::use(parentCloseTime), soci::use(closeTimeResolution),
371 soci::use(closeFlags), soci::use(accountHash),
391 soci::session& session,
396 boost::optional<std::string> hash, parentHash, accountHash, txHash;
397 boost::optional<std::uint64_t> seq, drops, closeTime, parentCloseTime,
398 closeTimeResolution, closeFlags;
402 "LedgerHash, PrevHash, AccountSetHash, TransSetHash, "
404 "ClosingTime, PrevClosingTime, CloseTimeRes, CloseFlags,"
405 "LedgerSeq FROM Ledgers " +
408 session << sql, soci::into(hash), soci::into(parentHash),
409 soci::into(accountHash), soci::into(txHash), soci::into(drops),
410 soci::into(closeTime), soci::into(parentCloseTime),
411 soci::into(closeTimeResolution), soci::into(closeFlags),
414 if (!session.got_data())
416 JLOG(j.
debug()) <<
"Ledger not found: " << sqlSuffix;
425 if (hash && !info.hash.parseHex(*hash))
427 JLOG(j.
debug()) <<
"Hash parse error for ledger: " << sqlSuffix;
431 if (parentHash && !info.parentHash.parseHex(*parentHash))
433 JLOG(j.
debug()) <<
"parentHash parse error for ledger: " << sqlSuffix;
437 if (accountHash && !info.accountHash.parseHex(*accountHash))
439 JLOG(j.
debug()) <<
"accountHash parse error for ledger: " << sqlSuffix;
443 if (txHash && !info.txHash.parseHex(*txHash))
445 JLOG(j.
debug()) <<
"txHash parse error for ledger: " << sqlSuffix;
449 info.seq = rangeCheckedCast<std::uint32_t>(seq.value_or(0));
450 info.drops =
drops.value_or(0);
451 info.closeTime = time_point{duration{closeTime.value_or(0)}};
452 info.parentCloseTime = time_point{duration{parentCloseTime.value_or(0)}};
453 info.closeFlags = closeFlags.value_or(0);
454 info.closeTimeResolution = duration{closeTimeResolution.value_or(0)};
461 soci::session& session,
466 s <<
"WHERE LedgerSeq = " << ledgerSeq;
474 s <<
"ORDER BY LedgerSeq DESC LIMIT 1";
480 soci::session& session,
486 " ORDER BY LedgerSeq ASC LIMIT 1";
492 soci::session& session,
498 " ORDER BY LedgerSeq DESC LIMIT 1";
504 soci::session& session,
509 s <<
"WHERE LedgerHash = '" << ledgerHash <<
"'";
519 "SELECT LedgerHash FROM Ledgers INDEXED BY SeqLedger WHERE LedgerSeq='";
520 sql.
append(beast::lexicalCastThrow<std::string>(ledgerIndex));
526 boost::optional<std::string> lh;
527 session << sql, soci::into(lh);
529 if (!session.got_data() || !lh)
537 if (!ret.parseHex(hash))
545 soci::session& session,
550 boost::optional<std::string> lhO, phO;
552 session <<
"SELECT LedgerHash,PrevHash FROM Ledgers "
553 "INDEXED BY SeqLedger WHERE LedgerSeq = :ls;",
554 soci::into(lhO), soci::into(phO), soci::use(ledgerIndex);
558 auto stream = j.
trace();
559 JLOG(stream) <<
"Don't have ledger " << ledgerIndex;
563 LedgerHashPair hashes;
564 if (!hashes.ledgerHash.parseHex(*lhO) || !hashes.parentHash.parseHex(*phO))
567 JLOG(stream) <<
"Error parse hashes for ledger " << ledgerIndex;
576 soci::session& session,
582 "SELECT LedgerSeq,LedgerHash,PrevHash FROM Ledgers WHERE LedgerSeq >= ";
583 sql.
append(beast::lexicalCastThrow<std::string>(minSeq));
584 sql.
append(
" AND LedgerSeq <= ");
585 sql.
append(beast::lexicalCastThrow<std::string>(maxSeq));
591 boost::optional<std::string> ph;
593 (session.prepare << sql,
602 LedgerHashPair& hashes = res[rangeCheckedCast<LedgerIndex>(ls)];
603 if (!hashes.ledgerHash.parseHex(lh))
605 JLOG(j.
warn()) <<
"Error parsed hash for ledger seq: " << ls;
609 JLOG(j.
warn()) <<
"Null prev hash for ledger seq: " << ls;
611 else if (!hashes.parentHash.parseHex(*ph))
613 JLOG(j.
warn()) <<
"Error parsed prev hash for ledger seq: " << ls;
621 soci::session& session,
629 "SELECT LedgerSeq, Status, RawTxn "
630 "FROM Transactions ORDER BY LedgerSeq DESC LIMIT %u,%u;") %
631 startIndex % quantity);
638 boost::optional<std::uint64_t> ledgerSeq;
639 boost::optional<std::string>
status;
640 soci::blob sociRawTxnBlob(session);
645 (session.prepare << sql,
646 soci::into(ledgerSeq),
648 soci::into(sociRawTxnBlob, rti));
653 if (soci::i_ok == rti)
654 convert(sociRawTxnBlob, rawTxn);
659 ledgerSeq,
status, rawTxn, app))
668 session <<
"SELECT COUNT(*) FROM Transactions;", soci::into(total);
700 RelationalDBInterface::AccountTxOptions
const& options,
716 else if (options.limit == UINT32_MAX)
718 numberOfResults = binary ? BINARY_PAGE_LENGTH : NONBINARY_PAGE_LENGTH;
720 else if (!options.bUnlimited)
723 binary ? BINARY_PAGE_LENGTH : NONBINARY_PAGE_LENGTH, options.limit);
727 numberOfResults = options.limit;
732 if (numberOfResults <= *limit_used)
735 numberOfResults -= *limit_used;
741 if (options.maxLedger)
743 maxClause = boost::str(
744 boost::format(
"AND AccountTransactions.LedgerSeq <= '%u'") %
748 if (options.minLedger)
750 minClause = boost::str(
751 boost::format(
"AND AccountTransactions.LedgerSeq >= '%u'") %
759 boost::format(
"SELECT %s FROM AccountTransactions "
760 "WHERE Account = '%s' %s %s LIMIT %u, %u;") %
761 selection % app.accountIDCache().toBase58(options.account) %
762 maxClause % minClause %
763 beast::lexicalCastThrow<std::string>(options.offset) %
764 beast::lexicalCastThrow<std::string>(numberOfResults));
769 "AccountTransactions INNER JOIN Transactions "
770 "ON Transactions.TransID = AccountTransactions.TransID "
771 "WHERE Account = '%s' %s %s "
772 "ORDER BY AccountTransactions.LedgerSeq %s, "
773 "AccountTransactions.TxnSeq %s, AccountTransactions.TransID %s "
775 selection % app.accountIDCache().toBase58(options.account) %
776 maxClause % minClause % (descending ?
"DESC" :
"ASC") %
777 (descending ?
"DESC" :
"ASC") % (descending ?
"DESC" :
"ASC") %
778 beast::lexicalCastThrow<std::string>(options.offset) %
779 beast::lexicalCastThrow<std::string>(numberOfResults));
780 JLOG(j.
trace()) <<
"txSQL query: " << sql;
809 soci::session& session,
811 LedgerMaster& ledgerMaster,
812 RelationalDBInterface::AccountTxOptions
const& options,
821 "AccountTransactions.LedgerSeq,Status,RawTxn,TxnMeta",
834 boost::optional<std::uint64_t> ledgerSeq;
835 boost::optional<std::string>
status;
836 soci::blob sociTxnBlob(session), sociTxnMetaBlob(session);
837 soci::indicator rti, tmi;
838 Blob rawTxn, txnMeta;
841 (session.prepare << sql,
842 soci::into(ledgerSeq),
844 soci::into(sociTxnBlob, rti),
845 soci::into(sociTxnMetaBlob, tmi));
850 if (soci::i_ok == rti)
855 if (soci::i_ok == tmi)
856 convert(sociTxnMetaBlob, txnMeta);
866 rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0));
869 <<
"Recovering ledger " << seq <<
", txn " << txn->getID();
879 std::make_shared<TxMeta>(
880 txn->getID(), txn->getLedger(), txnMeta));
885 if (!total && limit_used)
887 RelationalDBInterface::AccountTxOptions opt = options;
890 app,
"COUNT(*)", opt, limit_used, descending,
false,
false, j);
892 session << sql1, soci::into(total);
903 soci::session& session,
905 LedgerMaster& ledgerMaster,
906 RelationalDBInterface::AccountTxOptions
const& options,
911 session, app, ledgerMaster, options, limit_used,
false, j);
916 soci::session& session,
918 LedgerMaster& ledgerMaster,
919 RelationalDBInterface::AccountTxOptions
const& options,
924 session, app, ledgerMaster, options, limit_used,
true, j);
952 soci::session& session,
954 RelationalDBInterface::AccountTxOptions
const& options,
963 "AccountTransactions.LedgerSeq,Status,RawTxn,TxnMeta",
977 boost::optional<std::uint64_t> ledgerSeq;
978 boost::optional<std::string>
status;
979 soci::blob sociTxnBlob(session), sociTxnMetaBlob(session);
980 soci::indicator rti, tmi;
983 (session.prepare << sql,
984 soci::into(ledgerSeq),
986 soci::into(sociTxnBlob, rti),
987 soci::into(sociTxnMetaBlob, tmi));
993 if (soci::i_ok == rti)
996 if (soci::i_ok == tmi)
997 convert(sociTxnMetaBlob, txnMeta);
1000 rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0));
1002 ret.
emplace_back(std::move(rawTxn), std::move(txnMeta), seq);
1006 if (!total && limit_used)
1008 RelationalDBInterface::AccountTxOptions opt = options;
1011 app,
"COUNT(*)", opt, limit_used, descending,
true,
false, j);
1013 session << sql1, soci::into(total);
1019 return {ret, total};
1024 soci::session& session,
1026 RelationalDBInterface::AccountTxOptions
const& options,
1030 return getAccountTxsB(session, app, options, limit_used,
false, j);
1035 soci::session& session,
1037 RelationalDBInterface::AccountTxOptions
const& options,
1041 return getAccountTxsB(session, app, options, limit_used,
true, j);
1068 soci::session& session,
1069 AccountIDCache
const& idCache,
1074 RelationalDBInterface::AccountTxPageOptions
const& options,
1081 bool lookingForMarker = options.marker.has_value();
1085 if (options.limit == 0 || options.limit == UINT32_MAX ||
1086 (options.limit > page_length && !options.bAdmin))
1087 numberOfResults = page_length;
1089 numberOfResults = options.limit;
1091 if (numberOfResults < limit_used)
1092 return {options.marker, -1};
1093 numberOfResults -= limit_used;
1103 if (lookingForMarker)
1105 findLedger = options.marker->ledgerSeq;
1106 findSeq = options.marker->txnSeq;
1111 newmarker = options.marker;
1114 R
"(SELECT AccountTransactions.LedgerSeq,AccountTransactions.TxnSeq,
1115 Status,RawTxn,TxnMeta
1116 FROM AccountTransactions INNER JOIN Transactions
1117 ON Transactions.TransID = AccountTransactions.TransID
1118 AND AccountTransactions.Account = '%s' WHERE
1125 const char*
const order =
forward ?
"ASC" :
"DESC";
1127 if (findLedger == 0)
1131 prefix + (R
"(AccountTransactions.LedgerSeq BETWEEN '%u' AND '%u'
1132 ORDER BY AccountTransactions.LedgerSeq %s,
1133 AccountTransactions.TxnSeq %s
1135 idCache.toBase58(options.account) % options.minLedger %
1136 options.maxLedger % order % order % queryLimit);
1142 forward ? findLedger + 1 : options.minLedger;
1144 forward ? options.maxLedger : findLedger - 1;
1146 auto b58acct = idCache.toBase58(options.account);
1149 R
"(SELECT AccountTransactions.LedgerSeq,AccountTransactions.TxnSeq,
1150 Status,RawTxn,TxnMeta
1151 FROM AccountTransactions, Transactions WHERE
1152 (AccountTransactions.TransID = Transactions.TransID AND
1153 AccountTransactions.Account = '%s' AND
1154 AccountTransactions.LedgerSeq BETWEEN '%u' AND '%u')
1156 (AccountTransactions.TransID = Transactions.TransID AND
1157 AccountTransactions.Account = '%s' AND
1158 AccountTransactions.LedgerSeq = '%u' AND
1159 AccountTransactions.TxnSeq %s '%u')
1160 ORDER BY AccountTransactions.LedgerSeq %s,
1161 AccountTransactions.TxnSeq %s
1164 b58acct % minLedger % maxLedger % b58acct % findLedger % compare %
1165 findSeq % order % order % queryLimit);
1173 boost::optional<std::uint64_t> ledgerSeq;
1174 boost::optional<std::uint32_t> txnSeq;
1175 boost::optional<std::string>
status;
1176 soci::blob txnData(session);
1177 soci::blob txnMeta(session);
1178 soci::indicator dataPresent, metaPresent;
1180 soci::statement st =
1181 (session.prepare << sql,
1182 soci::into(ledgerSeq),
1185 soci::into(txnData, dataPresent),
1186 soci::into(txnMeta, metaPresent));
1192 if (lookingForMarker)
1194 if (findLedger == ledgerSeq.value_or(0) &&
1195 findSeq == txnSeq.value_or(0))
1197 lookingForMarker =
false;
1202 else if (numberOfResults == 0)
1205 rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0)),
1210 if (dataPresent == soci::i_ok)
1215 if (metaPresent == soci::i_ok)
1221 if (rawMeta.size() == 0)
1222 onUnsavedLedger(ledgerSeq.value_or(0));
1227 rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0)),
1230 std::move(rawMeta));
1244 return {newmarker, total};
1249 soci::session& session,
1272 soci::session& session,
1295 soci::session& session,
1302 "SELECT LedgerSeq,Status,RawTxn,TxnMeta "
1303 "FROM Transactions WHERE TransID='";
1309 boost::optional<std::uint64_t> ledgerSeq;
1310 boost::optional<std::string> status;
1311 Blob rawTxn, rawMeta;
1313 soci::blob sociRawTxnBlob(session), sociRawMetaBlob(session);
1314 soci::indicator txn, meta;
1316 session << sql, soci::into(ledgerSeq), soci::into(status),
1317 soci::into(sociRawTxnBlob, txn), soci::into(sociRawMetaBlob, meta);
1319 auto const got_data = session.got_data();
1321 if ((!got_data || txn != soci::i_ok || meta != soci::i_ok) && !
range)
1327 soci::indicator rti;
1330 <<
"SELECT COUNT(DISTINCT LedgerSeq) FROM Transactions WHERE "
1331 "LedgerSeq BETWEEN "
1332 <<
range->first() <<
" AND " <<
range->last() <<
";",
1333 soci::into(count, rti);
1335 if (!session.got_data() || rti != soci::i_ok)
1338 return count == (
range->last() -
range->first() + 1)
1343 convert(sociRawTxnBlob, rawTxn);
1344 convert(sociRawMetaBlob, rawMeta);
1353 return std::pair{std::move(txn),
nullptr};
1356 rangeCheckedCast<std::uint32_t>(ledgerSeq.value());
1358 auto txMeta = std::make_shared<TxMeta>(
id, inLedger, rawMeta);
1360 return std::pair{std::move(txn), std::move(txMeta)};
1364 JLOG(app.journal(
"Ledger").warn())
1365 <<
"Unable to deserialize transaction from raw SQL value. Error: "
1377 boost::filesystem::space_info
space =
1378 boost::filesystem::space(config.legacy(
"database_path"));
1382 JLOG(j.
fatal()) <<
"Remaining free disk space is less than 512MB";
1386 if (config.useTxTables())
1389 boost::filesystem::path dbPath = dbSetup.dataDir /
TxDBName;
1390 boost::system::error_code ec;
1392 boost::filesystem::file_size(dbPath, ec);
1396 <<
"Error checking transaction db file size: " << ec.message();
1400 static auto const pageSize = [&] {
1402 session <<
"PRAGMA page_size;", soci::into(ps);
1405 static auto const maxPages = [&] {
1407 session <<
"PRAGMA max_page_count;", soci::into(mp);
1411 session <<
"PRAGMA page_count;", soci::into(pageCount);
1414 safe_cast<std::uint64_t>(freePages) * pageSize;
1416 <<
"Transaction DB pathname: " << dbPath.string()
1417 <<
"; file size: " << dbSize.
value_or(-1) <<
" bytes"
1418 <<
"; SQLite page size: " << pageSize <<
" bytes"
1419 <<
"; Free pages: " << freePages <<
"; Free space: " << freeSpace
1421 <<
"Note that this does not take into account available disk "
1427 <<
"Free SQLite space for transaction db is less than "
1428 "512MB. To fix this, rippled must be executed with the "
1429 "vacuum parameter before restarting. "
1430 "Note that this activity can take multiple days, "
1431 "depending on database size.";