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/rdb/RelationalDBInterface.h>
24 #include <ripple/app/rdb/RelationalDBInterface_shards.h>
25 #include <ripple/basics/BasicConfig.h>
26 #include <ripple/basics/StringUtilities.h>
27 #include <ripple/core/DatabaseCon.h>
28 #include <ripple/core/SociDB.h>
29 #include <ripple/json/to_string.h>
30 #include <boost/algorithm/string.hpp>
31 #include <boost/range/adaptor/transformed.hpp>
42 auto lgrMetaDB{std::make_unique<DatabaseCon>(
52 auto txMetaDB{std::make_unique<DatabaseCon>(
59 return {std::move(lgrMetaDB), std::move(txMetaDB)};
62 return {std::move(lgrMetaDB),
nullptr};
69 soci::session& lgrMetaSession,
70 soci::session& txnMetaSession,
74 R
"sql(INSERT OR REPLACE INTO LedgerMeta VALUES
75 (:ledgerHash,:shardIndex);)sql";
78 lgrMetaSession << lgrSQL, soci::use(hash), soci::use(shardIndex);
82 auto const aLedger = [&app,
90 aLedger = std::make_shared<AcceptedLedger>(ledger, app);
100 <<
"An accepted ledger was missing nodes";
109 soci::transaction tr(txnMetaSession);
111 for (
auto const& acceptedLedgerTx : *aLedger)
114 R
"sql(INSERT OR REPLACE INTO TransactionMeta VALUES
115 (:transactionID,:shardIndex);)sql";
118 to_string(acceptedLedgerTx->getTransactionID());
121 soci::use(shardIndex);
134 session <<
"SELECT ShardIndex FROM LedgerMeta WHERE LedgerHash = '" << hash
136 soci::into(shardIndex);
138 if (!session.got_data())
148 session <<
"SELECT ShardIndex FROM TransactionMeta WHERE TransID = '" <<
id
150 soci::into(shardIndex);
152 if (!session.got_data())
163 auto tx{std::make_unique<DatabaseCon>(
165 tx->getSession() << boost::str(
166 boost::format(
"PRAGMA cache_size=-%d;") %
169 auto lgr{std::make_unique<DatabaseCon>(
171 lgr->getSession() << boost::str(
172 boost::format(
"PRAGMA cache_size=-%d;") %
175 return {std::move(lgr), std::move(tx)};
185 auto tx{std::make_unique<DatabaseCon>(
187 tx->getSession() << boost::str(
188 boost::format(
"PRAGMA cache_size=-%d;") %
192 auto lgr{std::make_unique<DatabaseCon>(
194 lgr->getSession() << boost::str(
195 boost::format(
"PRAGMA cache_size=-%d;") %
198 return {std::move(lgr), std::move(tx)};
203 soci::session& txsession,
204 soci::session& lgrsession,
210 auto const ledgerSeq{ledger->
info().
seq};
214 auto& session{txsession};
215 soci::transaction tr(session);
217 session <<
"DELETE FROM Transactions "
218 "WHERE LedgerSeq = :seq;",
219 soci::use(ledgerSeq);
220 session <<
"DELETE FROM AccountTransactions "
221 "WHERE LedgerSeq = :seq;",
222 soci::use(ledgerSeq);
230 <<
"shard " << index <<
" has an invalid transaction map"
231 <<
" on sequence " << sSeq;
235 for (
auto const& item : ledger->
txs)
240 auto const txID{item.first->getTransactionID()};
242 auto const txMeta{std::make_shared<TxMeta>(
243 txID, ledger->
seq(), *item.second)};
245 session <<
"DELETE FROM AccountTransactions "
246 "WHERE TransID = :txID;",
249 auto const& accounts = txMeta->getAffectedAccounts();
250 if (!accounts.empty())
253 auto const s{boost::str(
254 boost::format(
"('%s','%s',%s,%s)") % sTxID %
"%s" %
257 sql.
reserve((accounts.size() + 1) * 128);
259 "INSERT INTO AccountTransactions "
260 "(TransID, Account, LedgerSeq, TxnSeq) VALUES ";
261 sql += boost::algorithm::join(
263 boost::adaptors::transformed(
274 <<
"shard " << index <<
" account transaction: " << sql;
281 <<
"shard " << index <<
" transaction in ledger "
282 << sSeq <<
" affects no accounts";
289 item.first->getMetaSQL(
302 auto& session{lgrsession};
303 soci::transaction tr(session);
310 session <<
"DELETE FROM Ledgers "
311 "WHERE LedgerSeq = :seq;",
312 soci::use(ledgerSeq);
313 session <<
"INSERT OR REPLACE INTO Ledgers ("
314 "LedgerHash, LedgerSeq, PrevHash, TotalCoins, ClosingTime,"
315 "PrevClosingTime, CloseTimeRes, CloseFlags, AccountSetHash,"
318 ":ledgerHash, :ledgerSeq, :prevHash, :totalCoins,"
319 ":closingTime, :prevClosingTime, :closeTimeRes,"
320 ":closeFlags, :accountSetHash, :transSetHash);",
321 soci::use(sHash), soci::use(ledgerSeq), soci::use(sParentHash),
343 return std::make_unique<DatabaseCon>(
354 session <<
"INSERT INTO Shard (ShardIndex) "
355 "VALUES (:shardIndex);",
364 boost::optional<std::uint32_t> resIndex;
365 soci::blob sociBlob(session);
366 soci::indicator blobPresent;
368 session <<
"SELECT ShardIndex, StoredLedgerSeqs "
370 "WHERE ShardIndex = :index;",
371 soci::into(resIndex), soci::into(sociBlob, blobPresent),
374 if (!resIndex || index != resIndex)
377 if (blobPresent != soci::i_ok)
391 boost::optional<std::uint32_t> resIndex;
392 boost::optional<std::string> sHash0;
393 soci::blob sociBlob(session);
394 soci::indicator blobPresent;
396 session <<
"SELECT ShardIndex, LastLedgerHash, StoredLedgerSeqs "
398 "WHERE ShardIndex = :index;",
399 soci::into(resIndex), soci::into(sHash0),
400 soci::into(sociBlob, blobPresent), soci::use(index);
405 if (!resIndex || index != resIndex)
406 return {
false, {{}, {}}};
408 if (blobPresent != soci::i_ok)
409 return {
true, {{}, sHash}};
414 return {
true, {s, sHash}};
419 soci::session& session,
425 soci::blob sociBlob(session);
431 if (ledger->
info().
seq == lastSeq)
434 session <<
"UPDATE Shard "
435 "SET LastLedgerHash = :lastLedgerHash,"
436 "StoredLedgerSeqs = :storedLedgerSeqs "
437 "WHERE ShardIndex = :shardIndex;",
438 soci::use(sHash), soci::use(sociBlob), soci::use(index);
442 session <<
"UPDATE Shard "
443 "SET StoredLedgerSeqs = :storedLedgerSeqs "
444 "WHERE ShardIndex = :shardIndex;",
445 soci::use(sociBlob), soci::use(index);
454 return std::make_unique<DatabaseCon>(
463 soci::rowset<soci::row> rs =
464 (db.
getSession().prepare <<
"SELECT * FROM State;");
466 for (
auto it = rs.begin(); it != rs.end(); ++it)
478 db.
getSession() <<
"INSERT INTO State VALUES (:index, :url);",
479 soci::use(shardIndex), soci::use(url);
485 db.
getSession() <<
"DELETE FROM State WHERE ShardIndex = :index;",
486 soci::use(shardIndex);
constexpr std::array< char const *, 1 > AcquireShardDBPragma
constexpr auto AcquireShardDBName
std::unique_ptr< DatabaseCon > makeAcquireDB(DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeAcquireDB Opens shard acquire DB and returns its descriptor.
virtual TaggedCache< uint256, AcceptedLedger > & getAcceptedLedgerCache()=0
Stream trace() const
Severity stream access functions.
constexpr std::array< char const *, 3 > LgrMetaDBInit
void convert(soci::blob &from, std::vector< std::uint8_t > &to)
std::pair< bool, AcquireShardSeqsHash > selectAcquireDBLedgerSeqsHash(soci::session &session, std::uint32_t index)
selectAcquireDBLedgerSeqsHash Returns set of acquired ledgers and hash for given shard.
void insertAcquireDBIndex(soci::session &session, std::uint32_t index)
insertAcquireDBIndex Adds new shard index to shard acquire DB.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
constexpr auto kilobytes(T value) noexcept
constexpr std::array< char const *, 1 > AcquireShardDBInit
static constexpr std::array< char const *, 3 > ShardArchiveHandlerDBInit
static std::string const & getMetaSQLInsertReplaceHeader()
constexpr std::array< char const *, 5 > LgrDBInit
constexpr std::array TxMetaDBPragma
constexpr auto LgrMetaDBName
NetClock::time_point closeTime
static constexpr std::array< char const *, 2 > DownloaderDBPragma
void updateAcquireDB(soci::session &session, std::shared_ptr< Ledger const > const &ledger, std::uint32_t index, std::uint32_t lastSeq, std::optional< std::string > const &seqs)
updateAcquireDB Updates information in acquire DB.
LedgerInfo const & info() const override
Returns information about the ledger.
T time_since_epoch(T... args)
bool isPseudoTx(STObject const &tx)
Check whether a transaction is a pseudo-transaction.
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.
constexpr auto TxMetaDBName
constexpr std::array< char const *, 1 > LgrDBPragma
void deleteFromArchiveDB(DatabaseCon &db, std::uint32_t shardIndex)
deleteFromArchiveDB Deletes entry from shard archive DB.
virtual Config & config()=0
void dropArchiveDB(DatabaseCon &db)
dropArchiveDB Removes table in shard archive DB.
constexpr std::array< char const *, 3 > TxMetaDBInit
@ transactionID
transaction plus signature to give transaction ID
A generic endpoint for log messages.
constexpr std::array< char const *, 2 > FinalShardDBPragma
soci::session & getSession()
SHAMap const & txMap() const
bool updateLedgerDBs(soci::session &txsession, soci::session &lgrsession, std::shared_ptr< Ledger const > const &ledger, std::uint32_t index, std::atomic< bool > &stop, beast::Journal j)
updateLedgerDBs Save given ledger to shard databases.
constexpr std::array LgrMetaDBPragma
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
virtual beast::Journal journal(std::string const &name)=0
void readArchiveDB(DatabaseCon &db, std::function< void(std::string const &, int)> const &func)
readArchiveDB Read entries from shard archive database and calls fiven callback for each entry.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
NetClock::duration closeTimeResolution
DatabasePair makeMetaDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeMetaDBs Opens ledger and transaction 'meta' databases which map ledger hashes and transaction IDs...
constexpr std::array TxDBPragma
bool saveLedgerMeta(std::shared_ptr< Ledger const > const &ledger, Application &app, soci::session &lgrMetaSession, soci::session &txnMetaSession, std::uint32_t const shardIndex)
saveLedgerMeta Stores (transaction ID -> shard index) and (ledger hash -> shard index) mappings in th...
DatabasePair makeShardIncompleteLedgerDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeShardIncompleteLedgerDBs Opens shard databases for not fully downloaded or verified shard and ret...
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
void insertArchiveDB(DatabaseCon &db, std::uint32_t shardIndex, std::string const &url)
insertArchiveDB Adds entry to shard archive database.
std::pair< bool, std::optional< std::string > > selectAcquireDBLedgerSeqs(soci::session &session, std::uint32_t index)
selectAcquireDBLedgerSeqs Returns set of acquired ledgers for given shard.
constexpr std::array< char const *, 8 > TxDBInit
std::unique_ptr< DatabaseCon > makeArchiveDB(boost::filesystem::path const &dir, std::string const &dbName)
makeArchiveDB Opens shard archive DB and returns its descriptor.
std::optional< std::uint32_t > getShardIndexforTransaction(soci::session &session, TxID const &id)
getShardIndexforTransaction Queries the transaction meta database to retrieve the index of the shard ...
std::string sqlBlobLiteral(Blob const &blob)
Format arbitrary binary data as an SQLite "blob literal".
DatabasePair makeShardCompleteLedgerDBs(Config const &config, DatabaseCon::Setup const &setup)
makeShardCompleteLedgerDBs Opens shard databases for already verified shard and returns its descripto...
NetClock::time_point parentCloseTime
std::optional< std::uint32_t > getShardIndexforLedger(soci::session &session, LedgerHash const &hash)
getShardIndexforLedger Queries the ledger meta database to retrieve the index of the shard that conta...