rippled
RelationalDBInterfacePostgres.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/ledger/LedgerToJson.h>
23 #include <ripple/app/ledger/TransactionMaster.h>
24 #include <ripple/app/main/Application.h>
25 #include <ripple/app/misc/Manifest.h>
26 #include <ripple/app/misc/impl/AccountTxPaging.h>
27 #include <ripple/app/rdb/RelationalDBInterface_nodes.h>
28 #include <ripple/app/rdb/RelationalDBInterface_postgres.h>
29 #include <ripple/app/rdb/backend/RelationalDBInterfacePostgres.h>
30 #include <ripple/basics/BasicConfig.h>
31 #include <ripple/basics/StringUtilities.h>
32 #include <ripple/core/DatabaseCon.h>
33 #include <ripple/core/Pg.h>
34 #include <ripple/core/SociDB.h>
35 #include <ripple/json/to_string.h>
36 #include <ripple/nodestore/DatabaseShard.h>
37 #include <boost/algorithm/string.hpp>
38 #include <boost/range/adaptor/transformed.hpp>
39 #include <soci/sqlite3/soci-sqlite3.h>
40 
41 namespace ripple {
42 
44 {
45 public:
47  Application& app,
48  Config const& config,
49  JobQueue& jobQueue)
50  : app_(app)
51  , j_(app_.journal("PgPool"))
52  , pgPool_(
53 #ifdef RIPPLED_REPORTING
54  make_PgPool(
55  config.section("ledger_tx_tables"),
56  *dynamic_cast<Stoppable*>(&app_),
57  j_)
58 #endif
59  )
60  {
61  assert(config.reporting());
62 #ifdef RIPPLED_REPORTING
63  if (config.reporting() && !config.reportingReadOnly()) // use pg
64  {
65  initSchema(pgPool_);
66  }
67 #endif
68  }
69 
70  void
71  sweep() override;
72 
74  getMinLedgerSeq() override;
75 
77  getMaxLedgerSeq() override;
78 
80  getCompleteLedgers() override;
81 
83  getValidatedLedgerAge() override;
84 
85  bool
87  LedgerInfo const& info,
88  std::vector<AccountTransactionsData> const& accountTxData) override;
89 
91  getLedgerInfoByIndex(LedgerIndex ledgerSeq) override;
92 
94  getNewestLedgerInfo() override;
95 
97  getLedgerInfoByHash(uint256 const& ledgerHash) override;
98 
99  uint256
100  getHashByIndex(LedgerIndex ledgerIndex) override;
101 
103  getHashesByIndex(LedgerIndex ledgerIndex) override;
104 
106  getHashesByIndex(LedgerIndex minSeq, LedgerIndex maxSeq) override;
107 
109  getTxHashes(LedgerIndex seq) override;
110 
112  getTxHistory(LedgerIndex startIndex) override;
113 
115  getAccountTx(AccountTxArgs const& args) override;
116 
118  locateTransaction(uint256 const& id) override;
119 
120  bool
121  ledgerDbHasSpace(Config const& config) override;
122 
123  bool
124  transactionDbHasSpace(Config const& config) override;
125 
126 private:
130 
131  bool
132  dbHasSpace(Config const& config);
133 };
134 
135 void
137 {
138 #ifdef RIPPLED_REPORTING
139  pgPool_->idleSweeper();
140 #endif
141 }
142 
145 {
147 }
148 
151 {
153 }
154 
157 {
159 }
160 
163 {
165 }
166 
167 bool
169  LedgerInfo const& info,
170  std::vector<AccountTransactionsData> const& accountTxData)
171 {
172  return ripple::writeLedgerAndTransactions(pgPool_, info, accountTxData, j_);
173 }
174 
177 {
178  return ripple::getLedgerInfoByIndex(pgPool_, ledgerSeq, app_);
179 }
180 
183 {
185 }
186 
189 {
190  return ripple::getLedgerInfoByHash(pgPool_, ledgerHash, app_);
191 }
192 
193 uint256
195 {
196  return ripple::getHashByIndex(pgPool_, ledgerIndex, app_);
197 }
198 
201 {
202  LedgerHashPair p;
204  pgPool_, ledgerIndex, p.ledgerHash, p.parentHash, app_))
205  return {};
206  return p;
207 }
208 
211  LedgerIndex minSeq,
212  LedgerIndex maxSeq)
213 {
214  return ripple::getHashesByIndex(pgPool_, minSeq, maxSeq, app_);
215 }
216 
219 {
220  return ripple::getTxHashes(pgPool_, seq, app_);
221 }
222 
225 {
226  return ripple::getTxHistory(pgPool_, startIndex, app_, j_);
227 }
228 
231 {
232  return ripple::getAccountTx(pgPool_, args, app_, j_);
233 }
234 
237 {
239 }
240 
241 bool
243 {
244  /* Postgres server could be running on a different machine. */
245 
246  return true;
247 }
248 
249 bool
251 {
252  return dbHasSpace(config);
253 }
254 
255 bool
257 {
258  return dbHasSpace(config);
259 }
260 
263  Application& app,
264  Config const& config,
265  JobQueue& jobQueue)
266 {
267  return std::make_unique<RelationalDBInterfacePostgresImp>(
268  app, config, jobQueue);
269 }
270 
271 } // namespace ripple
ripple::RelationalDBInterfacePostgresImp::locateTransaction
Transaction::Locator locateTransaction(uint256 const &id) override
locateTransaction Returns information used to locate a transaction.
Definition: RelationalDBInterfacePostgres.cpp:236
ripple::getLedgerInfoByIndex
std::optional< LedgerInfo > getLedgerInfoByIndex(soci::session &session, LedgerIndex ledgerSeq, beast::Journal j)
getLedgerInfoByIndex Returns ledger by its sequence.
Definition: RelationalDBInterface_nodes.cpp:453
ripple::Application
Definition: Application.h:102
ripple::RelationalDBInterfacePostgresImp::ledgerDbHasSpace
bool ledgerDbHasSpace(Config const &config) override
ledgerDbHasSpace Checks if ledger database has available space.
Definition: RelationalDBInterfacePostgres.cpp:250
ripple::RelationalDBInterfacePostgresImp::dbHasSpace
bool dbHasSpace(Config const &config)
Definition: RelationalDBInterfacePostgres.cpp:242
ripple::RelationalDBInterface::AccountTxArgs
Definition: RelationalDBInterface.h:96
ripple::getAccountTx
std::pair< AccountTxResult, RPC::Status > getAccountTx(std::shared_ptr< PgPool > const &pgPool, AccountTxArgs const &args, Application &app, beast::Journal j)
getAccountTx Get last account transactions specifies by passed argumenrs structure.
Definition: RelationalDBInterface_postgres.cpp:542
std::string
STL class.
std::shared_ptr< PgPool >
ripple::getTxHistory
std::pair< std::vector< std::shared_ptr< Transaction > >, int > getTxHistory(soci::session &session, Application &app, LedgerIndex startIndex, int quantity, bool count)
getTxHistory Returns given number of most recent transactions starting from given number of entry.
Definition: RelationalDBInterface_nodes.cpp:613
ripple::getHashByIndex
uint256 getHashByIndex(soci::session &session, LedgerIndex ledgerIndex)
getHashByIndex Returns hash of ledger with given sequence.
Definition: RelationalDBInterface_nodes.cpp:507
ripple::RelationalDBInterfacePostgresImp::getNewestLedgerInfo
std::optional< LedgerInfo > getNewestLedgerInfo() override
getNewestLedgerInfo Returns info of newest saved ledger.
Definition: RelationalDBInterfacePostgres.cpp:182
std::pair
ripple::RelationalDBInterfacePostgresImp::RelationalDBInterfacePostgresImp
RelationalDBInterfacePostgresImp(Application &app, Config const &config, JobQueue &jobQueue)
Definition: RelationalDBInterfacePostgres.cpp:46
std::vector
STL class.
std::chrono::seconds
ripple::getLedgerInfoByHash
std::optional< LedgerInfo > getLedgerInfoByHash(soci::session &session, uint256 const &ledgerHash, beast::Journal j)
getLedgerInfoByHash Returns info of ledger with given hash.
Definition: RelationalDBInterface_nodes.cpp:496
ripple::locateTransaction
Transaction::Locator locateTransaction(std::shared_ptr< PgPool > const &pgPool, uint256 const &id, Application &app)
locateTransaction Returns information used to locate a transaction.
Definition: RelationalDBInterface_postgres.cpp:657
ripple::RelationalDBInterfacePostgresImp::getAccountTx
std::pair< AccountTxResult, RPC::Status > getAccountTx(AccountTxArgs const &args) override
getAccountTx Get last account transactions specifies by passed argumenrs structure.
Definition: RelationalDBInterfacePostgres.cpp:230
ripple::RelationalDBInterfacePostgres
Definition: RelationalDBInterfacePostgres.h:27
ripple::getRelationalDBInterfacePostgres
std::unique_ptr< RelationalDBInterface > getRelationalDBInterfacePostgres(Application &app, Config const &config, JobQueue &jobQueue)
Definition: RelationalDBInterfacePostgres.cpp:262
ripple::getCompleteLedgers
std::string getCompleteLedgers(std::shared_ptr< PgPool > const &pgPool)
getCompleteLedgers Returns string which contains list of completed ledgers
Definition: RelationalDBInterface_postgres.cpp:61
ripple::RelationalDBInterfacePostgresImp::getTxHistory
std::vector< std::shared_ptr< Transaction > > getTxHistory(LedgerIndex startIndex) override
getTxHistory Returns most recent 20 transactions starting from given number or entry.
Definition: RelationalDBInterfacePostgres.cpp:224
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:74
ripple::writeLedgerAndTransactions
bool writeLedgerAndTransactions(std::shared_ptr< PgPool > const &pgPool, LedgerInfo const &info, std::vector< AccountTransactionsData > const &accountTxData, beast::Journal &j)
writeLedgerAndTransactions Write new ledger and transaction data to Postgres.
Definition: RelationalDBInterface_postgres.cpp:762
ripple::getValidatedLedgerAge
std::chrono::seconds getValidatedLedgerAge(std::shared_ptr< PgPool > const &pgPool, beast::Journal j)
getValidatedLedgerAge Returns age of last validated ledger
Definition: RelationalDBInterface_postgres.cpp:72
ripple::Config::reporting
bool reporting() const
Definition: Config.h:270
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:201
ripple::LedgerHashPair::ledgerHash
uint256 ledgerHash
Definition: RelationalDBInterface.h:38
ripple::Transaction::Locator
Definition: Transaction.h:316
ripple::Config
Definition: Config.h:67
ripple::LedgerHashPair
Definition: RelationalDBInterface.h:36
ripple::RelationalDBInterfacePostgresImp
Definition: RelationalDBInterfacePostgres.cpp:43
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
std::map
STL class.
ripple::RelationalDBInterfacePostgresImp::getLedgerInfoByHash
std::optional< LedgerInfo > getLedgerInfoByHash(uint256 const &ledgerHash) override
getLedgerInfoByHash Returns info of ledger with given hash.
Definition: RelationalDBInterfacePostgres.cpp:188
ripple::RelationalDBInterfacePostgresImp::getTxHashes
std::vector< uint256 > getTxHashes(LedgerIndex seq) override
getTxHashes Returns vector of tx hashes by given ledger sequence.
Definition: RelationalDBInterfacePostgres.cpp:218
ripple::RelationalDBInterfacePostgresImp::getMaxLedgerSeq
std::optional< LedgerIndex > getMaxLedgerSeq() override
getMaxLedgerSeq Returns maximum ledger sequence in Ledgers table.
Definition: RelationalDBInterfacePostgres.cpp:150
ripple::JobQueue
A pool of threads to perform work.
Definition: JobQueue.h:55
ripple::RelationalDBInterfacePostgresImp::getValidatedLedgerAge
std::chrono::seconds getValidatedLedgerAge() override
getValidatedLedgerAge Returns age of last validated ledger.
Definition: RelationalDBInterfacePostgres.cpp:162
ripple::RelationalDBInterfacePostgresImp::j_
beast::Journal j_
Definition: RelationalDBInterfacePostgres.cpp:128
ripple::RelationalDBInterfacePostgresImp::getCompleteLedgers
std::string getCompleteLedgers() override
getCompleteLedgers Returns string which contains list of completed ledgers.
Definition: RelationalDBInterfacePostgres.cpp:156
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RelationalDBInterfacePostgresImp::sweep
void sweep() override
sweep Sweep the database.
Definition: RelationalDBInterfacePostgres.cpp:136
ripple::RelationalDBInterfacePostgresImp::getLedgerInfoByIndex
std::optional< LedgerInfo > getLedgerInfoByIndex(LedgerIndex ledgerSeq) override
getLedgerInfoByIndex Returns ledger by its sequence.
Definition: RelationalDBInterfacePostgres.cpp:176
ripple::getHashesByIndex
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: RelationalDBInterface_nodes.cpp:537
ripple::RelationalDBInterfacePostgresImp::transactionDbHasSpace
bool transactionDbHasSpace(Config const &config) override
transactionDbHasSpace Checks if transaction database has available space.
Definition: RelationalDBInterfacePostgres.cpp:256
ripple::LedgerHashPair::parentHash
uint256 parentHash
Definition: RelationalDBInterface.h:39
std::optional
ripple::LedgerInfo
Information about the notional ledger backing the view.
Definition: ReadView.h:84
ripple::Config::reportingReadOnly
bool reportingReadOnly() const
Definition: Config.h:282
ripple::getMinLedgerSeq
std::optional< LedgerIndex > getMinLedgerSeq(soci::session &session, TableType type)
getMinLedgerSeq Returns minimum ledger sequence in given table.
Definition: RelationalDBInterface_nodes.cpp:121
std::unique_ptr
STL class.
ripple::RelationalDBInterfacePostgresImp::getHashesByIndex
std::optional< LedgerHashPair > getHashesByIndex(LedgerIndex ledgerIndex) override
getHashesByIndex Returns hash of the ledger and hash of parent ledger for the ledger of given sequenc...
Definition: RelationalDBInterfacePostgres.cpp:200
ripple::RelationalDBInterfacePostgresImp::app_
Application & app_
Definition: RelationalDBInterfacePostgres.cpp:127
ripple::getNewestLedgerInfo
std::optional< LedgerInfo > getNewestLedgerInfo(soci::session &session, beast::Journal j)
getNewestLedgerInfo Returns info of newest saved ledger.
Definition: RelationalDBInterface_nodes.cpp:464
ripple::RelationalDBInterfacePostgresImp::writeLedgerAndTransactions
bool writeLedgerAndTransactions(LedgerInfo const &info, std::vector< AccountTransactionsData > const &accountTxData) override
writeLedgerAndTransactions Write new ledger and transaction data into database.
Definition: RelationalDBInterfacePostgres.cpp:168
ripple::getMaxLedgerSeq
std::optional< LedgerIndex > getMaxLedgerSeq(soci::session &session, TableType type)
getMaxLedgerSeq Returns maximum ledger sequence in given table.
Definition: RelationalDBInterface_nodes.cpp:131
ripple::RelationalDBInterfacePostgresImp::getHashByIndex
uint256 getHashByIndex(LedgerIndex ledgerIndex) override
getHashByIndex Returns hash of ledger with given sequence.
Definition: RelationalDBInterfacePostgres.cpp:194
ripple::RelationalDBInterfacePostgresImp::pgPool_
std::shared_ptr< PgPool > pgPool_
Definition: RelationalDBInterfacePostgres.cpp:129
ripple::RelationalDBInterfacePostgresImp::getMinLedgerSeq
std::optional< LedgerIndex > getMinLedgerSeq() override
getMinLedgerSeq Returns minimum ledger sequence in Ledgers table.
Definition: RelationalDBInterfacePostgres.cpp:144
ripple::getTxHashes
std::vector< uint256 > getTxHashes(std::shared_ptr< PgPool > const &pgPool, LedgerIndex seq, Application &app)
getTxHashes Returns vector of tx hashes by given ledger sequence
Definition: RelationalDBInterface_postgres.cpp:330