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);
89 aLedger = std::make_shared<AcceptedLedger>(ledger, app);
99 <<
"An accepted ledger was missing nodes";
108 soci::transaction tr(txnMetaSession);
110 for (
auto const& [_, acceptedLedgerTx] : aLedger->getMap())
115 R
"sql(INSERT OR REPLACE INTO TransactionMeta VALUES
116 (:transactionID,:shardIndex);)sql";
119 to_string(acceptedLedgerTx->getTransactionID());
122 soci::use(shardIndex);
135 session <<
"SELECT ShardIndex FROM LedgerMeta WHERE LedgerHash = '" << hash
137 soci::into(shardIndex);
139 if (!session.got_data())
149 session <<
"SELECT ShardIndex FROM TransactionMeta WHERE TransID = '" <<
id
151 soci::into(shardIndex);
153 if (!session.got_data())
164 auto tx{std::make_unique<DatabaseCon>(
166 tx->getSession() << boost::str(
167 boost::format(
"PRAGMA cache_size=-%d;") %
170 auto lgr{std::make_unique<DatabaseCon>(
172 lgr->getSession() << boost::str(
173 boost::format(
"PRAGMA cache_size=-%d;") %
176 return {std::move(lgr), std::move(tx)};
186 auto tx{std::make_unique<DatabaseCon>(
188 tx->getSession() << boost::str(
189 boost::format(
"PRAGMA cache_size=-%d;") %
193 auto lgr{std::make_unique<DatabaseCon>(
195 lgr->getSession() << boost::str(
196 boost::format(
"PRAGMA cache_size=-%d;") %
199 return {std::move(lgr), std::move(tx)};
204 soci::session& txsession,
205 soci::session& lgrsession,
211 auto const ledgerSeq{ledger->
info().
seq};
215 auto& session{txsession};
216 soci::transaction tr(session);
218 session <<
"DELETE FROM Transactions "
219 "WHERE LedgerSeq = :seq;",
220 soci::use(ledgerSeq);
221 session <<
"DELETE FROM AccountTransactions "
222 "WHERE LedgerSeq = :seq;",
223 soci::use(ledgerSeq);
231 <<
"shard " << index <<
" has an invalid transaction map"
232 <<
" on sequence " << sSeq;
236 for (
auto const& item : ledger->
txs)
241 auto const txID{item.first->getTransactionID()};
243 auto const txMeta{std::make_shared<TxMeta>(
244 txID, ledger->
seq(), *item.second)};
246 session <<
"DELETE FROM AccountTransactions "
247 "WHERE TransID = :txID;",
250 auto const& accounts = txMeta->getAffectedAccounts(j);
251 if (!accounts.empty())
254 auto const s{boost::str(
255 boost::format(
"('%s','%s',%s,%s)") % sTxID %
"%s" %
258 sql.
reserve((accounts.size() + 1) * 128);
260 "INSERT INTO AccountTransactions "
261 "(TransID, Account, LedgerSeq, TxnSeq) VALUES ";
262 sql += boost::algorithm::join(
264 boost::adaptors::transformed(
275 <<
"shard " << index <<
" account transaction: " << sql;
282 <<
"shard " << index <<
" transaction in ledger "
283 << sSeq <<
" affects no accounts";
290 item.first->getMetaSQL(
303 auto& session{lgrsession};
304 soci::transaction tr(session);
311 session <<
"DELETE FROM Ledgers "
312 "WHERE LedgerSeq = :seq;",
313 soci::use(ledgerSeq);
314 session <<
"INSERT OR REPLACE INTO Ledgers ("
315 "LedgerHash, LedgerSeq, PrevHash, TotalCoins, ClosingTime,"
316 "PrevClosingTime, CloseTimeRes, CloseFlags, AccountSetHash,"
319 ":ledgerHash, :ledgerSeq, :prevHash, :totalCoins,"
320 ":closingTime, :prevClosingTime, :closeTimeRes,"
321 ":closeFlags, :accountSetHash, :transSetHash);",
322 soci::use(sHash), soci::use(ledgerSeq), soci::use(sParentHash),
344 return std::make_unique<DatabaseCon>(
355 session <<
"INSERT INTO Shard (ShardIndex) "
356 "VALUES (:shardIndex);",
365 boost::optional<std::uint32_t> resIndex;
366 soci::blob sociBlob(session);
367 soci::indicator blobPresent;
369 session <<
"SELECT ShardIndex, StoredLedgerSeqs "
371 "WHERE ShardIndex = :index;",
372 soci::into(resIndex), soci::into(sociBlob, blobPresent),
375 if (!resIndex || index != resIndex)
378 if (blobPresent != soci::i_ok)
392 boost::optional<std::uint32_t> resIndex;
393 boost::optional<std::string> sHash0;
394 soci::blob sociBlob(session);
395 soci::indicator blobPresent;
397 session <<
"SELECT ShardIndex, LastLedgerHash, StoredLedgerSeqs "
399 "WHERE ShardIndex = :index;",
400 soci::into(resIndex), soci::into(sHash0),
401 soci::into(sociBlob, blobPresent), soci::use(index);
406 if (!resIndex || index != resIndex)
407 return {
false, {{}, {}}};
409 if (blobPresent != soci::i_ok)
410 return {
true, {{}, sHash}};
415 return {
true, {s, sHash}};
420 soci::session& session,
426 soci::blob sociBlob(session);
432 if (ledger->
info().
seq == lastSeq)
435 session <<
"UPDATE Shard "
436 "SET LastLedgerHash = :lastLedgerHash,"
437 "StoredLedgerSeqs = :storedLedgerSeqs "
438 "WHERE ShardIndex = :shardIndex;",
439 soci::use(sHash), soci::use(sociBlob), soci::use(index);
443 session <<
"UPDATE Shard "
444 "SET StoredLedgerSeqs = :storedLedgerSeqs "
445 "WHERE ShardIndex = :shardIndex;",
446 soci::use(sociBlob), soci::use(index);
455 return std::make_unique<DatabaseCon>(
464 soci::rowset<soci::row> rs =
465 (db.
getSession().prepare <<
"SELECT * FROM State;");
467 for (
auto it = rs.begin(); it != rs.end(); ++it)
479 db.
getSession() <<
"INSERT INTO State VALUES (:index, :url);",
480 soci::use(shardIndex), soci::use(url);
486 db.
getSession() <<
"DELETE FROM State WHERE ShardIndex = :index;",
487 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...