rippled
app/rdb/backend/detail/impl/Shard.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/ledger/AcceptedLedger.h>
21 #include <ripple/app/ledger/LedgerMaster.h>
22 #include <ripple/app/rdb/backend/detail/Shard.h>
23 #include <ripple/basics/StringUtilities.h>
24 #include <ripple/core/DatabaseCon.h>
25 #include <ripple/core/SociDB.h>
26 #include <ripple/json/to_string.h>
27 
28 namespace ripple {
29 namespace detail {
30 
31 DatabasePair
33  Config const& config,
34  DatabaseCon::Setup const& setup,
35  DatabaseCon::CheckpointerSetup const& checkpointerSetup)
36 {
37  // ledger meta database
38  auto lgrMetaDB{std::make_unique<DatabaseCon>(
39  setup,
43  checkpointerSetup)};
44 
45  if (!config.useTxTables())
46  return {std::move(lgrMetaDB), nullptr};
47 
48  // transaction meta database
49  auto txMetaDB{std::make_unique<DatabaseCon>(
50  setup, TxMetaDBName, TxMetaDBPragma, TxMetaDBInit, checkpointerSetup)};
51 
52  return {std::move(lgrMetaDB), std::move(txMetaDB)};
53 }
54 
55 bool
57  std::shared_ptr<Ledger const> const& ledger,
58  Application& app,
59  soci::session& lgrMetaSession,
60  soci::session& txnMetaSession,
61  std::uint32_t const shardIndex)
62 {
63  std::string_view constexpr lgrSQL =
64  R"sql(INSERT OR REPLACE INTO LedgerMeta VALUES
65  (:ledgerHash,:shardIndex);)sql";
66 
67  auto const hash = to_string(ledger->info().hash);
68  lgrMetaSession << lgrSQL, soci::use(hash), soci::use(shardIndex);
69 
70  if (!app.config().useTxTables())
71  return true;
72 
73  auto const aLedger = [&app, ledger]() -> std::shared_ptr<AcceptedLedger> {
74  try
75  {
76  auto aLedger =
77  app.getAcceptedLedgerCache().fetch(ledger->info().hash);
78  if (!aLedger)
79  {
80  aLedger = std::make_shared<AcceptedLedger>(ledger, app);
81  app.getAcceptedLedgerCache().canonicalize_replace_client(
82  ledger->info().hash, aLedger);
83  }
84 
85  return aLedger;
86  }
87  catch (std::exception const&)
88  {
89  JLOG(app.journal("Ledger").warn())
90  << "An accepted ledger was missing nodes";
91  }
92 
93  return {};
94  }();
95 
96  if (!aLedger)
97  return false;
98 
99  soci::transaction tr(txnMetaSession);
100 
101  for (auto const& acceptedLedgerTx : *aLedger)
102  {
103  std::string_view constexpr txnSQL =
104  R"sql(INSERT OR REPLACE INTO TransactionMeta VALUES
105  (:transactionID,:shardIndex);)sql";
106 
107  auto const transactionID =
108  to_string(acceptedLedgerTx->getTransactionID());
109 
110  txnMetaSession << txnSQL, soci::use(transactionID),
111  soci::use(shardIndex);
112  }
113 
114  tr.commit();
115  return true;
116 }
117 
119 getShardIndexforLedger(soci::session& session, LedgerHash const& hash)
120 {
121  std::uint32_t shardIndex;
122  session << "SELECT ShardIndex FROM LedgerMeta WHERE LedgerHash = '" << hash
123  << "';",
124  soci::into(shardIndex);
125 
126  if (!session.got_data())
127  return std::nullopt;
128 
129  return shardIndex;
130 }
131 
133 getShardIndexforTransaction(soci::session& session, TxID const& id)
134 {
135  std::uint32_t shardIndex;
136  session << "SELECT ShardIndex FROM TransactionMeta WHERE TransID = '" << id
137  << "';",
138  soci::into(shardIndex);
139 
140  if (!session.got_data())
141  return std::nullopt;
142 
143  return shardIndex;
144 }
145 
146 } // namespace detail
147 } // namespace ripple
ripple::Application
Definition: Application.h:115
ripple::detail::getShardIndexforTransaction
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 ...
Definition: app/rdb/backend/detail/impl/Shard.cpp:131
ripple::Application::getAcceptedLedgerCache
virtual TaggedCache< uint256, AcceptedLedger > & getAcceptedLedgerCache()=0
std::shared_ptr
STL class.
std::exception
STL class.
ripple::DatabaseCon::Setup
Definition: DatabaseCon.h:84
ripple::detail::saveLedgerMeta
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...
Definition: app/rdb/backend/detail/impl/Shard.cpp:56
ripple::LgrMetaDBInit
constexpr std::array< char const *, 3 > LgrMetaDBInit
Definition: DBInit.h:149
std::string_view
STL class.
ripple::detail::to_string
static std::string to_string(TableType type)
to_string Returns the name of a table according to its TableType.
Definition: Node.cpp:46
ripple::detail::makeMetaDBs
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...
Definition: app/rdb/backend/detail/impl/Shard.cpp:32
ripple::DatabaseCon::CheckpointerSetup
Definition: DatabaseCon.h:107
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
ripple::LgrMetaDBName
constexpr auto LgrMetaDBName
Definition: DBInit.h:128
ripple::base_uint< 256 >
ripple::TxMetaDBName
constexpr auto TxMetaDBName
Definition: DBInit.h:162
ripple::Config
Definition: Config.h:89
ripple::Application::config
virtual Config & config()=0
ripple::Config::useTxTables
bool useTxTables() const
Definition: Config.h:343
ripple::TxMetaDBInit
constexpr std::array< char const *, 3 > TxMetaDBInit
Definition: DBInit.h:183
ripple::LgrMetaDBPragma
constexpr std::array< char const *, 4 > LgrMetaDBPragma
Definition: DBInit.h:133
ripple::HashPrefix::transactionID
@ transactionID
transaction plus signature to give transaction ID
std::uint32_t
ripple::TxMetaDBPragma
constexpr std::array< char const *, 4 > TxMetaDBPragma
Definition: DBInit.h:167
ripple::detail::getShardIndexforLedger
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...
Definition: app/rdb/backend/detail/impl/Shard.cpp:117
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Application::journal
virtual beast::Journal journal(std::string const &name)=0
std::optional< std::uint32_t >