rippled
RelationalDatabase.h
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 #ifndef RIPPLE_APP_RDB_RELATIONALDATABASE_H_INCLUDED
21 #define RIPPLE_APP_RDB_RELATIONALDATABASE_H_INCLUDED
22 
23 #include <ripple/app/ledger/Ledger.h>
24 #include <ripple/app/main/Application.h>
25 #include <ripple/app/misc/Transaction.h>
26 #include <ripple/core/Config.h>
27 #include <ripple/core/DatabaseCon.h>
28 #include <ripple/peerfinder/impl/Store.h>
29 #include <ripple/rpc/impl/RPCHelpers.h>
30 #include <boost/filesystem.hpp>
31 #include <boost/optional.hpp>
32 #include <boost/variant.hpp>
33 
34 namespace ripple {
35 
37 {
40 };
41 
43 {
44  uint32_t min;
45  uint32_t max;
46 };
47 
49 {
50 public:
51  struct CountMinMax
52  {
56  };
57 
59  {
62  };
63 
65  {
71  bool bUnlimited;
72  };
73 
75  {
81  bool bAdmin;
82  };
83 
84  using AccountTx =
89 
90  using LedgerSequence = uint32_t;
93  using LedgerSpecifier =
95 
97  {
100  bool binary = false;
101  bool forward = false;
102  uint32_t limit = 0;
104  };
105 
107  {
110  uint32_t limit;
112  };
113 
117  {
118  boost::container::flat_set<AccountID> accounts;
119  uint32_t ledgerSequence;
123 
125  TxMeta const& meta,
126  uint256 const& nodestoreHash,
127  beast::Journal j)
128  : accounts(meta.getAffectedAccounts())
129  , ledgerSequence(meta.getLgrSeq())
130  , transactionIndex(meta.getIndex())
131  , txHash(meta.getTxID())
133  {
134  }
135  };
136 
146  init(Application& app, Config const& config, JobQueue& jobQueue);
147 
148  virtual ~RelationalDatabase() = default;
149 
156  getMinLedgerSeq() = 0;
157 
164  getMaxLedgerSeq() = 0;
165 
172  getLedgerInfoByIndex(LedgerIndex ledgerSeq) = 0;
173 
179  getNewestLedgerInfo() = 0;
180 
188  getLedgerInfoByHash(uint256 const& ledgerHash) = 0;
189 
196  virtual uint256
197  getHashByIndex(LedgerIndex ledgerIndex) = 0;
198 
207  getHashesByIndex(LedgerIndex ledgerIndex) = 0;
208 
219  getHashesByIndex(LedgerIndex minSeq, LedgerIndex maxSeq) = 0;
220 
229  getTxHistory(LedgerIndex startIndex) = 0;
230 
237  virtual bool
238  ledgerDbHasSpace(Config const& config) = 0;
239 
246  virtual bool
247  transactionDbHasSpace(Config const& config) = 0;
248 };
249 
250 template <class T, class C>
251 T
253 {
254  if ((c > std::numeric_limits<T>::max()) ||
255  (!std::numeric_limits<T>::is_signed && c < 0) ||
259  {
260  /* This should never happen */
261  assert(0);
262  JLOG(debugLog().error())
263  << "rangeCheckedCast domain error:"
264  << " value = " << c << " min = " << std::numeric_limits<T>::lowest()
265  << " max: " << std::numeric_limits<T>::max();
266  }
267 
268  return static_cast<T>(c);
269 }
270 
271 } // namespace ripple
272 
273 #endif
ripple::RelationalDatabase::AccountTxOptions::offset
std::uint32_t offset
Definition: RelationalDatabase.h:69
ripple::RelationalDatabase::init
static std::unique_ptr< RelationalDatabase > init(Application &app, Config const &config, JobQueue &jobQueue)
init Creates and returns an appropriate RelationalDatabase instance based on configuration.
Definition: RelationalDatabase.cpp:34
ripple::RelationalDatabase::getMaxLedgerSeq
virtual std::optional< LedgerIndex > getMaxLedgerSeq()=0
getMaxLedgerSeq Returns the maximum ledger sequence in the Ledgers table.
ripple::Application
Definition: Application.h:115
ripple::RelationalDatabase::~RelationalDatabase
virtual ~RelationalDatabase()=default
std::shared_ptr
STL class.
ripple::RelationalDatabase::AccountTransactionsData::accounts
boost::container::flat_set< AccountID > accounts
Definition: RelationalDatabase.h:118
ripple::RelationalDatabase::AccountTxArgs
Definition: RelationalDatabase.h:96
std::pair
ripple::RPC::LedgerShortcut
LedgerShortcut
Definition: RPCHelpers.h:129
ripple::RelationalDatabase::AccountTxArgs::binary
bool binary
Definition: RelationalDatabase.h:100
ripple::rangeCheckedCast
T rangeCheckedCast(C c)
Definition: RelationalDatabase.h:252
std::vector
STL class.
ripple::RelationalDatabase::getTxHistory
virtual std::vector< std::shared_ptr< Transaction > > getTxHistory(LedgerIndex startIndex)=0
getTxHistory Returns the 20 most recent transactions starting from the given number.
ripple::RelationalDatabase::AccountTxPageOptions::minLedger
std::uint32_t minLedger
Definition: RelationalDatabase.h:77
ripple::RelationalDatabase::CountMinMax::minLedgerSequence
LedgerIndex minLedgerSequence
Definition: RelationalDatabase.h:54
ripple::RelationalDatabase::AccountTxResult::transactions
std::variant< AccountTxs, MetaTxsList > transactions
Definition: RelationalDatabase.h:108
ripple::RelationalDatabase::AccountTxOptions
Definition: RelationalDatabase.h:64
ripple::RelationalDatabase::AccountTxOptions::minLedger
std::uint32_t minLedger
Definition: RelationalDatabase.h:67
std::tuple
ripple::RelationalDatabase::AccountTxPageOptions::bAdmin
bool bAdmin
Definition: RelationalDatabase.h:81
ripple::RelationalDatabase::getNewestLedgerInfo
virtual std::optional< LedgerInfo > getNewestLedgerInfo()=0
getNewestLedgerInfo Returns the info of the newest saved ledger.
ripple::RelationalDatabase::AccountTxArgs::account
AccountID account
Definition: RelationalDatabase.h:98
ripple::RelationalDatabase::AccountTxOptions::limit
std::uint32_t limit
Definition: RelationalDatabase.h:70
ripple::RelationalDatabase::getLedgerInfoByIndex
virtual std::optional< LedgerInfo > getLedgerInfoByIndex(LedgerIndex ledgerSeq)=0
getLedgerInfoByIndex Returns a ledger by its sequence.
ripple::RelationalDatabase::AccountTxResult
Definition: RelationalDatabase.h:106
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
std::numeric_limits::lowest
T lowest(T... args)
ripple::RelationalDatabase::AccountTxPageOptions::maxLedger
std::uint32_t maxLedger
Definition: RelationalDatabase.h:78
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::RelationalDatabase::AccountTxResult::ledgerRange
LedgerRange ledgerRange
Definition: RelationalDatabase.h:109
ripple::TxMeta
Definition: TxMeta.h:32
ripple::RelationalDatabase::CountMinMax
Definition: RelationalDatabase.h:51
ripple::base_uint< 256 >
ripple::RelationalDatabase::AccountTxOptions::bUnlimited
bool bUnlimited
Definition: RelationalDatabase.h:71
ripple::RelationalDatabase::AccountTxPageOptions::account
AccountID const & account
Definition: RelationalDatabase.h:76
ripple::LedgerHashPair::ledgerHash
uint256 ledgerHash
Definition: RelationalDatabase.h:38
ripple::Config
Definition: Config.h:89
ripple::LedgerHashPair
Definition: RelationalDatabase.h:36
ripple::RelationalDatabase::AccountTxMarker::txnSeq
std::uint32_t txnSeq
Definition: RelationalDatabase.h:61
ripple::RelationalDatabase::getHashByIndex
virtual uint256 getHashByIndex(LedgerIndex ledgerIndex)=0
getHashByIndex Returns the hash of the ledger with the given sequence.
ripple::RelationalDatabase::AccountTxArgs::marker
std::optional< AccountTxMarker > marker
Definition: RelationalDatabase.h:103
ripple::RelationalDatabase::AccountTxArgs::limit
uint32_t limit
Definition: RelationalDatabase.h:102
ripple::RelationalDatabase::AccountTxMarker
Definition: RelationalDatabase.h:58
ripple::RelationalDatabase::AccountTxResult::marker
std::optional< AccountTxMarker > marker
Definition: RelationalDatabase.h:111
ripple::RelationalDatabase::getLedgerInfoByHash
virtual std::optional< LedgerInfo > getLedgerInfoByHash(uint256 const &ledgerHash)=0
getLedgerInfoByHash Returns the info of the ledger with given hash.
ripple::RelationalDatabase::getMinLedgerSeq
virtual std::optional< LedgerIndex > getMinLedgerSeq()=0
getMinLedgerSeq Returns the minimum ledger sequence in the Ledgers table.
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
std::map
STL class.
ripple::RelationalDatabase::CountMinMax::maxLedgerSequence
LedgerIndex maxLedgerSequence
Definition: RelationalDatabase.h:55
ripple::RelationalDatabase::AccountTxOptions::account
AccountID const & account
Definition: RelationalDatabase.h:66
ripple::JobQueue
A pool of threads to perform work.
Definition: JobQueue.h:55
ripple::RelationalDatabase::AccountTxOptions::maxLedger
std::uint32_t maxLedger
Definition: RelationalDatabase.h:68
ripple::RelationalDatabase::AccountTxArgs::forward
bool forward
Definition: RelationalDatabase.h:101
ripple::RelationalDatabase::AccountTransactionsData::transactionIndex
uint32_t transactionIndex
Definition: RelationalDatabase.h:120
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RelationalDatabase::transactionDbHasSpace
virtual bool transactionDbHasSpace(Config const &config)=0
transactionDbHasSpace Checks if the transaction database has available space.
ripple::RelationalDatabase::ledgerDbHasSpace
virtual bool ledgerDbHasSpace(Config const &config)=0
ledgerDbHasSpace Checks if the ledger database has available space.
ripple::RelationalDatabase::AccountTxPageOptions::limit
std::uint32_t limit
Definition: RelationalDatabase.h:80
ripple::LedgerRange::max
uint32_t max
Definition: RelationalDatabase.h:45
ripple::RelationalDatabase::AccountTxMarker::ledgerSeq
std::uint32_t ledgerSeq
Definition: RelationalDatabase.h:60
ripple::RelationalDatabase::LedgerSequence
uint32_t LedgerSequence
Definition: RelationalDatabase.h:90
ripple::LedgerHashPair::parentHash
uint256 parentHash
Definition: RelationalDatabase.h:39
std::optional
std::size_t
ripple::RelationalDatabase::CountMinMax::numberOfRows
std::size_t numberOfRows
Definition: RelationalDatabase.h:53
ripple::RelationalDatabase::AccountTransactionsData::ledgerSequence
uint32_t ledgerSequence
Definition: RelationalDatabase.h:119
ripple::RelationalDatabase::AccountTxArgs::ledger
std::optional< LedgerSpecifier > ledger
Definition: RelationalDatabase.h:99
ripple::LedgerRange
Definition: RelationalDatabase.h:42
ripple::RelationalDatabase::AccountTransactionsData::AccountTransactionsData
AccountTransactionsData(TxMeta const &meta, uint256 const &nodestoreHash, beast::Journal j)
Definition: RelationalDatabase.h:124
std::numeric_limits::max
T max(T... args)
ripple::RelationalDatabase
Definition: RelationalDatabase.h:48
ripple::LedgerRange::min
uint32_t min
Definition: RelationalDatabase.h:44
std::unique_ptr
STL class.
std::numeric_limits
ripple::RelationalDatabase::AccountTxResult::limit
uint32_t limit
Definition: RelationalDatabase.h:110
ripple::RelationalDatabase::getHashesByIndex
virtual std::optional< LedgerHashPair > getHashesByIndex(LedgerIndex ledgerIndex)=0
getHashesByIndex Returns the hashes of the ledger and its parent as specified by the ledgerIndex.
ripple::RelationalDatabase::AccountTransactionsData::txHash
uint256 txHash
Definition: RelationalDatabase.h:121
ripple::RelationalDatabase::AccountTxPageOptions::marker
std::optional< AccountTxMarker > marker
Definition: RelationalDatabase.h:79
ripple::RelationalDatabase::AccountTransactionsData::nodestoreHash
uint256 nodestoreHash
Definition: RelationalDatabase.h:122
ripple::RelationalDatabase::AccountTxPageOptions
Definition: RelationalDatabase.h:74
ripple::RelationalDatabase::AccountTransactionsData
Struct used to keep track of what to write to transactions and account_transactions tables in Postgre...
Definition: RelationalDatabase.h:116
std::variant