Modularise RelationalDatabase

Signed-off-by: JCW <a1q123456@users.noreply.github.com>
This commit is contained in:
JCW
2026-01-08 13:05:42 +00:00
parent 0169c888e1
commit 30a4089133
17 changed files with 747 additions and 803 deletions

View File

@@ -0,0 +1,483 @@
#ifndef XRPL_APP_RDB_RELATIONALDATABASE_H_INCLUDED
#define XRPL_APP_RDB_RELATIONALDATABASE_H_INCLUDED
#include <xrpld/app/misc/Transaction.h>
#include <xrpld/core/Config.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/core/ServiceRegistry.h>
#include <xrpl/protocol/LedgerShortcut.h>
#include <xrpl/rdb/DatabaseCon.h>
#include <boost/filesystem.hpp>
#include <boost/variant.hpp>
namespace xrpl {
struct LedgerHashPair
{
uint256 ledgerHash;
uint256 parentHash;
};
struct LedgerRange
{
uint32_t min;
uint32_t max;
};
class RelationalDatabase
{
public:
struct CountMinMax
{
std::size_t numberOfRows;
LedgerIndex minLedgerSequence;
LedgerIndex maxLedgerSequence;
};
struct AccountTxMarker
{
std::uint32_t ledgerSeq = 0;
std::uint32_t txnSeq = 0;
};
struct AccountTxOptions
{
AccountID const& account;
std::uint32_t minLedger;
std::uint32_t maxLedger;
std::uint32_t offset;
std::uint32_t limit;
bool bUnlimited;
};
struct AccountTxPageOptions
{
AccountID const& account;
std::uint32_t minLedger;
std::uint32_t maxLedger;
std::optional<AccountTxMarker> marker;
std::uint32_t limit;
bool bAdmin;
};
using AccountTx =
std::pair<std::shared_ptr<Transaction>, std::shared_ptr<TxMeta>>;
using AccountTxs = std::vector<AccountTx>;
using txnMetaLedgerType = std::tuple<Blob, Blob, std::uint32_t>;
using MetaTxsList = std::vector<txnMetaLedgerType>;
using LedgerSequence = uint32_t;
using LedgerHash = uint256;
using LedgerSpecifier =
std::variant<LedgerRange, LedgerShortcut, LedgerSequence, LedgerHash>;
struct AccountTxArgs
{
AccountID account;
std::optional<LedgerSpecifier> ledger;
bool binary = false;
bool forward = false;
uint32_t limit = 0;
std::optional<AccountTxMarker> marker;
};
struct AccountTxResult
{
std::variant<AccountTxs, MetaTxsList> transactions;
LedgerRange ledgerRange;
uint32_t limit;
std::optional<AccountTxMarker> marker;
};
virtual ~RelationalDatabase() = default;
/**
* @brief getMinLedgerSeq Returns the minimum ledger sequence in the Ledgers
* table.
* @return Ledger sequence or no value if no ledgers exist.
*/
virtual std::optional<LedgerIndex>
getMinLedgerSeq() = 0;
/**
* @brief getMaxLedgerSeq Returns the maximum ledger sequence in the Ledgers
* table.
* @return Ledger sequence or none if no ledgers exist.
*/
virtual std::optional<LedgerIndex>
getMaxLedgerSeq() = 0;
/**
* @brief getLedgerInfoByIndex Returns a ledger by its sequence.
* @param ledgerSeq Ledger sequence.
* @return The ledger if found, otherwise no value.
*/
virtual std::optional<LedgerHeader>
getLedgerInfoByIndex(LedgerIndex ledgerSeq) = 0;
/**
* @brief getNewestLedgerInfo Returns the info of the newest saved ledger.
* @return Ledger info if found, otherwise no value.
*/
virtual std::optional<LedgerHeader>
getNewestLedgerInfo() = 0;
/**
* @brief getLedgerInfoByHash Returns the info of the ledger with given
* hash.
* @param ledgerHash Hash of the ledger.
* @return Ledger if found, otherwise no value.
*/
virtual std::optional<LedgerHeader>
getLedgerInfoByHash(uint256 const& ledgerHash) = 0;
/**
* @brief getHashByIndex Returns the hash of the ledger with the given
* sequence.
* @param ledgerIndex Ledger sequence.
* @return Hash of the ledger.
*/
virtual uint256
getHashByIndex(LedgerIndex ledgerIndex) = 0;
/**
* @brief getHashesByIndex Returns the hashes of the ledger and its parent
* as specified by the ledgerIndex.
* @param ledgerIndex Ledger sequence.
* @return Struct LedgerHashPair which contains hashes of the ledger and
* its parent.
*/
virtual std::optional<LedgerHashPair>
getHashesByIndex(LedgerIndex ledgerIndex) = 0;
/**
* @brief getHashesByIndex Returns hashes of each ledger and its parent for
* all ledgers within the provided range.
* @param minSeq Minimum ledger sequence.
* @param maxSeq Maximum ledger sequence.
* @return Container that maps the sequence number of a found ledger to the
* struct LedgerHashPair which contains the hashes of the ledger and
* its parent.
*/
virtual std::map<LedgerIndex, LedgerHashPair>
getHashesByIndex(LedgerIndex minSeq, LedgerIndex maxSeq) = 0;
/**
* @brief getTxHistory Returns the 20 most recent transactions starting from
* the given number.
* @param startIndex First number of returned entry.
* @return Vector of shared pointers to transactions sorted in
* descending order by ledger sequence.
*/
virtual std::vector<std::shared_ptr<Transaction>>
getTxHistory(LedgerIndex startIndex) = 0;
/**
* @brief getTransactionsMinLedgerSeq Returns the minimum ledger sequence
* stored in the Transactions table.
* @return Ledger sequence or no value if no ledgers exist.
*/
virtual std::optional<LedgerIndex>
getTransactionsMinLedgerSeq() = 0;
/**
* @brief getAccountTransactionsMinLedgerSeq Returns the minimum ledger
* sequence stored in the AccountTransactions table.
* @return Ledger sequence or no value if no ledgers exist.
*/
virtual std::optional<LedgerIndex>
getAccountTransactionsMinLedgerSeq() = 0;
/**
* @brief deleteTransactionByLedgerSeq Deletes transactions from the ledger
* with the given sequence.
* @param ledgerSeq Ledger sequence.
*/
virtual void
deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq) = 0;
/**
* @brief deleteBeforeLedgerSeq Deletes all ledgers with a sequence number
* less than or equal to the given ledger sequence.
* @param ledgerSeq Ledger sequence.
*/
virtual void
deleteBeforeLedgerSeq(LedgerIndex ledgerSeq) = 0;
/**
* @brief deleteTransactionsBeforeLedgerSeq Deletes all transactions with
* a sequence number less than or equal to the given ledger
* sequence.
* @param ledgerSeq Ledger sequence.
*/
virtual void
deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) = 0;
/**
* @brief deleteAccountTransactionsBeforeLedgerSeq Deletes all account
* transactions with a sequence number less than or equal to the
* given ledger sequence.
* @param ledgerSeq Ledger sequence.
*/
virtual void
deleteAccountTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) = 0;
/**
* @brief getTransactionCount Returns the number of transactions.
* @return Number of transactions.
*/
virtual std::size_t
getTransactionCount() = 0;
/**
* @brief getAccountTransactionCount Returns the number of account
* transactions.
* @return Number of account transactions.
*/
virtual std::size_t
getAccountTransactionCount() = 0;
/**
* @brief getLedgerCountMinMax Returns the minimum ledger sequence,
* maximum ledger sequence and total number of saved ledgers.
* @return Struct CountMinMax which contains the minimum sequence,
* maximum sequence and number of ledgers.
*/
virtual struct CountMinMax
getLedgerCountMinMax() = 0;
/**
* @brief saveValidatedLedger Saves a ledger into the database.
* @param ledger The ledger.
* @param current True if the ledger is current.
* @return True if saving was successful.
*/
virtual bool
saveValidatedLedger(
std::shared_ptr<Ledger const> const& ledger,
bool current) = 0;
/**
* @brief getLimitedOldestLedgerInfo Returns the info of the oldest ledger
* whose sequence number is greater than or equal to the given
* sequence number.
* @param ledgerFirstIndex Minimum ledger sequence.
* @return Ledger info if found, otherwise no value.
*/
virtual std::optional<LedgerHeader>
getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex) = 0;
/**
* @brief getLimitedNewestLedgerInfo Returns the info of the newest ledger
* whose sequence number is greater than or equal to the given
* sequence number.
* @param ledgerFirstIndex Minimum ledger sequence.
* @return Ledger info if found, otherwise no value.
*/
virtual std::optional<LedgerHeader>
getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex) = 0;
/**
* @brief getOldestAccountTxs Returns the oldest transactions for the
* account that matches the given criteria starting from the provided
* offset.
* @param options Struct AccountTxOptions which contains the criteria to
* match: the account, ledger search range, the offset of the first
* entry to return, the number of transactions to return, a flag if
* this number is unlimited.
* @return Vector of pairs of found transactions and their metadata
* sorted in ascending order by account sequence.
*/
virtual AccountTxs
getOldestAccountTxs(AccountTxOptions const& options) = 0;
/**
* @brief getNewestAccountTxs Returns the newest transactions for the
* account that matches the given criteria starting from the provided
* offset.
* @param options Struct AccountTxOptions which contains the criteria to
* match: the account, the ledger search range, the offset of the
* first entry to return, the number of transactions to return, a
* flag if this number unlimited.
* @return Vector of pairs of found transactions and their metadata
* sorted in descending order by account sequence.
*/
virtual AccountTxs
getNewestAccountTxs(AccountTxOptions const& options) = 0;
/**
* @brief getOldestAccountTxsB Returns the oldest transactions in binary
* form for the account that matches the given criteria starting from
* the provided offset.
* @param options Struct AccountTxOptions which contains the criteria to
* match: the account, the ledger search range, the offset of the
* first entry to return, the number of transactions to return, a
* flag if this number unlimited.
* @return Vector of tuples of found transactions, their metadata and
* account sequences sorted in ascending order by account sequence.
*/
virtual MetaTxsList
getOldestAccountTxsB(AccountTxOptions const& options) = 0;
/**
* @brief getNewestAccountTxsB Returns the newest transactions in binary
* form for the account that matches the given criteria starting from
* the provided offset.
* @param options Struct AccountTxOptions which contains the criteria to
* match: the account, the ledger search range, the offset of the
* first entry to return, the number of transactions to return, a
* flag if this number is unlimited.
* @return Vector of tuples of found transactions, their metadata and
* account sequences sorted in descending order by account
* sequence.
*/
virtual MetaTxsList
getNewestAccountTxsB(AccountTxOptions const& options) = 0;
/**
* @brief oldestAccountTxPage Returns the oldest transactions for the
* account that matches the given criteria starting from the
* provided marker.
* @param options Struct AccountTxPageOptions which contains the criteria to
* match: the account, the ledger search range, the marker of first
* returned entry, the number of transactions to return, a flag if
* this number is unlimited.
* @return Vector of pairs of found transactions and their metadata
* sorted in ascending order by account sequence and a marker
* for the next search if the search was not finished.
*/
virtual std::pair<AccountTxs, std::optional<AccountTxMarker>>
oldestAccountTxPage(AccountTxPageOptions const& options) = 0;
/**
* @brief newestAccountTxPage Returns the newest transactions for the
* account that matches the given criteria starting from the provided
* marker.
* @param options Struct AccountTxPageOptions which contains the criteria to
* match: the account, the ledger search range, the marker of the
* first returned entry, the number of transactions to return, a flag
* if this number unlimited.
* @return Vector of pairs of found transactions and their metadata
* sorted in descending order by account sequence and a marker
* for the next search if the search was not finished.
*/
virtual std::pair<AccountTxs, std::optional<AccountTxMarker>>
newestAccountTxPage(AccountTxPageOptions const& options) = 0;
/**
* @brief oldestAccountTxPageB Returns the oldest transactions in binary
* form for the account that matches the given criteria starting from
* the provided marker.
* @param options Struct AccountTxPageOptions which contains criteria to
* match: the account, the ledger search range, the marker of the
* first returned entry, the number of transactions to return, a flag
* if this number unlimited.
* @return Vector of tuples of found transactions, their metadata and
* account sequences sorted in ascending order by account
* sequence and a marker for the next search if the search was not
* finished.
*/
virtual std::pair<MetaTxsList, std::optional<AccountTxMarker>>
oldestAccountTxPageB(AccountTxPageOptions const& options) = 0;
/**
* @brief newestAccountTxPageB Returns the newest transactions in binary
* form for the account that matches the given criteria starting from
* the provided marker.
* @param options Struct AccountTxPageOptions which contains the criteria to
* match: the account, the ledger search range, the marker of the
* first returned entry, the number of transactions to return, a flag
* if this number is unlimited.
* @return Vector of tuples of found transactions, their metadata and
* account sequences sorted in descending order by account
* sequence and a marker for the next search if the search was not
* finished.
*/
virtual std::pair<MetaTxsList, std::optional<AccountTxMarker>>
newestAccountTxPageB(AccountTxPageOptions const& options) = 0;
/**
* @brief getTransaction Returns the transaction with the given hash. If a
* range is provided but the transaction is not found, then check if
* all ledgers in the range are present in the database.
* @param id Hash of the transaction.
* @param range Range of ledgers to check, if present.
* @param ec Default error code value.
* @return Transaction and its metadata if found, otherwise TxSearched::all
* if a range is provided and all ledgers from the range are present
* in the database, TxSearched::some if a range is provided and not
* all ledgers are present, TxSearched::unknown if the range is not
* provided or a deserializing error occurred. In the last case the
* error code is returned via the ec parameter, in other cases the
* default error code is not changed.
*/
virtual std::variant<AccountTx, TxSearched>
getTransaction(
uint256 const& id,
std::optional<ClosedInterval<uint32_t>> const& range,
error_code_i& ec) = 0;
/**
* @brief getKBUsedAll Returns the amount of space used by all databases.
* @return Space in kilobytes.
*/
virtual uint32_t
getKBUsedAll() = 0;
/**
* @brief getKBUsedLedger Returns the amount of space space used by the
* ledger database.
* @return Space in kilobytes.
*/
virtual uint32_t
getKBUsedLedger() = 0;
/**
* @brief getKBUsedTransaction Returns the amount of space used by the
* transaction database.
* @return Space in kilobytes.
*/
virtual uint32_t
getKBUsedTransaction() = 0;
/**
* @brief Closes the ledger database
*/
virtual void
closeLedgerDB() = 0;
/**
* @brief Closes the transaction database
*/
virtual void
closeTransactionDB() = 0;
};
template <class T, class C>
T
rangeCheckedCast(C c)
{
if ((c > std::numeric_limits<T>::max()) ||
(!std::numeric_limits<T>::is_signed && c < 0) ||
(std::numeric_limits<T>::is_signed &&
std::numeric_limits<C>::is_signed &&
c < std::numeric_limits<T>::lowest()))
{
// This should never happen
// LCOV_EXCL_START
UNREACHABLE("xrpl::rangeCheckedCast : domain error");
JLOG(debugLog().error())
<< "rangeCheckedCast domain error:"
<< " value = " << c << " min = " << std::numeric_limits<T>::lowest()
<< " max: " << std::numeric_limits<T>::max();
// LCOV_EXCL_STOP
}
return static_cast<T>(c);
}
} // namespace xrpl
#endif

View File

@@ -2,7 +2,6 @@
#include <xrpld/app/ledger/Ledger.h>
#include <xrpld/app/ledger/PendingSaves.h>
#include <xrpld/app/misc/HashRouter.h>
#include <xrpld/app/rdb/backend/SQLiteDatabase.h>
#include <xrpld/consensus/LedgerTiming.h>
#include <xrpld/core/Config.h> // for Config::FEE_UNITS_DEPRECATED
@@ -23,6 +22,7 @@
#include <xrpl/protocol/SecretKey.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/protocol/jss.h>
#include <xrpl/rdb/RelationalDatabase.h>
#include <utility>
#include <vector>
@@ -964,12 +964,9 @@ saveValidatedLedger(
return true;
}
auto const db =
dynamic_cast<SQLiteDatabase*>(&registry.getRelationalDatabase());
if (!db)
Throw<std::runtime_error>("Failed to get relational database");
auto& db = registry.getRelationalDatabase();
auto const res = db->saveValidatedLedger(ledger, current);
auto const res = db.saveValidatedLedger(ledger, current);
// Clients can now trust the database for
// information about this ledger sequence.

View File

@@ -13,7 +13,6 @@
#include <xrpld/app/misc/TxQ.h>
#include <xrpld/app/misc/ValidatorList.h>
#include <xrpld/app/paths/PathRequests.h>
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpld/overlay/Overlay.h>
#include <xrpld/overlay/Peer.h>
@@ -29,6 +28,7 @@
#include <xrpl/protocol/BuildInfo.h>
#include <xrpl/protocol/HashPrefix.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/rdb/RelationalDatabase.h>
#include <xrpl/resource/Fees.h>
#include <algorithm>

View File

@@ -23,7 +23,7 @@
#include <xrpld/app/misc/ValidatorKeys.h>
#include <xrpld/app/misc/ValidatorSite.h>
#include <xrpld/app/paths/PathRequests.h>
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpld/app/rdb/backend/SQLiteDatabase.h>
#include <xrpld/app/rpc/LedgerToJson.h>
#include <xrpld/app/tx/apply.h>
#include <xrpld/core/ServiceRegistryImpl.h>
@@ -204,7 +204,7 @@ public:
boost::asio::steady_timer sweepTimer_;
boost::asio::steady_timer entropyTimer_;
std::unique_ptr<RelationalDatabase> mRelationalDatabase;
std::unique_ptr<SQLiteDatabase> relationalDatabase_;
std::unique_ptr<DatabaseCon> mWalletDB;
std::unique_ptr<Overlay> overlay_;
std::optional<uint256> trapTxID_;
@@ -835,10 +835,10 @@ public:
getRelationalDatabase() override
{
XRPL_ASSERT(
mRelationalDatabase,
relationalDatabase_,
"xrpl::ApplicationImp::getRelationalDatabase : non-null "
"relational database");
return *mRelationalDatabase;
return *relationalDatabase_;
}
DatabaseCon&
@@ -878,7 +878,7 @@ public:
try
{
mRelationalDatabase = RelationalDatabase::init(
relationalDatabase_ = setup_RelationalDatabase(
getServiceRegistry(), *config_, *m_jobQueue);
// wallet database
@@ -1005,7 +1005,7 @@ public:
doSweep()
{
if (!config_->standalone() &&
!getRelationalDatabase().transactionDbHasSpace(*config_))
!relationalDatabase_->transactionDbHasSpace(*config_))
{
signalStop("Out of transaction DB space");
}

View File

@@ -1,7 +1,7 @@
#ifndef XRPL_APP_MISC_IMPL_ACCOUNTTXPAGING_H_INCLUDED
#define XRPL_APP_MISC_IMPL_ACCOUNTTXPAGING_H_INCLUDED
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpl/rdb/RelationalDatabase.h>
#include <cstdint>

View File

@@ -1,7 +1,6 @@
#include <xrpld/app/ledger/LedgerMaster.h>
#include <xrpld/app/misc/HashRouter.h>
#include <xrpld/app/misc/Transaction.h>
#include <xrpld/app/rdb/backend/SQLiteDatabase.h>
#include <xrpld/app/tx/apply.h>
#include <xrpld/rpc/CTID.h>
@@ -9,6 +8,7 @@
#include <xrpl/core/ServiceRegistry.h>
#include <xrpl/protocol/ErrorCodes.h>
#include <xrpl/protocol/jss.h>
#include <xrpl/rdb/RelationalDatabase.h>
namespace xrpl {
@@ -127,14 +127,9 @@ Transaction::load(
std::optional<ClosedInterval<uint32_t>> const& range,
error_code_i& ec)
{
auto const db = dynamic_cast<SQLiteDatabase*>(&app.getRelationalDatabase());
auto& db = app.getRelationalDatabase();
if (!db)
{
Throw<std::runtime_error>("Failed to get relational database");
}
return db->getTransaction(id, range, ec);
return db.getTransaction(id, range, ec);
}
// options 1 to include the date of the transaction

View File

@@ -1,47 +0,0 @@
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpld/core/ConfigSections.h>
namespace xrpl {
extern std::unique_ptr<RelationalDatabase>
getSQLiteDatabase(
ServiceRegistry& registry,
Config const& config,
JobQueue& jobQueue);
std::unique_ptr<RelationalDatabase>
RelationalDatabase::init(
ServiceRegistry& registry,
Config const& config,
JobQueue& jobQueue)
{
bool use_sqlite = false;
Section const& rdb_section{config.section(SECTION_RELATIONAL_DB)};
if (!rdb_section.empty())
{
if (boost::iequals(get(rdb_section, "backend"), "sqlite"))
{
use_sqlite = true;
}
else
{
Throw<std::runtime_error>(
"Invalid rdb_section backend value: " +
get(rdb_section, "backend"));
}
}
else
{
use_sqlite = true;
}
if (use_sqlite)
{
return getSQLiteDatabase(registry, config, jobQueue);
}
return std::unique_ptr<RelationalDatabase>();
}
} // namespace xrpl

View File

@@ -1,232 +0,0 @@
#ifndef XRPL_APP_RDB_RELATIONALDATABASE_H_INCLUDED
#define XRPL_APP_RDB_RELATIONALDATABASE_H_INCLUDED
#include <xrpld/app/misc/Transaction.h>
#include <xrpld/core/Config.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/core/ServiceRegistry.h>
#include <xrpl/protocol/LedgerShortcut.h>
#include <xrpl/rdb/DatabaseCon.h>
#include <boost/filesystem.hpp>
#include <boost/variant.hpp>
namespace xrpl {
struct LedgerHashPair
{
uint256 ledgerHash;
uint256 parentHash;
};
struct LedgerRange
{
uint32_t min;
uint32_t max;
};
class RelationalDatabase
{
public:
struct CountMinMax
{
std::size_t numberOfRows;
LedgerIndex minLedgerSequence;
LedgerIndex maxLedgerSequence;
};
struct AccountTxMarker
{
std::uint32_t ledgerSeq = 0;
std::uint32_t txnSeq = 0;
};
struct AccountTxOptions
{
AccountID const& account;
std::uint32_t minLedger;
std::uint32_t maxLedger;
std::uint32_t offset;
std::uint32_t limit;
bool bUnlimited;
};
struct AccountTxPageOptions
{
AccountID const& account;
std::uint32_t minLedger;
std::uint32_t maxLedger;
std::optional<AccountTxMarker> marker;
std::uint32_t limit;
bool bAdmin;
};
using AccountTx =
std::pair<std::shared_ptr<Transaction>, std::shared_ptr<TxMeta>>;
using AccountTxs = std::vector<AccountTx>;
using txnMetaLedgerType = std::tuple<Blob, Blob, std::uint32_t>;
using MetaTxsList = std::vector<txnMetaLedgerType>;
using LedgerSequence = uint32_t;
using LedgerHash = uint256;
using LedgerSpecifier =
std::variant<LedgerRange, LedgerShortcut, LedgerSequence, LedgerHash>;
struct AccountTxArgs
{
AccountID account;
std::optional<LedgerSpecifier> ledger;
bool binary = false;
bool forward = false;
uint32_t limit = 0;
std::optional<AccountTxMarker> marker;
};
struct AccountTxResult
{
std::variant<AccountTxs, MetaTxsList> transactions;
LedgerRange ledgerRange;
uint32_t limit;
std::optional<AccountTxMarker> marker;
};
/**
* @brief init Creates and returns an appropriate RelationalDatabase
* instance based on configuration.
* @param registry The service registry.
* @param config Config object.
* @param jobQueue JobQueue object.
* @return Unique pointer to the interface.
*/
static std::unique_ptr<RelationalDatabase>
init(ServiceRegistry& registry, Config const& config, JobQueue& jobQueue);
virtual ~RelationalDatabase() = default;
/**
* @brief getMinLedgerSeq Returns the minimum ledger sequence in the Ledgers
* table.
* @return Ledger sequence or no value if no ledgers exist.
*/
virtual std::optional<LedgerIndex>
getMinLedgerSeq() = 0;
/**
* @brief getMaxLedgerSeq Returns the maximum ledger sequence in the Ledgers
* table.
* @return Ledger sequence or none if no ledgers exist.
*/
virtual std::optional<LedgerIndex>
getMaxLedgerSeq() = 0;
/**
* @brief getLedgerInfoByIndex Returns a ledger by its sequence.
* @param ledgerSeq Ledger sequence.
* @return The ledger if found, otherwise no value.
*/
virtual std::optional<LedgerHeader>
getLedgerInfoByIndex(LedgerIndex ledgerSeq) = 0;
/**
* @brief getNewestLedgerInfo Returns the info of the newest saved ledger.
* @return Ledger info if found, otherwise no value.
*/
virtual std::optional<LedgerHeader>
getNewestLedgerInfo() = 0;
/**
* @brief getLedgerInfoByHash Returns the info of the ledger with given
* hash.
* @param ledgerHash Hash of the ledger.
* @return Ledger if found, otherwise no value.
*/
virtual std::optional<LedgerHeader>
getLedgerInfoByHash(uint256 const& ledgerHash) = 0;
/**
* @brief getHashByIndex Returns the hash of the ledger with the given
* sequence.
* @param ledgerIndex Ledger sequence.
* @return Hash of the ledger.
*/
virtual uint256
getHashByIndex(LedgerIndex ledgerIndex) = 0;
/**
* @brief getHashesByIndex Returns the hashes of the ledger and its parent
* as specified by the ledgerIndex.
* @param ledgerIndex Ledger sequence.
* @return Struct LedgerHashPair which contains hashes of the ledger and
* its parent.
*/
virtual std::optional<LedgerHashPair>
getHashesByIndex(LedgerIndex ledgerIndex) = 0;
/**
* @brief getHashesByIndex Returns hashes of each ledger and its parent for
* all ledgers within the provided range.
* @param minSeq Minimum ledger sequence.
* @param maxSeq Maximum ledger sequence.
* @return Container that maps the sequence number of a found ledger to the
* struct LedgerHashPair which contains the hashes of the ledger and
* its parent.
*/
virtual std::map<LedgerIndex, LedgerHashPair>
getHashesByIndex(LedgerIndex minSeq, LedgerIndex maxSeq) = 0;
/**
* @brief getTxHistory Returns the 20 most recent transactions starting from
* the given number.
* @param startIndex First number of returned entry.
* @return Vector of shared pointers to transactions sorted in
* descending order by ledger sequence.
*/
virtual std::vector<std::shared_ptr<Transaction>>
getTxHistory(LedgerIndex startIndex) = 0;
/**
* @brief ledgerDbHasSpace Checks if the ledger database has available
* space.
* @param config Config object.
* @return True if space is available.
*/
virtual bool
ledgerDbHasSpace(Config const& config) = 0;
/**
* @brief transactionDbHasSpace Checks if the transaction database has
* available space.
* @param config Config object.
* @return True if space is available.
*/
virtual bool
transactionDbHasSpace(Config const& config) = 0;
};
template <class T, class C>
T
rangeCheckedCast(C c)
{
if ((c > std::numeric_limits<T>::max()) ||
(!std::numeric_limits<T>::is_signed && c < 0) ||
(std::numeric_limits<T>::is_signed &&
std::numeric_limits<C>::is_signed &&
c < std::numeric_limits<T>::lowest()))
{
// This should never happen
// LCOV_EXCL_START
UNREACHABLE("xrpl::rangeCheckedCast : domain error");
JLOG(debugLog().error())
<< "rangeCheckedCast domain error:"
<< " value = " << c << " min = " << std::numeric_limits<T>::lowest()
<< " max: " << std::numeric_limits<T>::max();
// LCOV_EXCL_STOP
}
return static_cast<T>(c);
}
} // namespace xrpl
#endif

View File

@@ -1,294 +1,247 @@
#ifndef XRPL_APP_RDB_BACKEND_SQLITEDATABASE_H_INCLUDED
#define XRPL_APP_RDB_BACKEND_SQLITEDATABASE_H_INCLUDED
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpl/rdb/RelationalDatabase.h>
#include <memory>
namespace xrpl {
class SQLiteDatabase : public RelationalDatabase
class Config;
class JobQueue;
class ServiceRegistry;
class SQLiteDatabase final : public RelationalDatabase
{
public:
/**
* @brief getTransactionsMinLedgerSeq Returns the minimum ledger sequence
* stored in the Transactions table.
* @return Ledger sequence or no value if no ledgers exist.
*/
virtual std::optional<LedgerIndex>
getTransactionsMinLedgerSeq() = 0;
SQLiteDatabase(
ServiceRegistry& registry,
Config const& config,
JobQueue& jobQueue)
: registry_(registry)
, useTxTables_(config.useTxTables())
, j_(registry.journal("SQLiteDatabase"))
{
DatabaseCon::Setup const setup = setup_DatabaseCon(config, j_);
if (!makeLedgerDBs(
config,
setup,
DatabaseCon::CheckpointerSetup{&jobQueue, &registry_.logs()}))
{
std::string_view constexpr error =
"Failed to create ledger databases";
/**
* @brief getAccountTransactionsMinLedgerSeq Returns the minimum ledger
* sequence stored in the AccountTransactions table.
* @return Ledger sequence or no value if no ledgers exist.
*/
virtual std::optional<LedgerIndex>
getAccountTransactionsMinLedgerSeq() = 0;
JLOG(j_.fatal()) << error;
Throw<std::runtime_error>(error.data());
}
}
/**
* @brief deleteTransactionByLedgerSeq Deletes transactions from the ledger
* with the given sequence.
* @param ledgerSeq Ledger sequence.
*/
virtual void
deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq) = 0;
std::optional<LedgerIndex>
getMinLedgerSeq() override;
/**
* @brief deleteBeforeLedgerSeq Deletes all ledgers with a sequence number
* less than or equal to the given ledger sequence.
* @param ledgerSeq Ledger sequence.
*/
virtual void
deleteBeforeLedgerSeq(LedgerIndex ledgerSeq) = 0;
std::optional<LedgerIndex>
getTransactionsMinLedgerSeq() override;
/**
* @brief deleteTransactionsBeforeLedgerSeq Deletes all transactions with
* a sequence number less than or equal to the given ledger
* sequence.
* @param ledgerSeq Ledger sequence.
*/
virtual void
deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) = 0;
std::optional<LedgerIndex>
getAccountTransactionsMinLedgerSeq() override;
/**
* @brief deleteAccountTransactionsBeforeLedgerSeq Deletes all account
* transactions with a sequence number less than or equal to the
* given ledger sequence.
* @param ledgerSeq Ledger sequence.
*/
virtual void
deleteAccountTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) = 0;
std::optional<LedgerIndex>
getMaxLedgerSeq() override;
/**
* @brief getTransactionCount Returns the number of transactions.
* @return Number of transactions.
*/
virtual std::size_t
getTransactionCount() = 0;
void
deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq) override;
/**
* @brief getAccountTransactionCount Returns the number of account
* transactions.
* @return Number of account transactions.
*/
virtual std::size_t
getAccountTransactionCount() = 0;
void
deleteBeforeLedgerSeq(LedgerIndex ledgerSeq) override;
/**
* @brief getLedgerCountMinMax Returns the minimum ledger sequence,
* maximum ledger sequence and total number of saved ledgers.
* @return Struct CountMinMax which contains the minimum sequence,
* maximum sequence and number of ledgers.
*/
virtual struct CountMinMax
getLedgerCountMinMax() = 0;
void
deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) override;
/**
* @brief saveValidatedLedger Saves a ledger into the database.
* @param ledger The ledger.
* @param current True if the ledger is current.
* @return True if saving was successful.
*/
virtual bool
void
deleteAccountTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) override;
std::size_t
getTransactionCount() override;
std::size_t
getAccountTransactionCount() override;
RelationalDatabase::CountMinMax
getLedgerCountMinMax() override;
bool
saveValidatedLedger(
std::shared_ptr<Ledger const> const& ledger,
bool current) = 0;
bool current) override;
/**
* @brief getLimitedOldestLedgerInfo Returns the info of the oldest ledger
* whose sequence number is greater than or equal to the given
* sequence number.
* @param ledgerFirstIndex Minimum ledger sequence.
* @return Ledger info if found, otherwise no value.
*/
virtual std::optional<LedgerHeader>
getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex) = 0;
std::optional<LedgerHeader>
getLedgerInfoByIndex(LedgerIndex ledgerSeq) override;
/**
* @brief getLimitedNewestLedgerInfo Returns the info of the newest ledger
* whose sequence number is greater than or equal to the given
* sequence number.
* @param ledgerFirstIndex Minimum ledger sequence.
* @return Ledger info if found, otherwise no value.
*/
virtual std::optional<LedgerHeader>
getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex) = 0;
std::optional<LedgerHeader>
getNewestLedgerInfo() override;
/**
* @brief getOldestAccountTxs Returns the oldest transactions for the
* account that matches the given criteria starting from the provided
* offset.
* @param options Struct AccountTxOptions which contains the criteria to
* match: the account, ledger search range, the offset of the first
* entry to return, the number of transactions to return, a flag if
* this number is unlimited.
* @return Vector of pairs of found transactions and their metadata
* sorted in ascending order by account sequence.
*/
virtual AccountTxs
getOldestAccountTxs(AccountTxOptions const& options) = 0;
std::optional<LedgerHeader>
getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex) override;
/**
* @brief getNewestAccountTxs Returns the newest transactions for the
* account that matches the given criteria starting from the provided
* offset.
* @param options Struct AccountTxOptions which contains the criteria to
* match: the account, the ledger search range, the offset of the
* first entry to return, the number of transactions to return, a
* flag if this number unlimited.
* @return Vector of pairs of found transactions and their metadata
* sorted in descending order by account sequence.
*/
virtual AccountTxs
getNewestAccountTxs(AccountTxOptions const& options) = 0;
std::optional<LedgerHeader>
getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex) override;
/**
* @brief getOldestAccountTxsB Returns the oldest transactions in binary
* form for the account that matches the given criteria starting from
* the provided offset.
* @param options Struct AccountTxOptions which contains the criteria to
* match: the account, the ledger search range, the offset of the
* first entry to return, the number of transactions to return, a
* flag if this number unlimited.
* @return Vector of tuples of found transactions, their metadata and
* account sequences sorted in ascending order by account sequence.
*/
virtual MetaTxsList
getOldestAccountTxsB(AccountTxOptions const& options) = 0;
std::optional<LedgerHeader>
getLedgerInfoByHash(uint256 const& ledgerHash) override;
/**
* @brief getNewestAccountTxsB Returns the newest transactions in binary
* form for the account that matches the given criteria starting from
* the provided offset.
* @param options Struct AccountTxOptions which contains the criteria to
* match: the account, the ledger search range, the offset of the
* first entry to return, the number of transactions to return, a
* flag if this number is unlimited.
* @return Vector of tuples of found transactions, their metadata and
* account sequences sorted in descending order by account
* sequence.
*/
virtual MetaTxsList
getNewestAccountTxsB(AccountTxOptions const& options) = 0;
uint256
getHashByIndex(LedgerIndex ledgerIndex) override;
/**
* @brief oldestAccountTxPage Returns the oldest transactions for the
* account that matches the given criteria starting from the
* provided marker.
* @param options Struct AccountTxPageOptions which contains the criteria to
* match: the account, the ledger search range, the marker of first
* returned entry, the number of transactions to return, a flag if
* this number is unlimited.
* @return Vector of pairs of found transactions and their metadata
* sorted in ascending order by account sequence and a marker
* for the next search if the search was not finished.
*/
virtual std::pair<AccountTxs, std::optional<AccountTxMarker>>
oldestAccountTxPage(AccountTxPageOptions const& options) = 0;
std::optional<LedgerHashPair>
getHashesByIndex(LedgerIndex ledgerIndex) override;
/**
* @brief newestAccountTxPage Returns the newest transactions for the
* account that matches the given criteria starting from the provided
* marker.
* @param options Struct AccountTxPageOptions which contains the criteria to
* match: the account, the ledger search range, the marker of the
* first returned entry, the number of transactions to return, a flag
* if this number unlimited.
* @return Vector of pairs of found transactions and their metadata
* sorted in descending order by account sequence and a marker
* for the next search if the search was not finished.
*/
virtual std::pair<AccountTxs, std::optional<AccountTxMarker>>
newestAccountTxPage(AccountTxPageOptions const& options) = 0;
std::map<LedgerIndex, LedgerHashPair>
getHashesByIndex(LedgerIndex minSeq, LedgerIndex maxSeq) override;
/**
* @brief oldestAccountTxPageB Returns the oldest transactions in binary
* form for the account that matches the given criteria starting from
* the provided marker.
* @param options Struct AccountTxPageOptions which contains criteria to
* match: the account, the ledger search range, the marker of the
* first returned entry, the number of transactions to return, a flag
* if this number unlimited.
* @return Vector of tuples of found transactions, their metadata and
* account sequences sorted in ascending order by account
* sequence and a marker for the next search if the search was not
* finished.
*/
virtual std::pair<MetaTxsList, std::optional<AccountTxMarker>>
oldestAccountTxPageB(AccountTxPageOptions const& options) = 0;
std::vector<std::shared_ptr<Transaction>>
getTxHistory(LedgerIndex startIndex) override;
/**
* @brief newestAccountTxPageB Returns the newest transactions in binary
* form for the account that matches the given criteria starting from
* the provided marker.
* @param options Struct AccountTxPageOptions which contains the criteria to
* match: the account, the ledger search range, the marker of the
* first returned entry, the number of transactions to return, a flag
* if this number is unlimited.
* @return Vector of tuples of found transactions, their metadata and
* account sequences sorted in descending order by account
* sequence and a marker for the next search if the search was not
* finished.
*/
virtual std::pair<MetaTxsList, std::optional<AccountTxMarker>>
newestAccountTxPageB(AccountTxPageOptions const& options) = 0;
AccountTxs
getOldestAccountTxs(AccountTxOptions const& options) override;
/**
* @brief getTransaction Returns the transaction with the given hash. If a
* range is provided but the transaction is not found, then check if
* all ledgers in the range are present in the database.
* @param id Hash of the transaction.
* @param range Range of ledgers to check, if present.
* @param ec Default error code value.
* @return Transaction and its metadata if found, otherwise TxSearched::all
* if a range is provided and all ledgers from the range are present
* in the database, TxSearched::some if a range is provided and not
* all ledgers are present, TxSearched::unknown if the range is not
* provided or a deserializing error occurred. In the last case the
* error code is returned via the ec parameter, in other cases the
* default error code is not changed.
*/
virtual std::variant<AccountTx, TxSearched>
AccountTxs
getNewestAccountTxs(AccountTxOptions const& options) override;
MetaTxsList
getOldestAccountTxsB(AccountTxOptions const& options) override;
MetaTxsList
getNewestAccountTxsB(AccountTxOptions const& options) override;
std::pair<AccountTxs, std::optional<AccountTxMarker>>
oldestAccountTxPage(AccountTxPageOptions const& options) override;
std::pair<AccountTxs, std::optional<AccountTxMarker>>
newestAccountTxPage(AccountTxPageOptions const& options) override;
std::pair<MetaTxsList, std::optional<AccountTxMarker>>
oldestAccountTxPageB(AccountTxPageOptions const& options) override;
std::pair<MetaTxsList, std::optional<AccountTxMarker>>
newestAccountTxPageB(AccountTxPageOptions const& options) override;
std::variant<AccountTx, TxSearched>
getTransaction(
uint256 const& id,
std::optional<ClosedInterval<uint32_t>> const& range,
error_code_i& ec) = 0;
std::optional<ClosedInterval<std::uint32_t>> const& range,
error_code_i& ec) override;
std::uint32_t
getKBUsedAll() override;
std::uint32_t
getKBUsedLedger() override;
std::uint32_t
getKBUsedTransaction() override;
void
closeLedgerDB() override;
void
closeTransactionDB() override;
/**
* @brief getKBUsedAll Returns the amount of space used by all databases.
* @return Space in kilobytes.
* @brief ledgerDbHasSpace Checks if the ledger database has available
* space.
* @param config Config object.
* @return True if space is available.
*/
virtual uint32_t
getKBUsedAll() = 0;
bool
ledgerDbHasSpace(Config const& config);
/**
* @brief getKBUsedLedger Returns the amount of space space used by the
* ledger database.
* @return Space in kilobytes.
* @brief transactionDbHasSpace Checks if the transaction database has
* available space.
* @param config Config object.
* @return True if space is available.
*/
virtual uint32_t
getKBUsedLedger() = 0;
bool
transactionDbHasSpace(Config const& config);
private:
ServiceRegistry& registry_;
bool const useTxTables_;
beast::Journal j_;
std::unique_ptr<DatabaseCon> lgrdb_, txdb_;
/**
* @brief getKBUsedTransaction Returns the amount of space used by the
* @brief makeLedgerDBs Opens ledger and transaction databases for the node
* store, and stores their descriptors in private member variables.
* @param config Config object.
* @param setup Path to the databases and other opening parameters.
* @param checkpointerSetup Checkpointer parameters.
* @return True if node databases opened successfully.
*/
bool
makeLedgerDBs(
Config const& config,
DatabaseCon::Setup const& setup,
DatabaseCon::CheckpointerSetup const& checkpointerSetup);
/**
* @brief existsLedger Checks if the node store ledger database exists.
* @return True if the node store ledger database exists.
*/
bool
existsLedger()
{
return static_cast<bool>(lgrdb_);
}
/**
* @brief existsTransaction Checks if the node store transaction database
* exists.
* @return True if the node store transaction database exists.
*/
bool
existsTransaction()
{
return static_cast<bool>(txdb_);
}
/**
* @brief checkoutTransaction Checks out and returns node store ledger
* database.
* @return Session to the node store ledger database.
*/
auto
checkoutLedger()
{
return lgrdb_->checkoutDb();
}
/**
* @brief checkoutTransaction Checks out and returns the node store
* transaction database.
* @return Space in kilobytes.
* @return Session to the node store transaction database.
*/
virtual uint32_t
getKBUsedTransaction() = 0;
/**
* @brief Closes the ledger database
*/
virtual void
closeLedgerDB() = 0;
/**
* @brief Closes the transaction database
*/
virtual void
closeTransactionDB() = 0;
auto
checkoutTransaction()
{
return txdb_->checkoutDb();
}
};
/**
* @brief setup_RelationalDatabase Creates and returns a SQLiteDatabase
* instance based on configuration.
* @param registry The service registry.
* @param config Config object.
* @param jobQueue JobQueue object.
* @return Unique pointer to the SQLiteDatabase implementation.
*/
std::unique_ptr<SQLiteDatabase>
setup_RelationalDatabase(
ServiceRegistry& registry,
Config const& config,
JobQueue& jobQueue);
} // namespace xrpl
#endif

View File

@@ -2,7 +2,6 @@
#include <xrpld/app/ledger/LedgerMaster.h>
#include <xrpld/app/ledger/PendingSaves.h>
#include <xrpld/app/ledger/TransactionMaster.h>
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpld/app/rdb/backend/detail/Node.h>
#include <xrpld/app/rpc/LedgerToJson.h>
@@ -11,6 +10,7 @@
#include <xrpl/core/ServiceRegistry.h>
#include <xrpl/json/to_string.h>
#include <xrpl/rdb/DatabaseCon.h>
#include <xrpl/rdb/RelationalDatabase.h>
#include <xrpl/rdb/SociDB.h>
#include <boost/range/adaptor/transformed.hpp>

View File

@@ -2,9 +2,10 @@
#define XRPL_APP_RDB_BACKEND_DETAIL_NODE_H_INCLUDED
#include <xrpld/app/ledger/Ledger.h>
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpld/core/Config.h>
#include <xrpl/rdb/RelationalDatabase.h>
namespace xrpl {
namespace detail {

View File

@@ -10,213 +10,8 @@
namespace xrpl {
class SQLiteDatabaseImp final : public SQLiteDatabase
{
public:
SQLiteDatabaseImp(
ServiceRegistry& registry,
Config const& config,
JobQueue& jobQueue)
: registry_(registry)
, useTxTables_(config.useTxTables())
, j_(registry.journal("SQLiteDatabaseImp"))
{
DatabaseCon::Setup const setup = setup_DatabaseCon(config, j_);
if (!makeLedgerDBs(
config,
setup,
DatabaseCon::CheckpointerSetup{&jobQueue, &registry_.logs()}))
{
std::string_view constexpr error =
"Failed to create ledger databases";
JLOG(j_.fatal()) << error;
Throw<std::runtime_error>(error.data());
}
}
std::optional<LedgerIndex>
getMinLedgerSeq() override;
std::optional<LedgerIndex>
getTransactionsMinLedgerSeq() override;
std::optional<LedgerIndex>
getAccountTransactionsMinLedgerSeq() override;
std::optional<LedgerIndex>
getMaxLedgerSeq() override;
void
deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq) override;
void
deleteBeforeLedgerSeq(LedgerIndex ledgerSeq) override;
void
deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) override;
void
deleteAccountTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq) override;
std::size_t
getTransactionCount() override;
std::size_t
getAccountTransactionCount() override;
RelationalDatabase::CountMinMax
getLedgerCountMinMax() override;
bool
saveValidatedLedger(
std::shared_ptr<Ledger const> const& ledger,
bool current) override;
std::optional<LedgerHeader>
getLedgerInfoByIndex(LedgerIndex ledgerSeq) override;
std::optional<LedgerHeader>
getNewestLedgerInfo() override;
std::optional<LedgerHeader>
getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex) override;
std::optional<LedgerHeader>
getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex) override;
std::optional<LedgerHeader>
getLedgerInfoByHash(uint256 const& ledgerHash) override;
uint256
getHashByIndex(LedgerIndex ledgerIndex) override;
std::optional<LedgerHashPair>
getHashesByIndex(LedgerIndex ledgerIndex) override;
std::map<LedgerIndex, LedgerHashPair>
getHashesByIndex(LedgerIndex minSeq, LedgerIndex maxSeq) override;
std::vector<std::shared_ptr<Transaction>>
getTxHistory(LedgerIndex startIndex) override;
AccountTxs
getOldestAccountTxs(AccountTxOptions const& options) override;
AccountTxs
getNewestAccountTxs(AccountTxOptions const& options) override;
MetaTxsList
getOldestAccountTxsB(AccountTxOptions const& options) override;
MetaTxsList
getNewestAccountTxsB(AccountTxOptions const& options) override;
std::pair<AccountTxs, std::optional<AccountTxMarker>>
oldestAccountTxPage(AccountTxPageOptions const& options) override;
std::pair<AccountTxs, std::optional<AccountTxMarker>>
newestAccountTxPage(AccountTxPageOptions const& options) override;
std::pair<MetaTxsList, std::optional<AccountTxMarker>>
oldestAccountTxPageB(AccountTxPageOptions const& options) override;
std::pair<MetaTxsList, std::optional<AccountTxMarker>>
newestAccountTxPageB(AccountTxPageOptions const& options) override;
std::variant<AccountTx, TxSearched>
getTransaction(
uint256 const& id,
std::optional<ClosedInterval<std::uint32_t>> const& range,
error_code_i& ec) override;
bool
ledgerDbHasSpace(Config const& config) override;
bool
transactionDbHasSpace(Config const& config) override;
std::uint32_t
getKBUsedAll() override;
std::uint32_t
getKBUsedLedger() override;
std::uint32_t
getKBUsedTransaction() override;
void
closeLedgerDB() override;
void
closeTransactionDB() override;
private:
ServiceRegistry& registry_;
bool const useTxTables_;
beast::Journal j_;
std::unique_ptr<DatabaseCon> lgrdb_, txdb_;
/**
* @brief makeLedgerDBs Opens ledger and transaction databases for the node
* store, and stores their descriptors in private member variables.
* @param config Config object.
* @param setup Path to the databases and other opening parameters.
* @param checkpointerSetup Checkpointer parameters.
* @return True if node databases opened successfully.
*/
bool
makeLedgerDBs(
Config const& config,
DatabaseCon::Setup const& setup,
DatabaseCon::CheckpointerSetup const& checkpointerSetup);
/**
* @brief existsLedger Checks if the node store ledger database exists.
* @return True if the node store ledger database exists.
*/
bool
existsLedger()
{
return static_cast<bool>(lgrdb_);
}
/**
* @brief existsTransaction Checks if the node store transaction database
* exists.
* @return True if the node store transaction database exists.
*/
bool
existsTransaction()
{
return static_cast<bool>(txdb_);
}
/**
* @brief checkoutTransaction Checks out and returns node store ledger
* database.
* @return Session to the node store ledger database.
*/
auto
checkoutLedger()
{
return lgrdb_->checkoutDb();
}
/**
* @brief checkoutTransaction Checks out and returns the node store
* transaction database.
* @return Session to the node store transaction database.
*/
auto
checkoutTransaction()
{
return txdb_->checkoutDb();
}
};
bool
SQLiteDatabaseImp::makeLedgerDBs(
SQLiteDatabase::makeLedgerDBs(
Config const& config,
DatabaseCon::Setup const& setup,
DatabaseCon::CheckpointerSetup const& checkpointerSetup)
@@ -229,7 +24,7 @@ SQLiteDatabaseImp::makeLedgerDBs(
}
std::optional<LedgerIndex>
SQLiteDatabaseImp::getMinLedgerSeq()
SQLiteDatabase::getMinLedgerSeq()
{
/* if databases exists, use it */
if (existsLedger())
@@ -243,7 +38,7 @@ SQLiteDatabaseImp::getMinLedgerSeq()
}
std::optional<LedgerIndex>
SQLiteDatabaseImp::getTransactionsMinLedgerSeq()
SQLiteDatabase::getTransactionsMinLedgerSeq()
{
if (!useTxTables_)
return {};
@@ -258,7 +53,7 @@ SQLiteDatabaseImp::getTransactionsMinLedgerSeq()
}
std::optional<LedgerIndex>
SQLiteDatabaseImp::getAccountTransactionsMinLedgerSeq()
SQLiteDatabase::getAccountTransactionsMinLedgerSeq()
{
if (!useTxTables_)
return {};
@@ -274,7 +69,7 @@ SQLiteDatabaseImp::getAccountTransactionsMinLedgerSeq()
}
std::optional<LedgerIndex>
SQLiteDatabaseImp::getMaxLedgerSeq()
SQLiteDatabase::getMaxLedgerSeq()
{
if (existsLedger())
{
@@ -286,7 +81,7 @@ SQLiteDatabaseImp::getMaxLedgerSeq()
}
void
SQLiteDatabaseImp::deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq)
SQLiteDatabase::deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq)
{
if (!useTxTables_)
return;
@@ -301,7 +96,7 @@ SQLiteDatabaseImp::deleteTransactionByLedgerSeq(LedgerIndex ledgerSeq)
}
void
SQLiteDatabaseImp::deleteBeforeLedgerSeq(LedgerIndex ledgerSeq)
SQLiteDatabase::deleteBeforeLedgerSeq(LedgerIndex ledgerSeq)
{
if (existsLedger())
{
@@ -313,7 +108,7 @@ SQLiteDatabaseImp::deleteBeforeLedgerSeq(LedgerIndex ledgerSeq)
}
void
SQLiteDatabaseImp::deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq)
SQLiteDatabase::deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq)
{
if (!useTxTables_)
return;
@@ -328,8 +123,7 @@ SQLiteDatabaseImp::deleteTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq)
}
void
SQLiteDatabaseImp::deleteAccountTransactionsBeforeLedgerSeq(
LedgerIndex ledgerSeq)
SQLiteDatabase::deleteAccountTransactionsBeforeLedgerSeq(LedgerIndex ledgerSeq)
{
if (!useTxTables_)
return;
@@ -344,7 +138,7 @@ SQLiteDatabaseImp::deleteAccountTransactionsBeforeLedgerSeq(
}
std::size_t
SQLiteDatabaseImp::getTransactionCount()
SQLiteDatabase::getTransactionCount()
{
if (!useTxTables_)
return 0;
@@ -359,7 +153,7 @@ SQLiteDatabaseImp::getTransactionCount()
}
std::size_t
SQLiteDatabaseImp::getAccountTransactionCount()
SQLiteDatabase::getAccountTransactionCount()
{
if (!useTxTables_)
return 0;
@@ -374,7 +168,7 @@ SQLiteDatabaseImp::getAccountTransactionCount()
}
RelationalDatabase::CountMinMax
SQLiteDatabaseImp::getLedgerCountMinMax()
SQLiteDatabase::getLedgerCountMinMax()
{
if (existsLedger())
{
@@ -386,7 +180,7 @@ SQLiteDatabaseImp::getLedgerCountMinMax()
}
bool
SQLiteDatabaseImp::saveValidatedLedger(
SQLiteDatabase::saveValidatedLedger(
std::shared_ptr<Ledger const> const& ledger,
bool current)
{
@@ -401,7 +195,7 @@ SQLiteDatabaseImp::saveValidatedLedger(
}
std::optional<LedgerHeader>
SQLiteDatabaseImp::getLedgerInfoByIndex(LedgerIndex ledgerSeq)
SQLiteDatabase::getLedgerInfoByIndex(LedgerIndex ledgerSeq)
{
if (existsLedger())
{
@@ -416,7 +210,7 @@ SQLiteDatabaseImp::getLedgerInfoByIndex(LedgerIndex ledgerSeq)
}
std::optional<LedgerHeader>
SQLiteDatabaseImp::getNewestLedgerInfo()
SQLiteDatabase::getNewestLedgerInfo()
{
if (existsLedger())
{
@@ -431,7 +225,7 @@ SQLiteDatabaseImp::getNewestLedgerInfo()
}
std::optional<LedgerHeader>
SQLiteDatabaseImp::getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex)
SQLiteDatabase::getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex)
{
if (existsLedger())
{
@@ -447,7 +241,7 @@ SQLiteDatabaseImp::getLimitedOldestLedgerInfo(LedgerIndex ledgerFirstIndex)
}
std::optional<LedgerHeader>
SQLiteDatabaseImp::getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex)
SQLiteDatabase::getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex)
{
if (existsLedger())
{
@@ -463,7 +257,7 @@ SQLiteDatabaseImp::getLimitedNewestLedgerInfo(LedgerIndex ledgerFirstIndex)
}
std::optional<LedgerHeader>
SQLiteDatabaseImp::getLedgerInfoByHash(uint256 const& ledgerHash)
SQLiteDatabase::getLedgerInfoByHash(uint256 const& ledgerHash)
{
if (existsLedger())
{
@@ -478,7 +272,7 @@ SQLiteDatabaseImp::getLedgerInfoByHash(uint256 const& ledgerHash)
}
uint256
SQLiteDatabaseImp::getHashByIndex(LedgerIndex ledgerIndex)
SQLiteDatabase::getHashByIndex(LedgerIndex ledgerIndex)
{
if (existsLedger())
{
@@ -493,7 +287,7 @@ SQLiteDatabaseImp::getHashByIndex(LedgerIndex ledgerIndex)
}
std::optional<LedgerHashPair>
SQLiteDatabaseImp::getHashesByIndex(LedgerIndex ledgerIndex)
SQLiteDatabase::getHashesByIndex(LedgerIndex ledgerIndex)
{
if (existsLedger())
{
@@ -508,7 +302,7 @@ SQLiteDatabaseImp::getHashesByIndex(LedgerIndex ledgerIndex)
}
std::map<LedgerIndex, LedgerHashPair>
SQLiteDatabaseImp::getHashesByIndex(LedgerIndex minSeq, LedgerIndex maxSeq)
SQLiteDatabase::getHashesByIndex(LedgerIndex minSeq, LedgerIndex maxSeq)
{
if (existsLedger())
{
@@ -523,7 +317,7 @@ SQLiteDatabaseImp::getHashesByIndex(LedgerIndex minSeq, LedgerIndex maxSeq)
}
std::vector<std::shared_ptr<Transaction>>
SQLiteDatabaseImp::getTxHistory(LedgerIndex startIndex)
SQLiteDatabase::getTxHistory(LedgerIndex startIndex)
{
if (!useTxTables_)
return {};
@@ -542,7 +336,7 @@ SQLiteDatabaseImp::getTxHistory(LedgerIndex startIndex)
}
RelationalDatabase::AccountTxs
SQLiteDatabaseImp::getOldestAccountTxs(AccountTxOptions const& options)
SQLiteDatabase::getOldestAccountTxs(AccountTxOptions const& options)
{
if (!useTxTables_)
return {};
@@ -561,7 +355,7 @@ SQLiteDatabaseImp::getOldestAccountTxs(AccountTxOptions const& options)
}
RelationalDatabase::AccountTxs
SQLiteDatabaseImp::getNewestAccountTxs(AccountTxOptions const& options)
SQLiteDatabase::getNewestAccountTxs(AccountTxOptions const& options)
{
if (!useTxTables_)
return {};
@@ -580,7 +374,7 @@ SQLiteDatabaseImp::getNewestAccountTxs(AccountTxOptions const& options)
}
RelationalDatabase::MetaTxsList
SQLiteDatabaseImp::getOldestAccountTxsB(AccountTxOptions const& options)
SQLiteDatabase::getOldestAccountTxsB(AccountTxOptions const& options)
{
if (!useTxTables_)
return {};
@@ -595,7 +389,7 @@ SQLiteDatabaseImp::getOldestAccountTxsB(AccountTxOptions const& options)
}
RelationalDatabase::MetaTxsList
SQLiteDatabaseImp::getNewestAccountTxsB(AccountTxOptions const& options)
SQLiteDatabase::getNewestAccountTxsB(AccountTxOptions const& options)
{
if (!useTxTables_)
return {};
@@ -612,7 +406,7 @@ SQLiteDatabaseImp::getNewestAccountTxsB(AccountTxOptions const& options)
std::pair<
RelationalDatabase::AccountTxs,
std::optional<RelationalDatabase::AccountTxMarker>>
SQLiteDatabaseImp::oldestAccountTxPage(AccountTxPageOptions const& options)
SQLiteDatabase::oldestAccountTxPage(AccountTxPageOptions const& options)
{
if (!useTxTables_)
return {};
@@ -646,7 +440,7 @@ SQLiteDatabaseImp::oldestAccountTxPage(AccountTxPageOptions const& options)
std::pair<
RelationalDatabase::AccountTxs,
std::optional<RelationalDatabase::AccountTxMarker>>
SQLiteDatabaseImp::newestAccountTxPage(AccountTxPageOptions const& options)
SQLiteDatabase::newestAccountTxPage(AccountTxPageOptions const& options)
{
if (!useTxTables_)
return {};
@@ -680,7 +474,7 @@ SQLiteDatabaseImp::newestAccountTxPage(AccountTxPageOptions const& options)
std::pair<
RelationalDatabase::MetaTxsList,
std::optional<RelationalDatabase::AccountTxMarker>>
SQLiteDatabaseImp::oldestAccountTxPageB(AccountTxPageOptions const& options)
SQLiteDatabase::oldestAccountTxPageB(AccountTxPageOptions const& options)
{
if (!useTxTables_)
return {};
@@ -713,7 +507,7 @@ SQLiteDatabaseImp::oldestAccountTxPageB(AccountTxPageOptions const& options)
std::pair<
RelationalDatabase::MetaTxsList,
std::optional<RelationalDatabase::AccountTxMarker>>
SQLiteDatabaseImp::newestAccountTxPageB(AccountTxPageOptions const& options)
SQLiteDatabase::newestAccountTxPageB(AccountTxPageOptions const& options)
{
if (!useTxTables_)
return {};
@@ -744,7 +538,7 @@ SQLiteDatabaseImp::newestAccountTxPageB(AccountTxPageOptions const& options)
}
std::variant<RelationalDatabase::AccountTx, TxSearched>
SQLiteDatabaseImp::getTransaction(
SQLiteDatabase::getTransaction(
uint256 const& id,
std::optional<ClosedInterval<std::uint32_t>> const& range,
error_code_i& ec)
@@ -762,7 +556,7 @@ SQLiteDatabaseImp::getTransaction(
}
bool
SQLiteDatabaseImp::ledgerDbHasSpace(Config const& config)
SQLiteDatabase::ledgerDbHasSpace(Config const& config)
{
if (existsLedger())
{
@@ -774,7 +568,7 @@ SQLiteDatabaseImp::ledgerDbHasSpace(Config const& config)
}
bool
SQLiteDatabaseImp::transactionDbHasSpace(Config const& config)
SQLiteDatabase::transactionDbHasSpace(Config const& config)
{
if (!useTxTables_)
return true;
@@ -789,7 +583,7 @@ SQLiteDatabaseImp::transactionDbHasSpace(Config const& config)
}
std::uint32_t
SQLiteDatabaseImp::getKBUsedAll()
SQLiteDatabase::getKBUsedAll()
{
if (existsLedger())
{
@@ -800,7 +594,7 @@ SQLiteDatabaseImp::getKBUsedAll()
}
std::uint32_t
SQLiteDatabaseImp::getKBUsedLedger()
SQLiteDatabase::getKBUsedLedger()
{
if (existsLedger())
{
@@ -811,7 +605,7 @@ SQLiteDatabaseImp::getKBUsedLedger()
}
std::uint32_t
SQLiteDatabaseImp::getKBUsedTransaction()
SQLiteDatabase::getKBUsedTransaction()
{
if (!useTxTables_)
return 0;
@@ -825,24 +619,24 @@ SQLiteDatabaseImp::getKBUsedTransaction()
}
void
SQLiteDatabaseImp::closeLedgerDB()
SQLiteDatabase::closeLedgerDB()
{
lgrdb_.reset();
}
void
SQLiteDatabaseImp::closeTransactionDB()
SQLiteDatabase::closeTransactionDB()
{
txdb_.reset();
}
std::unique_ptr<RelationalDatabase>
getSQLiteDatabase(
std::unique_ptr<SQLiteDatabase>
setup_RelationalDatabase(
ServiceRegistry& registry,
Config const& config,
JobQueue& jobQueue)
{
return std::make_unique<SQLiteDatabaseImp>(registry, config, jobQueue);
return std::make_unique<SQLiteDatabase>(registry, config, jobQueue);
}
} // namespace xrpl

View File

@@ -2,7 +2,6 @@
#include <xrpld/app/misc/NetworkOPs.h>
#include <xrpld/app/misc/ValidatorList.h>
#include <xrpld/app/misc/ValidatorSite.h>
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpld/overlay/Cluster.h>
#include <xrpld/overlay/detail/ConnectAttempt.h>
#include <xrpld/overlay/detail/PeerImp.h>
@@ -18,6 +17,7 @@
#include <xrpl/basics/random.h>
#include <xrpl/beast/core/LexicalCast.h>
#include <xrpl/protocol/STTx.h>
#include <xrpl/rdb/RelationalDatabase.h>
#include <xrpl/server/SimpleWriter.h>
#include <xrpl/server/Wallet.h>

View File

@@ -1,9 +1,8 @@
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpl/core/PeerReservationTable.h>
#include <xrpl/json/json_value.h>
#include <xrpl/protocol/PublicKey.h>
#include <xrpl/protocol/jss.h>
#include <xrpl/rdb/RelationalDatabase.h>
#include <xrpl/server/Wallet.h>
#include <algorithm>

View File

@@ -1,6 +1,5 @@
#include <xrpld/app/misc/Transaction.h>
#include <xrpld/app/paths/TrustLine.h>
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpld/app/tx/detail/NFTokenUtils.h>
#include <xrpld/rpc/Context.h>
#include <xrpld/rpc/DeliveredAmount.h>
@@ -10,6 +9,7 @@
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/RPCErr.h>
#include <xrpl/protocol/nftPageMask.h>
#include <xrpl/rdb/RelationalDatabase.h>
#include <xrpl/resource/Fees.h>
#include <boost/algorithm/string/case_conv.hpp>

View File

@@ -3,18 +3,19 @@
#include <xrpld/app/misc/DeliverMax.h>
#include <xrpld/app/misc/NetworkOPs.h>
#include <xrpld/app/misc/Transaction.h>
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpld/rpc/CTID.h>
#include <xrpld/rpc/Context.h>
#include <xrpld/rpc/DeliveredAmount.h>
#include <xrpld/rpc/GRPCHandlers.h>
#include <xrpld/rpc/MPTokenIssuanceID.h>
#include <xrpld/rpc/Status.h>
#include <xrpl/basics/ToString.h>
#include <xrpl/protocol/ErrorCodes.h>
#include <xrpl/protocol/NFTSyntheticSerializer.h>
#include <xrpl/protocol/RPCErr.h>
#include <xrpl/protocol/jss.h>
#include <xrpl/rdb/RelationalDatabase.h>
#include <regex>

View File

@@ -2,13 +2,13 @@
#include <xrpld/app/main/Application.h>
#include <xrpld/app/misc/DeliverMax.h>
#include <xrpld/app/misc/Transaction.h>
#include <xrpld/app/rdb/backend/RelationalDatabase.h>
#include <xrpld/rpc/Context.h>
#include <xrpld/rpc/Role.h>
#include <xrpl/protocol/ErrorCodes.h>
#include <xrpl/protocol/RPCErr.h>
#include <xrpl/protocol/jss.h>
#include <xrpl/rdb/RelationalDatabase.h>
#include <xrpl/resource/Fees.h>
namespace xrpl {