xrpld
Loading...
Searching...
No Matches
Node.cpp
1#include <xrpld/app/rdb/backend/detail/Node.h>
2
3#include <xrpld/app/ledger/AcceptedLedger.h>
4#include <xrpld/app/ledger/LedgerMaster.h>
5#include <xrpld/app/ledger/LedgerPersistence.h>
6#include <xrpld/app/ledger/LedgerToJson.h>
7#include <xrpld/app/ledger/TransactionMaster.h>
8#include <xrpld/core/Config.h>
9
10#include <xrpl/basics/Blob.h>
11#include <xrpl/basics/ByteUtilities.h>
12#include <xrpl/basics/Log.h>
13#include <xrpl/basics/RangeSet.h>
14#include <xrpl/basics/Slice.h>
15#include <xrpl/basics/base_uint.h>
16#include <xrpl/basics/chrono.h>
17#include <xrpl/basics/contract.h>
18#include <xrpl/basics/safe_cast.h>
19#include <xrpl/beast/utility/Journal.h>
20#include <xrpl/beast/utility/instrumentation.h>
21#include <xrpl/config/Constants.h>
22#include <xrpl/core/NetworkIDService.h>
23#include <xrpl/core/StartUpType.h>
24#include <xrpl/json/to_string.h> // IWYU pragma: keep
25#include <xrpl/ledger/PendingSaves.h>
26#include <xrpl/nodestore/NodeObject.h>
27#include <xrpl/protocol/AccountID.h>
28#include <xrpl/protocol/ErrorCodes.h>
29#include <xrpl/protocol/HashPrefix.h>
30#include <xrpl/protocol/LedgerHeader.h>
31#include <xrpl/protocol/Protocol.h>
32#include <xrpl/protocol/SField.h>
33#include <xrpl/protocol/STTx.h>
34#include <xrpl/protocol/Serializer.h>
35#include <xrpl/protocol/TxMeta.h>
36#include <xrpl/protocol/TxSearched.h>
37#include <xrpl/protocol/XRPAmount.h>
38#include <xrpl/rdb/DBInit.h>
39#include <xrpl/rdb/DatabaseCon.h>
40#include <xrpl/rdb/RelationalDatabase.h>
41#include <xrpl/rdb/SociDB.h>
42
43#include <boost/optional/optional.hpp> // IWYU pragma: keep
44#include <boost/system/detail/error_code.hpp>
45
46#include <soci/blob-exchange.h> // IWYU pragma: keep
47#include <soci/blob.h>
48#include <soci/boost-optional.h> // IWYU pragma: keep
49#include <soci/into.h>
50#include <soci/soci-backend.h>
51#include <soci/statement.h>
52#include <soci/transaction.h>
53#include <soci/use.h>
54
55#include <algorithm>
56#include <cstddef>
57#include <cstdint>
58#include <exception>
59#include <filesystem>
60#include <format>
61#include <functional>
62#include <limits>
63#include <map>
64#include <memory>
65#include <optional>
66#include <sstream>
67#include <stdexcept>
68#include <string>
69#include <system_error>
70#include <utility>
71#include <variant>
72#include <vector>
73
74namespace xrpl::detail {
75
81static std::string
83{
84 static_assert(kTableTypeCount == 3, "Need to modify switch statement if enum is modified");
85
86 switch (type)
87 {
89 return "Ledgers";
91 return "Transactions";
93 return "AccountTransactions";
94 // LCOV_EXCL_START
95 default:
96 UNREACHABLE("xrpl::detail::toString : invalid TableType");
97 return "Unknown";
98 // LCOV_EXCL_STOP
99 }
100}
101
102DatabasePairValid
104 Config const& config,
105 DatabaseCon::Setup const& setup,
106 DatabaseCon::CheckpointerSetup const& checkpointerSetup,
108{
109 // ledger database
110 auto lgr{
112 setup, kLgrDbName, setup.lgrPragma, kLgrDbInit, checkpointerSetup, j),
113 };
114 lgr->getSession() << std::format(
115 "PRAGMA cache_size=-{};", kilobytes(config.getValueFor(SizedItem::LgrDbCache)));
116
117 if (config.useTxTables())
118 {
119 // transaction database
120 auto tx{
122 setup, kTxDbName, setup.txPragma, kTxDbInit, checkpointerSetup, j),
123 };
124 tx->getSession() << std::format(
125 "PRAGMA cache_size=-{};", kilobytes(config.getValueFor(SizedItem::TxnDbCache)));
126
127 if (!setup.standAlone || setup.startUp == StartUpType::Load ||
129 {
130 // Check if AccountTransactions has primary key
131 std::string cid, name, type;
132 std::size_t notnull = 0, dfltValue = 0, pk = 0;
133 soci::indicator ind = soci::i_null;
134 soci::statement st =
135 (tx->getSession().prepare << "PRAGMA table_info(AccountTransactions);",
136 soci::into(cid),
137 soci::into(name),
138 soci::into(type),
139 soci::into(notnull),
140 soci::into(dfltValue, ind),
141 soci::into(pk));
142
143 st.execute();
144 while (st.fetch())
145 {
146 if (pk == 1)
147 {
148 return {
149 .ledgerDb = std::move(lgr),
150 .transactionDb = std::move(tx),
151 .valid = false,
152 };
153 }
154 }
155 }
156
157 return {.ledgerDb = std::move(lgr), .transactionDb = std::move(tx), .valid = true};
158 }
159
160 return {.ledgerDb = std::move(lgr), .transactionDb = {}, .valid = true};
161}
162
164getMinLedgerSeq(soci::session& session, TableType type)
165{
166 std::string const query = "SELECT MIN(LedgerSeq) FROM " + toString(type) + ";";
167 // SOCI requires boost::optional (not std::optional) as the parameter.
168 boost::optional<LedgerIndex> m;
169 session << query, soci::into(m);
170 return m ? *m : std::optional<LedgerIndex>();
171}
172
174getMaxLedgerSeq(soci::session& session, TableType type)
175{
176 std::string const query = "SELECT MAX(LedgerSeq) FROM " + toString(type) + ";";
177 // SOCI requires boost::optional (not std::optional) as the parameter.
178 boost::optional<LedgerIndex> m;
179 session << query, soci::into(m);
180 return m ? *m : std::optional<LedgerIndex>();
181}
182
183void
184deleteByLedgerSeq(soci::session& session, TableType type, LedgerIndex ledgerSeq)
185{
186 session << "DELETE FROM " << toString(type) << " WHERE LedgerSeq == " << ledgerSeq << ";";
187}
188
189void
190deleteBeforeLedgerSeq(soci::session& session, TableType type, LedgerIndex ledgerSeq)
191{
192 session << "DELETE FROM " << toString(type) << " WHERE LedgerSeq < " << ledgerSeq << ";";
193}
194
196getRows(soci::session& session, TableType type)
197{
198 std::size_t rows = 0;
199 session << "SELECT COUNT(*) AS rows "
200 "FROM "
201 << toString(type) << ";",
202 soci::into(rows);
203
204 return rows;
205}
206
208getRowsMinMax(soci::session& session, TableType type)
209{
211 session << "SELECT COUNT(*) AS rows, "
212 "MIN(LedgerSeq) AS first, "
213 "MAX(LedgerSeq) AS last "
214 "FROM "
215 << toString(type) << ";",
216 soci::into(res.numberOfRows), soci::into(res.minLedgerSequence),
217 soci::into(res.maxLedgerSequence);
218
219 return res;
220}
221
222bool
224 DatabaseCon& ldgDB,
225 std::unique_ptr<DatabaseCon> const& txnDB,
226 Application& app,
227 std::shared_ptr<Ledger const> const& ledger,
228 bool current)
229{
230 auto const j = app.getJournal("Ledger");
231 auto seq = ledger->header().seq;
232
233 // TODO(tom): Fix this hard-coded SQL!
234 JLOG(j.trace()) << "saveValidatedLedger " << (current ? "" : "fromAcquire ") << seq;
235
236 if (!ledger->header().accountHash.isNonZero())
237 {
238 // LCOV_EXCL_START
239 JLOG(j.fatal()) << "AH is zero: " << getJson({*ledger, {}});
240 UNREACHABLE("xrpl::detail::saveValidatedLedger : zero account hash");
241 // LCOV_EXCL_STOP
242 }
243
244 if (ledger->header().accountHash != ledger->stateMap().getHash().asUInt256())
245 {
246 // LCOV_EXCL_START
247 JLOG(j.fatal()) << "sAL: " << ledger->header().accountHash
248 << " != " << ledger->stateMap().getHash();
249 JLOG(j.fatal()) << "saveAcceptedLedger: seq=" << seq << ", current=" << current;
250 UNREACHABLE("xrpl::detail::saveValidatedLedger : mismatched account hash");
251 // LCOV_EXCL_STOP
252 }
253
254 XRPL_ASSERT(
255 ledger->header().txHash == ledger->txMap().getHash().asUInt256(),
256 "xrpl::detail::saveValidatedLedger : transaction hash match");
257
258 // Save the ledger header in the hashed object store
259 {
260 Serializer s(128);
262 addRaw(ledger->header(), s);
263 app.getNodeStore().store(
264 NodeObjectType::Ledger, std::move(s.modData()), ledger->header().hash, seq);
265 }
266
268 try
269 {
270 aLedger = app.getAcceptedLedgerCache().fetch(ledger->header().hash);
271 if (!aLedger)
272 {
273 aLedger = std::make_shared<AcceptedLedger>(ledger);
274 app.getAcceptedLedgerCache().canonicalizeReplaceClient(ledger->header().hash, aLedger);
275 }
276 }
277 catch (std::exception const&)
278 {
279 JLOG(j.warn()) << "An accepted ledger was missing nodes";
280 app.getLedgerMaster().failedSave(seq, ledger->header().hash);
281 // Clients can now trust the database for information about this
282 // ledger sequence.
283 app.getPendingSaves().finishWork(seq);
284 return false;
285 }
286
287 {
288 static constexpr char const* kDeleteLedger = "DELETE FROM Ledgers WHERE LedgerSeq = {};";
289 static constexpr char const* kDeleteTranS1 =
290 "DELETE FROM Transactions WHERE LedgerSeq = {};";
291 static constexpr char const* kDeleteTranS2 =
292 "DELETE FROM AccountTransactions WHERE LedgerSeq = {};";
293 static constexpr char const* kDeleteAcctTrans =
294 "DELETE FROM AccountTransactions WHERE TransID = '{}';";
295
296 {
297 auto db = ldgDB.checkoutDb();
298 *db << std::format(kDeleteLedger, seq);
299 }
300
301 if (app.config().useTxTables())
302 {
303 if (!txnDB)
304 {
305 // LCOV_EXCL_START
306 JLOG(j.fatal()) << "TxTables db isn't available";
307 Throw<std::runtime_error>("TxTables db isn't available");
308 // LCOV_EXCL_STOP
309 }
310
311 auto db = txnDB->checkoutDb();
312
313 soci::transaction tr(*db);
314
315 *db << std::format(kDeleteTranS1, seq);
316 *db << std::format(kDeleteTranS2, seq);
317
318 std::string const ledgerSeq(std::to_string(seq));
319
320 for (auto const& acceptedLedgerTx : *aLedger)
321 {
322 UInt256 const transactionID = acceptedLedgerTx->getTransactionID();
323
324 std::string const txnId(to_string(transactionID));
325 std::string const txnSeq(std::to_string(acceptedLedgerTx->getTxnSeq()));
326
327 *db << std::format(kDeleteAcctTrans, txnId);
328
329 auto const& accts = acceptedLedgerTx->getAffected();
330
331 if (!accts.empty())
332 {
333 std::string sql(
334 "INSERT INTO AccountTransactions "
335 "(TransID, Account, LedgerSeq, TxnSeq) VALUES ");
336
337 // Try to make an educated guess on how much space we'll
338 // need for our arguments. In argument order we have: 64
339 // + 34 + 10 + 10 = 118 + 10 extra = 128 bytes
340 sql.reserve(sql.length() + (accts.size() * 128));
341
342 bool first = true;
343 for (auto const& account : accts)
344 {
345 if (!first)
346 {
347 sql += ", ('";
348 }
349 else
350 {
351 sql += "('";
352 first = false;
353 }
354
355 sql += txnId;
356 sql += "','";
357 sql += toBase58(account);
358 sql += "',";
359 sql += ledgerSeq;
360 sql += ',';
361 sql += txnSeq;
362 sql += ')';
363 }
364 sql += ';';
365 JLOG(j.trace()) << "ActTx: " << sql;
366 *db << sql;
367 }
368 else if (auto const& sleTxn = acceptedLedgerTx->getTxn(); !isPseudoTx(*sleTxn))
369 {
370 // It's okay for pseudo transactions to not affect any
371 // accounts. But otherwise...
372 JLOG(j.warn()) << "Transaction in ledger " << seq << " affects no accounts";
373 JLOG(j.warn()) << sleTxn->getJson(JsonOptions::Values::None);
374 }
375
376 *db
378 acceptedLedgerTx->getTxn()->getMetaSQL(
379 seq, acceptedLedgerTx->getEscMeta()) +
380 ";");
381
383 transactionID,
384 seq,
385 acceptedLedgerTx->getTxnSeq(),
387 }
388
389 tr.commit();
390 }
391
392 {
393 static std::string const kAddLedger(
394 R"sql(INSERT OR REPLACE INTO Ledgers
395 (LedgerHash,LedgerSeq,PrevHash,TotalCoins,ClosingTime,PrevClosingTime,
396 CloseTimeRes,CloseFlags,AccountSetHash,TransSetHash)
397 VALUES
398 (:ledgerHash,:ledgerSeq,:prevHash,:totalCoins,:closingTime,:prevClosingTime,
399 :closeTimeRes,:closeFlags,:accountSetHash,:transSetHash);)sql");
400
401 auto db(ldgDB.checkoutDb());
402
403 soci::transaction tr(*db);
404
405 auto const hash = to_string(ledger->header().hash);
406 auto const parentHash = to_string(ledger->header().parentHash);
407 auto const drops = to_string(ledger->header().drops);
408 auto const closeTime = ledger->header().closeTime.time_since_epoch().count();
409 auto const parentCloseTime =
410 ledger->header().parentCloseTime.time_since_epoch().count();
411 auto const closeTimeResolution = ledger->header().closeTimeResolution.count();
412 auto const closeFlags = ledger->header().closeFlags;
413 auto const accountHash = to_string(ledger->header().accountHash);
414 auto const txHash = to_string(ledger->header().txHash);
415
416 *db << kAddLedger, soci::use(hash), soci::use(seq), soci::use(parentHash),
417 soci::use(drops), soci::use(closeTime), soci::use(parentCloseTime),
418 soci::use(closeTimeResolution), soci::use(closeFlags), soci::use(accountHash),
419 soci::use(txHash);
420
421 tr.commit();
422 }
423 }
424
425 return true;
426}
427
432 * @param sqlSuffix SQL string used to specify the sought ledger.
433 * @param j Journal.
434 * @return Ledger info or no value if the ledger was not found.
435 */
437getLedgerInfo(soci::session& session, std::string const& sqlSuffix, beast::Journal j)
438{
439 // SOCI requires boost::optional (not std::optional) as parameters.
440 boost::optional<std::string> hash, parentHash, accountHash, txHash;
441 boost::optional<std::uint64_t> seq, drops, closeTime, parentCloseTime, closeTimeResolution,
442 closeFlags;
443
444 std::string const sql =
445 "SELECT "
446 "LedgerHash, PrevHash, AccountSetHash, TransSetHash, "
447 "TotalCoins,"
448 "ClosingTime, PrevClosingTime, CloseTimeRes, CloseFlags,"
449 "LedgerSeq FROM Ledgers " +
450 sqlSuffix + ";";
451
452 session << sql, soci::into(hash), soci::into(parentHash), soci::into(accountHash),
453 soci::into(txHash), soci::into(drops), soci::into(closeTime), soci::into(parentCloseTime),
454 soci::into(closeTimeResolution), soci::into(closeFlags), soci::into(seq);
455
456 if (!session.got_data())
457 {
458 JLOG(j.debug()) << "Ledger not found: " << sqlSuffix;
459 return {};
460 }
461
462 using time_point = NetClock::time_point;
463 using duration = NetClock::duration;
464
465 LedgerHeader info;
466
467 if (hash && !info.hash.parseHex(*hash))
468 {
469 JLOG(j.debug()) << "Hash parse error for ledger: " << sqlSuffix;
470 return {};
471 }
472
473 if (parentHash && !info.parentHash.parseHex(*parentHash))
474 {
475 JLOG(j.debug()) << "parentHash parse error for ledger: " << sqlSuffix;
476 return {};
477 }
478
479 if (accountHash && !info.accountHash.parseHex(*accountHash))
480 {
481 JLOG(j.debug()) << "accountHash parse error for ledger: " << sqlSuffix;
482 return {};
483 }
484
485 if (txHash && !info.txHash.parseHex(*txHash))
486 {
487 JLOG(j.debug()) << "txHash parse error for ledger: " << sqlSuffix;
488 return {};
489 }
490
491 info.seq = rangeCheckedCast<std::uint32_t>(seq.value_or(0));
492 info.drops = drops.value_or(0);
493 info.closeTime = time_point{duration{closeTime.value_or(0)}};
494 info.parentCloseTime = time_point{duration{parentCloseTime.value_or(0)}};
495 info.closeFlags = closeFlags.value_or(0);
496 info.closeTimeResolution = duration{closeTimeResolution.value_or(0)};
498 return info;
499}
500
502getLedgerInfoByIndex(soci::session& session, LedgerIndex ledgerSeq, beast::Journal j)
503{
505 s << "WHERE LedgerSeq = " << ledgerSeq;
506 return getLedgerInfo(session, s.str(), j);
507}
508
510getNewestLedgerInfo(soci::session& session, beast::Journal j)
511{
513 s << "ORDER BY LedgerSeq DESC LIMIT 1";
514 return getLedgerInfo(session, s.str(), j);
515}
516
518getLimitedOldestLedgerInfo(soci::session& session, LedgerIndex ledgerFirstIndex, beast::Journal j)
519{
521 s << "WHERE LedgerSeq >= " + std::to_string(ledgerFirstIndex) +
522 " ORDER BY LedgerSeq ASC LIMIT 1";
523 return getLedgerInfo(session, s.str(), j);
524}
525
527getLimitedNewestLedgerInfo(soci::session& session, LedgerIndex ledgerFirstIndex, beast::Journal j)
528{
530 s << "WHERE LedgerSeq >= " + std::to_string(ledgerFirstIndex) +
531 " ORDER BY LedgerSeq DESC LIMIT 1";
532 return getLedgerInfo(session, s.str(), j);
533}
534
536getLedgerInfoByHash(soci::session& session, UInt256 const& ledgerHash, beast::Journal j)
537{
539 s << "WHERE LedgerHash = '" << ledgerHash << "'";
540 return getLedgerInfo(session, s.str(), j);
541}
542
544getHashByIndex(soci::session& session, LedgerIndex ledgerIndex)
545{
546 UInt256 ret;
547
548 std::string sql = "SELECT LedgerHash FROM Ledgers INDEXED BY SeqLedger WHERE LedgerSeq='";
549 sql.append(std::to_string(ledgerIndex));
550 sql.append("';");
551
552 std::string hash;
553 {
554 // SOCI requires boost::optional (not std::optional) as the parameter.
555 boost::optional<std::string> lh;
556 session << sql, soci::into(lh);
557
558 if (!session.got_data() || !lh)
559 return ret;
560
561 hash = *lh;
562 if (hash.empty())
563 return ret;
564 }
565
566 if (!ret.parseHex(hash))
567 return ret;
569 return ret;
570}
571
573getHashesByIndex(soci::session& session, LedgerIndex ledgerIndex, beast::Journal j)
574{
575 // SOCI requires boost::optional (not std::optional) as the parameter.
576 boost::optional<std::string> lhO, phO;
577
578 session << "SELECT LedgerHash,PrevHash FROM Ledgers "
579 "INDEXED BY SeqLedger WHERE LedgerSeq = :ls;",
580 soci::into(lhO), soci::into(phO), soci::use(ledgerIndex);
581
582 if (!lhO || !phO)
583 {
584 auto const stream = j.trace();
585 JLOG(stream) << "Don't have ledger " << ledgerIndex;
586 return {};
587 }
588
589 LedgerHashPair hashes;
590 if (!hashes.ledgerHash.parseHex(*lhO) || !hashes.parentHash.parseHex(*phO))
591 {
592 auto const stream = j.trace();
593 JLOG(stream) << "Error parse hashes for ledger " << ledgerIndex;
594 return {};
595 }
597 return hashes;
598}
599
601getHashesByIndex(soci::session& session, LedgerIndex minSeq, LedgerIndex maxSeq, beast::Journal j)
602{
603 std::string sql = "SELECT LedgerSeq,LedgerHash,PrevHash FROM Ledgers WHERE LedgerSeq >= ";
604 sql.append(std::to_string(minSeq));
605 sql.append(" AND LedgerSeq <= ");
606 sql.append(std::to_string(maxSeq));
607 sql.append(";");
608
609 std::uint64_t ls = 0;
610 std::string lh;
611 // SOCI requires boost::optional (not std::optional) as the parameter.
612 boost::optional<std::string> ph;
613 soci::statement st = (session.prepare << sql, soci::into(ls), soci::into(lh), soci::into(ph));
614
615 st.execute();
617 while (st.fetch())
618 {
620 if (!hashes.ledgerHash.parseHex(lh))
621 {
622 JLOG(j.warn()) << "Error parsed hash for ledger seq: " << ls;
623 }
624 if (!ph)
625 {
626 JLOG(j.warn()) << "Null prev hash for ledger seq: " << ls;
627 }
628 else if (!hashes.parentHash.parseHex(*ph))
629 {
630 JLOG(j.warn()) << "Error parsed prev hash for ledger seq: " << ls;
631 }
633 return res;
634}
635
637getTxHistory(soci::session& session, Application& app, LedgerIndex startIndex, int quantity)
638{
639 std::string const sql = std::format(
640 "SELECT LedgerSeq, Status, RawTxn "
641 "FROM Transactions ORDER BY LedgerSeq DESC LIMIT {},{};",
642 startIndex,
643 quantity);
644
646 int total = 0;
647
648 {
649 // SOCI requires boost::optional (not std::optional) as parameters.
650 boost::optional<std::uint64_t> ledgerSeq;
651 boost::optional<std::string> status;
652 soci::blob sociRawTxnBlob(session);
653 soci::indicator rti = soci::i_null;
654 Blob rawTxn;
655
656 soci::statement st =
657 (session.prepare << sql,
658 soci::into(ledgerSeq),
659 soci::into(status),
660 soci::into(sociRawTxnBlob, rti));
661
662 st.execute();
663 while (st.fetch())
664 {
665 if (soci::i_ok == rti)
666 {
667 convert(sociRawTxnBlob, rawTxn);
668 }
669 else
670 {
671 rawTxn.clear();
672 }
673
674 if (auto const trans = Transaction::transactionFromSQL(ledgerSeq, status, rawTxn, app))
675 {
676 total++;
677 txs.push_back(trans);
678 }
679 }
680 }
681
682 return {txs, total};
683}
684
698 * selecting them.
699 * @param j Journal.
700 * @return SQL query string.
701 */
702static std::string
704 Application const& app,
705 std::string selection,
707 bool descending,
708 bool binary,
709 bool count,
711{
712 static constexpr std::uint32_t kNonbinaryPageLength = 200;
713 static constexpr std::uint32_t kBinaryPageLength = 500;
714
715 std::uint32_t numberOfResults = 0;
716
717 if (count)
718 {
719 numberOfResults = std::numeric_limits<std::uint32_t>::max();
720 }
721 else if (options.limit == UINT32_MAX)
722 {
723 numberOfResults = binary ? kBinaryPageLength : kNonbinaryPageLength;
724 }
725 else if (!options.bUnlimited)
726 {
727 numberOfResults =
728 std::min(binary ? kBinaryPageLength : kNonbinaryPageLength, options.limit);
729 }
730 else
731 {
732 numberOfResults = options.limit;
733 }
734
735 std::string maxClause;
736 std::string minClause;
737
738 if (options.ledgerRange.max != 0u)
739 {
740 maxClause =
741 std::format("AND AccountTransactions.LedgerSeq <= '{}'", options.ledgerRange.max);
742 }
743
744 if (options.ledgerRange.min != 0u)
745 {
746 minClause =
747 std::format("AND AccountTransactions.LedgerSeq >= '{}'", options.ledgerRange.min);
748 }
749
750 std::string sql;
751
752 if (count)
753 {
754 sql = std::format(
755 "SELECT {} FROM AccountTransactions "
756 "WHERE Account = '{}' {} {} LIMIT {}, {};",
757 selection,
758 toBase58(options.account),
759 maxClause,
760 minClause,
761 options.offset,
762 numberOfResults);
763 }
764 else
765 {
766 char const* const order = descending ? "DESC" : "ASC";
767 sql = std::format(
768 "SELECT {} FROM "
769 "AccountTransactions INNER JOIN Transactions "
770 "ON Transactions.TransID = AccountTransactions.TransID "
771 "WHERE Account = '{}' {} {} "
772 "ORDER BY AccountTransactions.LedgerSeq {}, "
773 "AccountTransactions.TxnSeq {}, AccountTransactions.TransID {} "
774 "LIMIT {}, {};",
775 selection,
776 toBase58(options.account),
777 maxClause,
778 minClause,
779 order,
780 order,
781 order,
782 options.offset,
783 numberOfResults);
784 }
785 JLOG(j.trace()) << "txSQL query: " << sql;
786 return sql;
787}
788
806 * -number represents the number of transactions skipped. We need to
807 * skip some number of transactions if option offset is > 0 in the
808 * options structure.
809 */
812 soci::session& session,
813 Application& app,
814 LedgerMaster& ledgerMaster,
816 bool descending,
818{
820
821 std::string const sql = transactionsSQL(
822 app,
823 "AccountTransactions.LedgerSeq,Status,RawTxn,TxnMeta",
824 options,
825 descending,
826 false,
827 false,
828 j);
829 if (sql.empty())
830 return {ret, 0};
831
832 int total = 0;
833 {
834 // SOCI requires boost::optional (not std::optional) as parameters.
835 boost::optional<std::uint64_t> ledgerSeq;
836 boost::optional<std::string> status;
837 soci::blob sociTxnBlob(session), sociTxnMetaBlob(session);
838 soci::indicator rti = soci::i_null, tmi = soci::i_null;
839 Blob rawTxn, txnMeta;
840
841 soci::statement st =
842 (session.prepare << sql,
843 soci::into(ledgerSeq),
844 soci::into(status),
845 soci::into(sociTxnBlob, rti),
846 soci::into(sociTxnMetaBlob, tmi));
847
848 st.execute();
849 while (st.fetch())
850 {
851 if (soci::i_ok == rti)
852 {
853 convert(sociTxnBlob, rawTxn);
854 }
855 else
856 {
857 rawTxn.clear();
858 }
859
860 if (soci::i_ok == tmi)
861 {
862 convert(sociTxnMetaBlob, txnMeta);
863 }
864 else
865 {
866 txnMeta.clear();
867 }
868
869 auto const txn = Transaction::transactionFromSQL(ledgerSeq, status, rawTxn, app);
870
871 if (txnMeta.empty())
872 { // Work around a bug that could leave the metadata missing
873 auto const seq = rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0));
874
875 JLOG(j.warn()) << "Recovering ledger " << seq << ", txn " << txn->getID();
876
877 if (auto const l = ledgerMaster.getLedgerBySeq(seq))
878 pendSaveValidated(app, l, false, false);
879 }
880
881 if (txn)
882 {
883 ret.emplace_back(
884 txn, std::make_shared<TxMeta>(txn->getID(), txn->getLedger(), txnMeta));
885 total++;
886 }
887 }
888 }
890 return {ret, total};
891}
892
895 soci::session& session,
896 Application& app,
897 LedgerMaster& ledgerMaster,
898 RelationalDatabase::AccountTxOptions const& options,
901 return getAccountTxs(session, app, ledgerMaster, options, false, j);
902}
903
906 soci::session& session,
907 Application& app,
908 LedgerMaster& ledgerMaster,
911{
912 return getAccountTxs(session, app, ledgerMaster, options, true, j);
913}
914
931 * transactions processed, if it is < 0, then -number represents the
932 * number of transactions skipped. We need to skip some number of
933 * transactions if option offset is > 0 in the options structure.
934 */
937 soci::session& session,
938 Application const& app,
940 bool descending,
942{
944
945 std::string const sql = transactionsSQL(
946 app,
947 "AccountTransactions.LedgerSeq,Status,RawTxn,TxnMeta",
948 options,
949 descending,
950 true /*binary*/,
951 false,
952 j);
953 if (sql.empty())
954 return {ret, 0};
955
956 int total = 0;
957
958 {
959 // SOCI requires boost::optional (not std::optional) as parameters.
960 boost::optional<std::uint64_t> ledgerSeq;
961 boost::optional<std::string> status;
962 soci::blob sociTxnBlob(session), sociTxnMetaBlob(session);
963 soci::indicator rti = soci::i_null, tmi = soci::i_null;
964
965 soci::statement st =
966 (session.prepare << sql,
967 soci::into(ledgerSeq),
968 soci::into(status),
969 soci::into(sociTxnBlob, rti),
970 soci::into(sociTxnMetaBlob, tmi));
971
972 st.execute();
973 while (st.fetch())
974 {
975 Blob rawTxn;
976 if (soci::i_ok == rti)
977 convert(sociTxnBlob, rawTxn);
978 Blob txnMeta;
979 if (soci::i_ok == tmi)
980 convert(sociTxnMetaBlob, txnMeta);
981
982 auto const seq = rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0));
983
984 ret.emplace_back(std::move(rawTxn), std::move(txnMeta), seq);
985 total++;
986 }
987 }
989 return {ret, total};
990}
991
994 soci::session& session,
995 Application const& app,
996 RelationalDatabase::AccountTxOptions const& options,
999 return getAccountTxsB(session, app, options, false, j);
1000}
1001
1004 soci::session& session,
1005 Application const& app,
1008{
1009 return getAccountTxsB(session, app, options, true, j);
1010}
1011
1018 * @param contextAccount The account passed to account_tx (the queried account).
1019 * @return True if the transaction passes the filter and should be included,
1020 * false if it should be skipped.
1021 */
1022static bool
1024 Blob const& rawData,
1025 DelegateFilter const& filter,
1026 AccountID const& contextAccount)
1027{
1028 SerialIter sit{makeSlice(rawData)};
1029 STTx const tx{sit};
1030
1031 AccountID const txOwner = tx.getAccountID(sfAccount);
1032
1033 if (!tx.isFieldPresent(sfDelegate))
1034 return false;
1035
1036 AccountID const txSigner = tx.getAccountID(sfDelegate);
1037
1038 switch (filter.type)
1039 {
1040 case DelegateType::Actor: {
1041 // Keep txns where the queried account (A) is the owner but
1042 // another account (C) was the delegatee that signed.
1043 bool const isDelegated = (txOwner == contextAccount) && (txSigner != contextAccount);
1044 if (!isDelegated)
1045 return false;
1046 return !filter.counterparty || (txSigner == *filter.counterparty);
1047 }
1048
1050 // Keep txns where the queried account (C) is the signer acting
1051 // on behalf of another account (A, the delegator/owner).
1052 bool const isActingAsDelegate =
1053 (txSigner == contextAccount) && (txOwner != contextAccount);
1054 if (!isActingAsDelegate)
1055 return false;
1056 return !filter.counterparty || (txOwner == *filter.counterparty);
1057 }
1058 }
1059
1060 return false; // LCOV_EXCL_LINE
1061}
1062
1078 * sequences sorted in the specified order by account sequence, a marker
1079 * for the next search if the search was not finished and the number of
1080 * transactions processed during this call.
1081 */
1084 soci::session& session,
1085 std::function<void(std::uint32_t)> const& onUnsavedLedger,
1086 std::function<void(std::uint32_t, std::string const&, Blob&&, Blob&&)> const& onTransaction,
1088 std::uint32_t pageLength,
1089 bool forward)
1090{
1091 int total = 0;
1092
1093 bool const hasDelegateFilter = options.delegate.has_value();
1094 bool lookingForMarker = options.marker.has_value();
1095
1096 std::uint32_t numberOfResults = 0;
1097
1098 if (options.limit == 0 || options.limit == UINT32_MAX ||
1099 (options.limit > pageLength && !options.bAdmin))
1100 {
1101 numberOfResults = pageLength;
1102 }
1103 else
1104 {
1105 numberOfResults = options.limit;
1106 }
1107
1108 // As an account can have many thousands of transactions, there is a limit
1109 // placed on the amount of transactions returned. If the limit is reached
1110 // before the result set has been exhausted (we always query for one more
1111 // than the limit), then we return an opaque marker that can be supplied in
1112 // a subsequent query.
1113 std::uint32_t queryLimit = numberOfResults + 1;
1114 std::uint32_t findLedger = 0, findSeq = 0;
1115
1116 if (lookingForMarker)
1117 {
1118 findLedger = options.marker->ledgerSeq;
1119 findSeq = options.marker->txnSeq;
1120 }
1121
1123
1124 std::string sql;
1125
1126 // SQL's BETWEEN uses a closed interval ([a,b])
1127
1128 char const* const order = forward ? "ASC" : "DESC";
1129
1130 if (findLedger == 0)
1131 {
1132 sql = std::format(
1133 R"(SELECT AccountTransactions.LedgerSeq,AccountTransactions.TxnSeq,
1134 Status,RawTxn,TxnMeta
1135 FROM AccountTransactions INNER JOIN Transactions
1136 ON Transactions.TransID = AccountTransactions.TransID
1137 AND AccountTransactions.Account = '{}' WHERE
1138 AccountTransactions.LedgerSeq BETWEEN {} AND {}
1139 ORDER BY AccountTransactions.LedgerSeq {},
1140 AccountTransactions.TxnSeq {}
1141 LIMIT {};)",
1142 toBase58(options.account),
1143 options.ledgerRange.min,
1144 options.ledgerRange.max,
1145 order,
1146 order,
1147 queryLimit);
1148 }
1149 else
1150 {
1151 char const* const compare = forward ? ">=" : "<=";
1152 std::uint32_t const minLedger = forward ? findLedger + 1 : options.ledgerRange.min;
1153 std::uint32_t const maxLedger = forward ? options.ledgerRange.max : findLedger - 1;
1154
1155 auto b58acct = toBase58(options.account);
1156 sql = std::format(
1157 R"(SELECT AccountTransactions.LedgerSeq,AccountTransactions.TxnSeq,
1158 Status,RawTxn,TxnMeta
1159 FROM AccountTransactions, Transactions WHERE
1160 (AccountTransactions.TransID = Transactions.TransID AND
1161 AccountTransactions.Account = '{}' AND
1162 AccountTransactions.LedgerSeq BETWEEN {} AND {})
1163 UNION
1164 SELECT AccountTransactions.LedgerSeq,AccountTransactions.TxnSeq,Status,RawTxn,TxnMeta
1165 FROM AccountTransactions, Transactions WHERE
1166 (AccountTransactions.TransID = Transactions.TransID AND
1167 AccountTransactions.Account = '{}' AND
1168 AccountTransactions.LedgerSeq = {} AND
1169 AccountTransactions.TxnSeq {} {})
1170 ORDER BY AccountTransactions.LedgerSeq {},
1171 AccountTransactions.TxnSeq {}
1172 LIMIT {};
1173 )",
1174 b58acct,
1175 minLedger,
1176 maxLedger,
1177 b58acct,
1178 findLedger,
1179 compare,
1180 findSeq,
1181 order,
1182 order,
1183 queryLimit);
1184 }
1185
1186 {
1187 Blob rawData;
1188 Blob rawMeta;
1189 // Delegate filtering happens after SQL, so skipped rows need their own
1190 // continuation marker accounting.
1191 std::uint32_t fetchedRows = 0;
1194
1195 // SOCI requires boost::optional (not std::optional) as parameters.
1196 boost::optional<std::uint64_t> ledgerSeq;
1197 boost::optional<std::uint32_t> txnSeq;
1198 boost::optional<std::string> status;
1199 soci::blob txnData(session);
1200 soci::blob txnMeta(session);
1201 soci::indicator dataPresent = soci::i_null, metaPresent = soci::i_null;
1202
1203 soci::statement st =
1204 (session.prepare << sql,
1205 soci::into(ledgerSeq),
1206 soci::into(txnSeq),
1207 soci::into(status),
1208 soci::into(txnData, dataPresent),
1209 soci::into(txnMeta, metaPresent));
1210
1211 st.execute();
1212
1213 while (st.fetch())
1214 {
1215 if (hasDelegateFilter)
1216 {
1217 ++fetchedRows;
1218 lastScanned = {
1219 .ledgerSeq = rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0)),
1220 .txnSeq = txnSeq.value_or(0),
1221 };
1222 }
1223
1224 if (lookingForMarker)
1225 {
1226 if (findLedger == ledgerSeq.value_or(0) && findSeq == txnSeq.value_or(0))
1227 {
1228 lookingForMarker = false;
1229 // Delegate markers are continuation cursors for the last
1230 // scanned row, so resume after the marker row.
1231 if (hasDelegateFilter)
1232 continue;
1233 }
1234 else
1235 {
1236 continue;
1237 }
1238 }
1239
1240 if (!hasDelegateFilter && numberOfResults == 0)
1241 {
1242 newmarker = {
1243 .ledgerSeq = rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0)),
1244 .txnSeq = txnSeq.value_or(0),
1245 };
1246 break;
1247 }
1248
1249 if (dataPresent == soci::i_ok)
1250 {
1251 convert(txnData, rawData);
1252 }
1253 else
1254 {
1255 rawData.clear();
1256 }
1257
1258 if (metaPresent == soci::i_ok)
1259 {
1260 convert(txnMeta, rawMeta);
1261 }
1262 else
1263 {
1264 rawMeta.clear();
1265 }
1266
1267 if (hasDelegateFilter)
1268 {
1269 if (rawData.empty() ||
1270 !passesDelegateFilter(rawData, options.delegate.value(), options.account))
1271 {
1272 rawData.clear();
1273 rawMeta.clear();
1274 continue;
1275 }
1276
1277 if (numberOfResults == 0)
1278 {
1279 newmarker = lastEmitted;
1280 break;
1281 }
1282 }
1283
1284 // Work around a bug that could leave the metadata missing
1285 if (rawMeta.empty())
1286 onUnsavedLedger(ledgerSeq.value_or(0));
1287
1288 // `rawData` and `rawMeta` will be used after they are moved.
1289 // That's OK.
1290 onTransaction(
1291 rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0)),
1292 *status,
1293 std::move(rawData),
1294 std::move(rawMeta));
1295 // Note some callbacks will move the data, some will not. Clear
1296 // them so code doesn't depend on if the data was actually moved
1297 // or not. The code will be more efficient if `rawData` and
1298 // `rawMeta` don't have to allocate in `convert`, so don't
1299 // refactor my moving these variables into loop scope.
1300 rawData.clear();
1301 rawMeta.clear();
1302
1303 --numberOfResults;
1304 total++;
1305 if (hasDelegateFilter)
1307 lastEmitted = {
1308 .ledgerSeq = rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0)),
1309 .txnSeq = txnSeq.value_or(0),
1310 };
1311 }
1312 }
1313
1314 // If this filtered page did not fill the requested number of results,
1315 // still return a marker so the caller can continue scanning later rows.
1316 if (hasDelegateFilter && !newmarker && !lookingForMarker && fetchedRows == queryLimit)
1317 newmarker = lastScanned;
1318 }
1319
1320 return {newmarker, total};
1321}
1322
1323std::pair<std::optional<RelationalDatabase::AccountTxMarker>, int>
1325 soci::session& session,
1326 std::function<void(std::uint32_t)> const& onUnsavedLedger,
1327 std::function<void(std::uint32_t, std::string const&, Blob&&, Blob&&)> const& onTransaction,
1328 RelationalDatabase::AccountTxPageOptions const& options,
1329 std::uint32_t pageLength)
1330{
1331 return accountTxPage(session, onUnsavedLedger, onTransaction, options, pageLength, true);
1332}
1333
1334std::pair<std::optional<RelationalDatabase::AccountTxMarker>, int>
1336 soci::session& session,
1337 std::function<void(std::uint32_t)> const& onUnsavedLedger,
1338 std::function<void(std::uint32_t, std::string const&, Blob&&, Blob&&)> const& onTransaction,
1339 RelationalDatabase::AccountTxPageOptions const& options,
1340 std::uint32_t pageLength)
1341{
1342 return accountTxPage(session, onUnsavedLedger, onTransaction, options, pageLength, false);
1343}
1344
1345std::variant<RelationalDatabase::AccountTx, TxSearched>
1347 soci::session& session,
1348 Application& app,
1349 UInt256 const& id,
1350 std::optional<ClosedInterval<uint32_t>> const& range,
1351 ErrorCodeI& ec)
1352{
1353 std::string sql =
1354 "SELECT LedgerSeq,Status,RawTxn,TxnMeta "
1355 "FROM Transactions WHERE TransID='";
1356
1357 sql.append(to_string(id));
1358 sql.append("';");
1359
1360 // SOCI requires boost::optional (not std::optional) as parameters.
1361 boost::optional<std::uint64_t> ledgerSeq;
1362 boost::optional<std::string> status;
1363 Blob rawTxn, rawMeta;
1364 {
1365 soci::blob sociRawTxnBlob(session), sociRawMetaBlob(session);
1366 soci::indicator txn = soci::i_null, meta = soci::i_null;
1367
1368 session << sql, soci::into(ledgerSeq), soci::into(status), soci::into(sociRawTxnBlob, txn),
1369 soci::into(sociRawMetaBlob, meta);
1370
1371 auto const gotData = session.got_data();
1372
1373 if ((!gotData || txn != soci::i_ok || meta != soci::i_ok) && !range)
1374 return TxSearched::Unknown;
1375
1376 if (!gotData)
1377 {
1378 uint64_t count = 0;
1379 soci::indicator rti = soci::i_null;
1380
1381 session << "SELECT COUNT(DISTINCT LedgerSeq) FROM Transactions WHERE "
1382 "LedgerSeq BETWEEN "
1383 << range->first() << " AND " << range->last() << ";",
1384 soci::into(count, rti);
1385
1386 if (!session.got_data() || rti != soci::i_ok)
1387 return TxSearched::Some;
1388
1389 return count == (range->last() - range->first() + 1) ? TxSearched::All
1391 }
1392
1393 convert(sociRawTxnBlob, rawTxn);
1394 convert(sociRawMetaBlob, rawMeta);
1395 }
1396
1397 try
1398 {
1399 auto txn = Transaction::transactionFromSQL(ledgerSeq, status, rawTxn, app);
1400
1401 if (!ledgerSeq)
1402 return std::pair{std::move(txn), nullptr};
1403
1404 auto const inLedger = rangeCheckedCast<std::uint32_t>(ledgerSeq.value());
1405
1406 auto txMeta = std::make_shared<TxMeta>(id, inLedger, rawMeta);
1407
1408 return std::pair{std::move(txn), std::move(txMeta)};
1409 }
1410 catch (std::exception& e)
1411 {
1412 JLOG(app.getJournal("Ledger").warn())
1413 << "Unable to deserialize transaction from raw SQL value. Error: " << e.what();
1414
1416 }
1417
1418 return TxSearched::Unknown;
1419}
1420
1421bool
1422dbHasSpace(soci::session& session, Config const& config, beast::Journal j)
1423{
1424 std::filesystem::space_info const space =
1426
1427 if (space.available < megabytes(512))
1428 {
1429 JLOG(j.fatal()) << "Remaining free disk space is less than 512MB";
1430 return false;
1431 }
1432
1433 if (config.useTxTables())
1434 {
1435 DatabaseCon::Setup const dbSetup = setupDatabaseCon(config);
1436 std::filesystem::path const dbPath = dbSetup.dataDir / kTxDbName;
1437 std::error_code ec;
1438 std::optional<std::uint64_t> dbSize = std::filesystem::file_size(dbPath, ec);
1439 if (ec)
1440 {
1441 JLOG(j.error()) << "Error checking transaction db file size: " << ec.message();
1442 dbSize.reset();
1443 }
1444
1445 static auto const kPageSize = [&] {
1446 std::uint32_t ps = 0;
1447 session << "PRAGMA page_size;", soci::into(ps);
1448 return ps;
1449 }();
1450 static auto const kMaxPages = [&] {
1451 std::uint32_t mp = 0;
1452 session << "PRAGMA max_page_count;", soci::into(mp);
1453 return mp;
1454 }();
1455 std::uint32_t pageCount = 0;
1456 session << "PRAGMA page_count;", soci::into(pageCount);
1457 std::uint32_t const freePages = kMaxPages - pageCount;
1458 std::uint64_t const freeSpace = safeCast<std::uint64_t>(freePages) * kPageSize;
1459 JLOG(j.info()) << "Transaction DB pathname: " << dbPath.string()
1460 << "; file size: " << dbSize.value_or(-1) << " bytes"
1461 << "; SQLite page size: " << kPageSize << " bytes"
1462 << "; Free pages: " << freePages << "; Free space: " << freeSpace
1463 << " bytes; "
1464 << "Note that this does not take into account available disk "
1465 "space.";
1466
1467 if (freeSpace < megabytes(512))
1468 {
1469 JLOG(j.fatal()) << "Free SQLite space for transaction db is less than "
1470 "512MB. To fix this, xrpld must be executed with the "
1471 "vacuum parameter before restarting. "
1472 "Note that this activity can take multiple days, "
1473 "depending on database size.";
1474 return false;
1475 }
1476 }
1477
1478 return true;
1479}
1480
1481} // namespace xrpl::detail
T append(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
Stream fatal() const
Definition Journal.h:368
Stream error() const
Definition Journal.h:362
Stream debug() const
Definition Journal.h:344
Stream info() const
Definition Journal.h:350
Stream trace() const
Severity stream access functions.
Definition Journal.h:338
Stream warn() const
Definition Journal.h:356
virtual Config & config()=0
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:526
bool useTxTables() const
int getValueFor(SizedItem item, std::optional< std::size_t > node=std::nullopt) const
Retrieve the default value for the item at the specified node size.
LockedSociSession checkoutDb()
Tracks the current ledger and any ledgers in the process of closing.
std::shared_ptr< Ledger const > getLedgerBySeq(std::uint32_t index)
Finds a ledger by sequence, preferring the validated chain.
void failedSave(std::uint32_t seq, UInt256 const &hash)
Marks a ledger no longer resident after its save failed, and starts fetching it again.
std::chrono::time_point< NetClock > time_point
Definition chrono.h:48
std::chrono::duration< rep, period > duration
Definition chrono.h:47
virtual std::uint32_t getNetworkID() const noexcept=0
Get the configured network ID.
void finishWork(LedgerIndex seq)
Finish working on a ledger.
std::vector< AccountTx > AccountTxs
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:643
static std::string const & getMetaSQLInsertReplaceHeader()
Definition STTx.cpp:388
virtual PendingSaves & getPendingSaves()=0
virtual beast::Journal getJournal(std::string const &name)=0
virtual TransactionMaster & getMasterTransaction()=0
virtual NetworkIDService & getNetworkIDService()=0
virtual node_store::Database & getNodeStore()=0
virtual TaggedCache< UInt256, AcceptedLedger > & getAcceptedLedgerCache()=0
virtual LedgerMaster & getLedgerMaster()=0
bool inLedger(UInt256 const &hash, std::uint32_t ledger, std::optional< uint32_t > tseq, std::optional< uint32_t > netID)
static Transaction::pointer transactionFromSQL(boost::optional< std::uint64_t > const &ledgerSeq, boost::optional< std::string > const &status, Blob const &rawTxn, Application &app)
virtual void store(NodeObjectType type, Blob &&data, UInt256 const &hash, std::uint32_t ledgerSeq)=0
Store the object.
T clear(T... args)
T count(T... args)
T emplace_back(T... args)
T empty(T... args)
T file_size(T... args)
T format(T... args)
T make_shared(T... args)
T make_unique(T... args)
T max(T... args)
T message(T... args)
T min(T... args)
static std::pair< std::optional< RelationalDatabase::AccountTxMarker >, int > accountTxPage(soci::session &session, std::function< void(std::uint32_t)> const &onUnsavedLedger, std::function< void(std::uint32_t, std::string const &, Blob &&, Blob &&)> const &onTransaction, RelationalDatabase::AccountTxPageOptions const &options, std::uint32_t pageLength, bool forward)
accountTxPage Searches for the oldest or newest transactions for the account that matches the given c...
Definition Node.cpp:1078
std::pair< std::optional< RelationalDatabase::AccountTxMarker >, int > newestAccountTxPage(soci::session &session, std::function< void(std::uint32_t)> const &onUnsavedLedger, std::function< void(std::uint32_t, std::string const &, Blob &&, Blob &&)> const &onTransaction, RelationalDatabase::AccountTxPageOptions const &options, std::uint32_t pageLength)
newestAccountTxPage Searches newest transactions for given account which match given criteria startin...
Definition Node.cpp:1306
constexpr int kTableTypeCount
Definition Node.h:36
bool saveValidatedLedger(DatabaseCon &ldgDB, std::unique_ptr< DatabaseCon > const &txnDB, Application &app, std::shared_ptr< Ledger const > const &ledger, bool current)
saveValidatedLedger Saves ledger into database.
Definition Node.cpp:223
std::pair< std::optional< RelationalDatabase::AccountTxMarker >, int > oldestAccountTxPage(soci::session &session, std::function< void(std::uint32_t)> const &onUnsavedLedger, std::function< void(std::uint32_t, std::string const &, Blob &&, Blob &&)> const &onTransaction, RelationalDatabase::AccountTxPageOptions const &options, std::uint32_t pageLength)
oldestAccountTxPage Searches oldest transactions for given account which match given criteria startin...
Definition Node.cpp:1295
void deleteBeforeLedgerSeq(soci::session &session, TableType type, LedgerIndex ledgerSeq)
deleteBeforeLedgerSeq Deletes all entries in given table for the ledgers with given sequence and all ...
Definition Node.cpp:190
std::optional< LedgerIndex > getMinLedgerSeq(soci::session &session, TableType type)
getMinLedgerSeq Returns minimum ledger sequence in given table.
Definition Node.cpp:164
std::optional< LedgerIndex > getMaxLedgerSeq(soci::session &session, TableType type)
getMaxLedgerSeq Returns maximum ledger sequence in given table.
Definition Node.cpp:174
std::pair< RelationalDatabase::AccountTxs, int > getNewestAccountTxs(soci::session &session, Application &app, LedgerMaster &ledgerMaster, RelationalDatabase::AccountTxOptions const &options, beast::Journal j)
getNewestAccountTxs Returns newest transactions for given account which match given criteria starting...
Definition Node.cpp:900
std::pair< std::vector< std::shared_ptr< Transaction > >, int > getTxHistory(soci::session &session, Application &app, LedgerIndex startIndex, int quantity)
getTxHistory Returns given number of most recent transactions starting from given number of entry.
Definition Node.cpp:632
std::optional< LedgerHeader > getNewestLedgerInfo(soci::session &session, beast::Journal j)
getNewestLedgerInfo Returns info of newest saved ledger.
Definition Node.cpp:505
std::optional< LedgerHeader > getLimitedOldestLedgerInfo(soci::session &session, LedgerIndex ledgerFirstIndex, beast::Journal j)
getLimitedOldestLedgerInfo Returns info of oldest ledger from ledgers with sequences greater or equal...
Definition Node.cpp:513
DatabasePairValid makeLedgerDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup, beast::Journal j)
makeLedgerDBs Opens ledger and transactions databases.
Definition Node.cpp:103
std::variant< RelationalDatabase::AccountTx, TxSearched > getTransaction(soci::session &session, Application &app, UInt256 const &id, std::optional< ClosedInterval< uint32_t > > const &range, ErrorCodeI &ec)
getTransaction Returns transaction with given hash.
Definition Node.cpp:1317
RelationalDatabase::CountMinMax getRowsMinMax(soci::session &session, TableType type)
getRowsMinMax Returns minimum ledger sequence, maximum ledger sequence and total number of rows in gi...
Definition Node.cpp:208
static std::pair< RelationalDatabase::AccountTxs, int > getAccountTxs(soci::session &session, Application &app, LedgerMaster &ledgerMaster, RelationalDatabase::AccountTxOptions const &options, bool descending, beast::Journal j)
getAccountTxs Returns the oldest or newest transactions for the account that matches the given criter...
Definition Node.cpp:806
static std::optional< LedgerHeader > getLedgerInfo(soci::session &session, std::string const &sqlSuffix, beast::Journal j)
getLedgerInfo Returns the info of the ledger retrieved from the database by using the provided SQL qu...
Definition Node.cpp:432
std::optional< LedgerHeader > getLedgerInfoByHash(soci::session &session, UInt256 const &ledgerHash, beast::Journal j)
getLedgerInfoByHash Returns info of ledger with given hash.
Definition Node.cpp:531
static bool passesDelegateFilter(Blob const &rawData, DelegateFilter const &filter, AccountID const &contextAccount)
Determines whether a transaction should be included in account_tx results based on a delegation filte...
Definition Node.cpp:1018
std::size_t getRows(soci::session &session, TableType type)
getRows Returns number of rows in given table.
Definition Node.cpp:196
std::optional< LedgerHeader > getLedgerInfoByIndex(soci::session &session, LedgerIndex ledgerSeq, beast::Journal j)
getLedgerInfoByIndex Returns ledger by its sequence.
Definition Node.cpp:497
void deleteByLedgerSeq(soci::session &session, TableType type, LedgerIndex ledgerSeq)
deleteByLedgerSeq Deletes all entries in given table for the ledger with given sequence.
Definition Node.cpp:184
static std::pair< std::vector< RelationalDatabase::TxnMetaLedgerType >, int > getAccountTxsB(soci::session &session, Application const &app, RelationalDatabase::AccountTxOptions const &options, bool descending, beast::Journal j)
getAccountTxsB Returns the oldest or newest transactions in binary form for the account that matches ...
Definition Node.cpp:931
UInt256 getHashByIndex(soci::session &session, LedgerIndex ledgerIndex)
getHashByIndex Returns hash of ledger with given sequence.
Definition Node.cpp:539
std::pair< RelationalDatabase::AccountTxs, int > getOldestAccountTxs(soci::session &session, Application &app, LedgerMaster &ledgerMaster, RelationalDatabase::AccountTxOptions const &options, beast::Journal j)
getOldestAccountTxs Returns oldest transactions for given account which match given criteria starting...
Definition Node.cpp:889
bool dbHasSpace(soci::session &session, Config const &config, beast::Journal j)
dbHasSpace Checks if given database has available space.
Definition Node.cpp:1393
std::optional< LedgerHeader > getLimitedNewestLedgerInfo(soci::session &session, LedgerIndex ledgerFirstIndex, beast::Journal j)
getLimitedNewestLedgerInfo Returns info of newest ledger from ledgers with sequences greater or equal...
Definition Node.cpp:522
static std::string transactionsSQL(Application const &app, std::string selection, RelationalDatabase::AccountTxOptions const &options, bool descending, bool binary, bool count, beast::Journal j)
transactionsSQL Returns a SQL query for selecting the oldest or newest transactions in decoded or bin...
Definition Node.cpp:698
std::pair< std::vector< RelationalDatabase::TxnMetaLedgerType >, int > getNewestAccountTxsB(soci::session &session, Application const &app, RelationalDatabase::AccountTxOptions const &options, beast::Journal j)
getNewestAccountTxsB Returns newest transactions in binary form for given account which match given c...
Definition Node.cpp:998
std::pair< std::vector< RelationalDatabase::TxnMetaLedgerType >, int > getOldestAccountTxsB(soci::session &session, Application const &app, RelationalDatabase::AccountTxOptions const &options, beast::Journal j)
getOldestAccountTxsB Returns oldest transactions in binary form for given account which match given c...
Definition Node.cpp:988
static std::string toString(TableType type)
to_string Returns the name of a table according to its TableType.
Definition Node.cpp:82
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...
Definition Node.cpp:568
constexpr std::array< char const *, 5 > kLgrDbInit
Definition DBInit.h:48
ErrorCodeI
Definition ErrorCodes.h:23
@ RpcDbDeserialization
Definition ErrorCodes.h:117
bool pendSaveValidated(ServiceRegistry &registry, std::shared_ptr< Ledger const > const &ledger, bool isSynchronous, bool isCurrent)
Save, or arrange to save, a fully-validated ledger.
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:409
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition RangeSet.h:37
constexpr std::array< char const *, 8 > kTxDbInit
Definition DBInit.h:78
T rangeCheckedCast(C c)
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
constexpr Dest safeCast(Src s) noexcept
Definition safe_cast.h:21
constexpr auto kLgrDbName
Definition DBInit.h:46
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
@ Authorizer
This account signed and submitted transactions on behalf of another account (this account is the sign...
@ Actor
Another account signed and submitted transactions on behalf of this account (this account is the owne...
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
BaseUInt< 256 > UInt256
Definition base_uint.h:581
constexpr auto megabytes(T value) noexcept
void addRaw(LedgerHeader const &, Serializer &, bool includeHash=false)
json::Value getJson(LedgerFill const &fill)
Return a new json::Value representing the ledger with given options.
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition RangeSet.h:27
constexpr auto kilobytes(T value) noexcept
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
@ LedgerMaster
ledger master data for signing
Definition HashPrefix.h:59
DatabaseCon::Setup setupDatabaseCon(Config const &c, std::optional< beast::Journal > j=std::nullopt)
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
constexpr auto kTxDbName
Definition DBInit.h:76
bool isPseudoTx(STObject const &tx)
Check whether a transaction is a pseudo-transaction.
Definition STTx.cpp:889
void convert(soci::blob &from, std::vector< std::uint8_t > &to)
Definition SociDB.cpp:146
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T push_back(T... args)
T reserve(T... args)
T reset(T... args)
T length(T... args)
T str(T... args)
std::array< std::string, 4 > txPragma
Definition DatabaseCon.h:98
std::array< std::string, 1 > lgrPragma
Definition DatabaseCon.h:99
std::optional< AccountID > counterparty
Information about the notional ledger backing the view.
NetClock::time_point parentCloseTime
NetClock::duration closeTimeResolution
NetClock::time_point closeTime
LedgerRange ledgerRange
Ledger sequence range to search.
static constexpr auto kDatabasePath
Definition Constants.h:13
T to_string(T... args)
T value_or(T... args)
T what(T... args)