migrate lending usage of owner count

This commit is contained in:
Mayukha Vadari
2026-07-02 17:30:50 -04:00
parent 8d36b46aae
commit 4afd95e4b3
5 changed files with 46 additions and 5 deletions

View File

@@ -58,6 +58,21 @@ canApplyToBrokerCover(
bool
checkLendingProtocolDependencies(Rules const& rules, STTx const& tx);
/** Adjust a LoanBroker's own sfOwnerCount.
*
* A LoanBroker's OwnerCount tracks the number of outstanding loans it holds,
* and is distinct from the broker's pseudo-account's owner count. Unlike an
* account's OwnerCount it carries no reserve and cannot be sponsored, so it is
* adjusted directly here rather than through ReserveContext / increaseOwnerCount.
*
* @param view The apply view for making changes
* @param brokerSle The LoanBroker ledger entry
* @param amount Signed amount to add to the loan count (nonzero)
* @param j Journal for logging
*/
void
adjustOwnerCount(ApplyView& view, SLE::ref brokerSle, std::int32_t amount, beast::Journal j);
static constexpr std::uint32_t kSecondsInYear = 365 * 24 * 60 * 60;
Number

View File

@@ -224,9 +224,7 @@ adjustOwnerCountSigned(
if (!accountSle)
return; // LCOV_EXCL_LINE
auto const sleType = accountSle->getType();
bool const validType = sponsorSle ? sleType == ltACCOUNT_ROOT
: sleType == ltLOAN_BROKER || sleType == ltACCOUNT_ROOT;
bool const validType = accountSle->getType() == ltACCOUNT_ROOT;
XRPL_ASSERT(validType, "xrpl::adjustOwnerCountSigned : valid account sle type");
if (!validType)
return; // LCOV_EXCL_LINE

View File

@@ -9,6 +9,7 @@
#include <xrpl/ledger/ApplyView.h>
#include <xrpl/ledger/ReadView.h>
#include <xrpl/ledger/View.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/Asset.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/LedgerFormats.h>
@@ -25,6 +26,7 @@
#include <cstddef>
#include <cstdint>
#include <expected>
#include <limits>
#include <string_view>
#include <utility>
@@ -61,6 +63,32 @@ canApplyToBrokerCover(
return tesSUCCESS;
}
void
adjustOwnerCount(ApplyView& view, SLE::ref brokerSle, std::int32_t amount, beast::Journal j)
{
XRPL_ASSERT(
brokerSle && brokerSle->getType() == ltLOAN_BROKER,
"xrpl::adjustOwnerCount : valid LoanBroker sle");
if (!brokerSle)
return; // LCOV_EXCL_LINE
XRPL_ASSERT(amount, "xrpl::adjustOwnerCount : nonzero amount input");
// A LoanBroker's OwnerCount counts its outstanding loans; it carries no
// reserve, so clamp against underflow/overflow (as an account owner count
// would) but skip all reserve/sponsor accounting.
std::uint32_t const current = brokerSle->at(sfOwnerCount);
std::uint32_t adjusted = current + amount;
if (amount > 0 && adjusted < current)
adjusted = std::numeric_limits<std::uint32_t>::max(); // LCOV_EXCL_LINE
else if (amount < 0 && adjusted > current)
adjusted = 0; // LCOV_EXCL_LINE
AccountID const id = brokerSle->getAccountID(sfAccount);
view.adjustOwnerCountHook(id, current, adjusted);
brokerSle->at(sfOwnerCount) = adjusted;
view.update(brokerSle);
}
bool
checkLendingProtocolDependencies(Rules const& rules, STTx const& tx)
{

View File

@@ -110,7 +110,7 @@ LoanDelete::doApply()
// Decrement the LoanBroker's owner count.
// The broker's owner count is solely for the number of outstanding loans,
// and is distinct from the broker's pseudo-account's owner count
decreaseOwnerCount(view, ReserveContext::makeFromAccount(view, brokerSle, nullptr), 1, j_);
adjustOwnerCount(view, brokerSle, -1, j_);
// If there are no loans left, then any remaining debt must be forgiven,
// because there is no other way to pay it back.

View File

@@ -647,7 +647,7 @@ LoanSet::doApply()
adjustImpreciseNumber(brokerSle->at(sfDebtTotal), newDebtDelta, vaultAsset, vaultScale);
// The broker's owner count is solely for the number of outstanding loans,
// and is distinct from the broker's pseudo-account's owner count
increaseOwnerCount(view, ReserveContext::makeFromAccount(view, brokerSle, nullptr), 1, j_);
adjustOwnerCount(view, brokerSle, 1, j_);
loanSequenceProxy += 1;
// The sequence should be extremely unlikely to roll over, but fail if it
// does