Compare commits

...

8 Commits

Author SHA1 Message Date
Pratik Mankawde
60d3b0082f style: Compare connection_ to nullptr explicitly 2026-10-02 17:15:09 +01:00
Pratik Mankawde
f4e5897e7c Merge branch 'develop'
Resolve the PeerReservationTable.h conflict with develop's naming and
member-init changes. Use insertOrAssign in the new error text, and add the
direct includes that misc-include-cleaner needs for the new Throw, JLOG
and std::runtime_error uses.
2026-10-02 16:34:21 +01:00
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 55 additions and 22 deletions

View File

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

View File

@@ -95,6 +95,10 @@ public:
private:
beast::Journal mutable journal_;
std::mutex mutable mutex_;
// 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_{};
std::unordered_set<PeerReservation, beast::Uhash<>, KeyEqual> table_;
};

View File

@@ -1,7 +1,9 @@
#pragma once
#include <xrpl/basics/Log.h>
#include <xrpl/basics/RangeSet.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/basics/contract.h>
#include <xrpl/beast/utility/Journal.h>
#include <xrpl/protocol/ErrorCodes.h>
#include <xrpl/protocol/LedgerHeader.h>
@@ -16,6 +18,7 @@
#include <map>
#include <memory>
#include <optional>
#include <stdexcept>
#include <utility>
#include <variant>
#include <vector>
@@ -398,15 +401,6 @@ public:
SQLiteDatabase&
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
* available space.
@@ -458,13 +452,27 @@ private:
}
/**
* @brief checkoutTransaction Checks out and returns node store ledger
* @brief checkoutLedger Checks out and returns 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
checkoutLedger()
{
if (!ledgerDb_)
{
constexpr auto msg = "Ledger database is not available";
JLOG(j_.fatal()) << msg;
Throw<std::runtime_error>(msg);
}
return ledgerDb_->checkoutDb();
}
@@ -472,10 +480,23 @@ private:
* @brief checkoutTransaction Checks out and returns 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
checkoutTransaction()
{
if (!txdb_)
{
constexpr auto msg = "Transaction database is not available";
JLOG(j_.fatal()) << msg;
Throw<std::runtime_error>(msg);
}
return txdb_->checkoutDb();
}
};

View File

@@ -559,18 +559,6 @@ SQLiteDatabase::SQLiteDatabase(SQLiteDatabase&& rhs) noexcept
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
SQLiteDatabase::transactionDbHasSpace(Config const& config)
{

View File

@@ -1,5 +1,6 @@
#include <xrpl/core/PeerReservationTable.h>
#include <xrpl/basics/contract.h>
#include <xrpl/json/json_value.h>
#include <xrpl/protocol/PublicKey.h>
#include <xrpl/protocol/jss.h>
@@ -10,6 +11,7 @@
#include <iterator>
#include <mutex>
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>
@@ -87,6 +89,15 @@ PeerReservationTable::insertOrAssign(PeerReservation const& 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_ == nullptr)
{
Throw<std::runtime_error>(
"PeerReservationTable::insertOrAssign: database connection is "
"not available");
}
auto db = connection_->checkoutDb();
insertPeerReservation(*db, reservation.nodeId, reservation.description);
@@ -105,6 +116,14 @@ PeerReservationTable::erase(PublicKey const& nodeId)
{
previous = *it;
table_.erase(it);
// Validate connection_ before dereferencing — see comment in
// insertOrAssign above.
if (connection_ == nullptr)
{
Throw<std::runtime_error>(
"PeerReservationTable::erase: database connection is not "
"available");
}
auto db = connection_->checkoutDb();
deletePeerReservation(*db, nodeId);
}