Compare commits

...

6 Commits

Author SHA1 Message Date
Pratik Mankawde
e2ee835952 minor naming change
Signed-off-by: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com>
2026-02-24 15:24:15 +00:00
Pratik Mankawde
f1723d8647 more checks
Signed-off-by: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com>
2026-02-24 15:17:13 +00:00
Pratik Mankawde
320f9c8866 Merge branch 'develop' into pratik/Add_checks_db_objects
Signed-off-by: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com>
2026-02-24 14:39:24 +00:00
Pratik Mankawde
229df04edd code review comments
Signed-off-by: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com>
2026-02-24 14:33:22 +00:00
Pratik Mankawde
640428a1d4 Merge branch 'develop' into pratik/Add_checks_db_objects 2025-12-11 16:31:16 +00:00
Pratik Mankawde
0363c12b23 added checks in SQLiteDatabase.cpp 2025-12-11 16:29:59 +00:00
5 changed files with 52 additions and 23 deletions

View File

@@ -136,6 +136,7 @@ words:
- legleux - legleux
- levelization - levelization
- levelized - levelized
- lgrdb
- libpb - libpb
- libxrpl - libxrpl
- llection - llection

View File

@@ -92,7 +92,11 @@ public:
private: private:
beast::Journal mutable journal_; beast::Journal mutable journal_;
std::mutex mutable mutex_; std::mutex mutable mutex_;
DatabaseCon* connection_; // Initialized to nullptr for safety. Set by load() during the second
// phase of ApplicationImp initialization. Methods that dereference
// this pointer must validate it first, since two-phase init means
// load() may not have been called yet.
DatabaseCon* connection_ = nullptr;
std::unordered_set<PeerReservation, beast::uhash<>, KeyEqual> table_; std::unordered_set<PeerReservation, beast::uhash<>, KeyEqual> table_;
}; };

View File

@@ -386,15 +386,6 @@ public:
SQLiteDatabase& SQLiteDatabase&
operator=(SQLiteDatabase&&) = delete; operator=(SQLiteDatabase&&) = delete;
/**
* @brief ledgerDbHasSpace Checks if the ledger database has available
* space.
* @param config Config object.
* @return True if space is available.
*/
bool
ledgerDbHasSpace(Config const& config);
/** /**
* @brief transactionDbHasSpace Checks if the transaction database has * @brief transactionDbHasSpace Checks if the transaction database has
* available space. * available space.
@@ -446,13 +437,27 @@ private:
} }
/** /**
* @brief checkoutTransaction Checks out and returns node store ledger * @brief checkoutLedger Checks out and returns node store ledger
* database. * database.
* @return Session to the node store ledger database. * @return Session to the node store ledger database.
* @throws std::runtime_error if ledger database is not available.
*
* @note Callers typically guard with existsLedger() before calling
* this method. The explicit null check here provides
* defense-in-depth so that safety does not depend solely on
* an implicit caller contract. See PR #6029 for context on
* the pattern of relying on config settings instead of
* validating actual objects.
*/ */
auto auto
checkoutLedger() checkoutLedger()
{ {
if (!ledgerDb_)
{
constexpr auto msg = "Ledger database is not available";
JLOG(j_.fatal()) << msg;
Throw<std::runtime_error>(msg);
}
return ledgerDb_->checkoutDb(); return ledgerDb_->checkoutDb();
} }
@@ -460,10 +465,23 @@ private:
* @brief checkoutTransaction Checks out and returns the node store * @brief checkoutTransaction Checks out and returns the node store
* transaction database. * transaction database.
* @return Session to the node store transaction database. * @return Session to the node store transaction database.
* @throws std::runtime_error if transaction database is not available.
*
* @note Callers typically guard with existsTransaction() and/or
* useTxTables_ before calling this method. The explicit null
* check here provides defense-in-depth so that safety does
* not depend solely on an implicit caller contract or config
* settings. See PR #6029 for context.
*/ */
auto auto
checkoutTransaction() checkoutTransaction()
{ {
if (!txdb_)
{
constexpr auto msg = "Transaction database is not available";
JLOG(j_.fatal()) << msg;
Throw<std::runtime_error>(msg);
}
return txdb_->checkoutDb(); return txdb_->checkoutDb();
} }
}; };

View File

@@ -524,18 +524,6 @@ SQLiteDatabase::SQLiteDatabase(SQLiteDatabase&& rhs) noexcept
std::exchange(txdb_, std::move(rhs.txdb_)); std::exchange(txdb_, std::move(rhs.txdb_));
} }
bool
SQLiteDatabase::ledgerDbHasSpace(Config const& config)
{
if (existsLedger())
{
auto db = checkoutLedger();
return detail::dbHasSpace(*db, config, j_);
}
return true;
}
bool bool
SQLiteDatabase::transactionDbHasSpace(Config const& config) SQLiteDatabase::transactionDbHasSpace(Config const& config)
{ {

View File

@@ -1,3 +1,4 @@
#include <xrpl/basics/contract.h>
#include <xrpl/core/PeerReservationTable.h> #include <xrpl/core/PeerReservationTable.h>
#include <xrpl/json/json_value.h> #include <xrpl/json/json_value.h>
#include <xrpl/protocol/PublicKey.h> #include <xrpl/protocol/PublicKey.h>
@@ -85,6 +86,15 @@ PeerReservationTable::insert_or_assign(PeerReservation const& reservation)
} }
table_.insert(hint, reservation); table_.insert(hint, reservation);
// connection_ is set by load() during two-phase init. Validate
// before dereferencing to guard against use-before-load or a reset
// connection. See PR #6029 for the general pattern discussion.
if (!connection_)
{
Throw<std::runtime_error>(
"PeerReservationTable::insert_or_assign: database connection is "
"not available");
}
auto db = connection_->checkoutDb(); auto db = connection_->checkoutDb();
insertPeerReservation(*db, reservation.nodeId, reservation.description); insertPeerReservation(*db, reservation.nodeId, reservation.description);
@@ -103,6 +113,14 @@ PeerReservationTable::erase(PublicKey const& nodeId)
{ {
previous = *it; previous = *it;
table_.erase(it); table_.erase(it);
// Validate connection_ before dereferencing — see comment in
// insert_or_assign above.
if (!connection_)
{
Throw<std::runtime_error>(
"PeerReservationTable::erase: database connection is not "
"available");
}
auto db = connection_->checkoutDb(); auto db = connection_->checkoutDb();
deletePeerReservation(*db, nodeId); deletePeerReservation(*db, nodeId);
} }