more cleanup

This commit is contained in:
Mayukha Vadari
2026-06-24 12:54:46 -04:00
parent 002922e459
commit 506ab2b590
11 changed files with 93 additions and 168 deletions

View File

@@ -85,85 +85,6 @@ confineOwnerCount(
return adjusted;
}
static std::uint32_t
ownerCountHlp(
ReadView const& view,
SLE::const_ref sle,
std::int32_t adjustment,
std::optional<AccountID> reportId,
beast::Journal j)
{
AccountID const id = sle->getAccountID(sfAccount);
std::uint32_t const savedCount = sle->at(sfOwnerCount);
std::uint32_t const hookedCount = view.ownerCountHook(id, savedCount);
std::uint32_t const sponsoredCount = sle->at(sfSponsoredOwnerCount);
std::uint32_t const sponsoringCount = sle->at(sfSponsoringOwnerCount);
if (hookedCount < sponsoredCount)
{
Throw<std::logic_error>(
"xrpl::ownerCountHlp : OwnerCount must be greater than or equal to "
"SponsoredOwnerCount");
}
std::int64_t deltaCount =
static_cast<std::int64_t>(adjustment) - sponsoredCount + sponsoringCount;
if (deltaCount > std::numeric_limits<std::int32_t>::max())
{
deltaCount = std::numeric_limits<std::int32_t>::max();
JLOG(j.fatal()) << "Account " << id << " delta count exceeds max, "
<< "adjustment: " << adjustment << ", sponsoredCount: " << sponsoredCount
<< ", sponsoringOwnerCount: " << sponsoringCount;
}
else if (deltaCount < std::numeric_limits<std::int32_t>::min())
{
deltaCount = std::numeric_limits<std::int32_t>::min();
JLOG(j.fatal()) << "Account " << id << " delta count exceeds min, "
<< "adjustment: " << adjustment << ", sponsoredCount: " << sponsoredCount
<< ", sponsoringCount: " << sponsoringCount;
}
return confineOwnerCount(hookedCount, static_cast<std::int32_t>(deltaCount), reportId, j);
}
static std::uint32_t
reserveCountHlp(SLE::const_ref sle, std::int32_t adjustment)
{
bool const isSponsored = sle->isFieldPresent(sfSponsor);
std::uint32_t const sponsoringCount = sle->getFieldU32(sfSponsoringAccountCount);
std::uint32_t const reserveCount = (isSponsored ? 0 : 1) + sponsoringCount;
return confineOwnerCount(reserveCount, adjustment);
}
static inline XRPAmount
baseReserveHlp(ReadView const& view, std::uint32_t ownerCount, std::uint32_t reserveCount)
{
auto const& fees = view.fees();
return (fees.reserve * reserveCount) + (fees.increment * ownerCount);
}
static XRPAmount
reserveHlp(
ReadView const& view,
SLE::const_ref sle,
std::uint32_t ownerCount,
std::uint32_t reserveCount)
{
// Pseudo-accounts have no reserve requirement
if (isPseudoAccount(sle))
return XRPAmount(0);
auto const reserve = baseReserveHlp(view, ownerCount, reserveCount);
return reserve;
}
std::uint32_t
ownerCount(ReadView const& view, SLE::const_ref sle, beast::Journal j, std::int32_t adjustment)
{
return ownerCountHlp(view, sle, adjustment, sle->getAccountID(sfAccount), j);
}
XRPAmount
xrpLiquid(ReadView const& view, AccountID const& id, std::int32_t ownerCountAdj, beast::Journal j)
{
@@ -171,9 +92,10 @@ xrpLiquid(ReadView const& view, AccountID const& id, std::int32_t ownerCountAdj,
if (sle == nullptr)
return beast::kZero;
std::uint32_t const ownerCount = ownerCountHlp(view, sle, ownerCountAdj, std::nullopt, j);
std::uint32_t const reserveCount = reserveCountHlp(sle, 0);
auto const reserve = reserveHlp(view, sle, ownerCount, reserveCount);
std::uint32_t const ownerCount = view.ownerCountHook(id, sle->at(sfOwnerCount)) -
sle->at(sfSponsoredOwnerCount) + sle->at(sfSponsoringOwnerCount) + ownerCountAdj;
auto const& fees = view.fees();
auto const reserve = (fees.reserve * accountOwnerCount(sle)) + (fees.increment * ownerCount);
auto const fullBalance = sle->getFieldAmount(sfBalance);
@@ -221,8 +143,8 @@ adjustOwnerCountHlp(
void
adjustOwnerCount(
ApplyView& view,
SLE::ref accountSle,
SLE::ref sponsorSle,
SLE::pointer accountSle,
SLE::pointer sponsorSle,
std::int32_t adjustment,
beast::Journal j)
{
@@ -265,8 +187,8 @@ adjustOwnerCount(
void
adjustOwnerCountObj(
ApplyView& view,
SLE::ref accountSle,
SLE::ref objectSle,
SLE::pointer accountSle,
SLE::pointer objectSle,
std::int32_t amount,
beast::Journal j)
{
@@ -275,37 +197,10 @@ adjustOwnerCountObj(
if (objectSle->getType() == ltACCOUNT_ROOT)
Throw<std::logic_error>("xrpl::adjustOwnerCount : valid object sle type");
SLE::ref sponsorSle = getLedgerEntryReserveSponsor(view, objectSle);
SLE::pointer sponsorSle = getLedgerEntryReserveSponsor(view, objectSle);
adjustOwnerCount(view, accountSle, sponsorSle, amount, j);
}
XRPAmount
accountReserve(
ReadView const& view,
SLE::const_ref sle,
beast::Journal j,
std::int32_t ownerCountAdj,
std::int32_t reserveCountAdj)
{
if (!sle)
Throw<std::runtime_error>("xrpl::accountReserve : valid sle");
if (sle->getType() != ltACCOUNT_ROOT)
Throw<std::logic_error>("xrpl::accountReserve : valid sle type");
std::uint32_t const ownerCount =
ownerCountHlp(view, sle, ownerCountAdj, sle->getAccountID(sfAccount), j);
std::uint32_t const reserveCount = reserveCountHlp(sle, reserveCountAdj);
return reserveHlp(view, sle, ownerCount, reserveCount);
}
XRPAmount
baseAccountReserve(ReadView const& view, std::int32_t ownerCount)
{
auto const reserve = baseReserveHlp(view, ownerCount, 1);
return reserve;
}
TER
checkInsufficientReserve(
ReadView const& view,
@@ -338,7 +233,7 @@ checkInsufficientReserve(
auto const sponsorBalance = sponsorSle->getFieldAmount(sfBalance);
STAmount const sponsorReserve =
accountReserve(view, sponsorSle, j, ownerCountDelta, reserveCountDelta);
totalAccountReserve(view, sponsorSle, ownerCountDelta, reserveCountDelta);
if (sponsorBalance < sponsorReserve)
return tecINSUFFICIENT_RESERVE;
@@ -346,7 +241,7 @@ checkInsufficientReserve(
else
{
STAmount const reserve =
accountReserve(view, accSle, j, ownerCountDelta, reserveCountDelta);
totalAccountReserve(view, accSle, ownerCountDelta, reserveCountDelta);
if (accBalance < reserve)
return tecINSUFFICIENT_RESERVE;
}
@@ -405,7 +300,7 @@ getPseudoAccountFields()
}
[[nodiscard]] bool
isPseudoAccount(SLE::const_ref sleAcct, std::set<SField const*> const& pseudoFieldFilter)
isPseudoAccount(SLE::const_pointer sleAcct, std::set<SField const*> const& pseudoFieldFilter)
{
auto const& fields = getPseudoAccountFields();

View File

@@ -206,8 +206,7 @@ authorizeMPToken(
// items. This is similar to the reserve requirements of trust lines.
// If PreFunded Sponsor, it must be checked whether sufficient
// ReserveCount exists.
if (ownerCount(view, *sponsorSle ? *sponsorSle : sleAcct, journal) >= 2 ||
isSponsoredAndPreFunded)
if (objectOwnerCount(*sponsorSle ? *sponsorSle : sleAcct) >= 2 || isSponsoredAndPreFunded)
{
if (auto const ret = checkInsufficientReserve(
view, tx, sleAcct, priorBalance, *sponsorSle, 1, 0, journal);

View File

@@ -553,8 +553,8 @@ Transactor::checkFee(PreclaimContext const& ctx, XRPAmount baseFee)
if (feePayer.type == FeePayerType::SponsorCoSigned)
{
STAmount const sponsorReserve = accountReserve(ctx.view, payerSle, ctx.j);
maxSpendable = payerSle->getFieldAmount(sfBalance).xrp() - sponsorReserve.xrp();
auto const sponsorReserve = totalAccountReserve(ctx.view, payerSle);
maxSpendable = payerSle->getFieldAmount(sfBalance).xrp() - sponsorReserve;
}
else
{

View File

@@ -437,7 +437,7 @@ transferHelper(
return tecINTERNAL; // LCOV_EXCL_LINE
{
auto const reserve = accountReserve(psb, sleSrc, j, 0, 0);
auto const reserve = totalAccountReserve(psb, sleSrc);
auto const availableBalance = [&]() -> STAmount {
STAmount curBal = (*sleSrc)[sfBalance];

View File

@@ -657,8 +657,7 @@ AMMWithdraw::withdraw(
return tecINTERNAL; // LCOV_EXCL_LINE
auto const balance = (*sleAccount)[sfBalance]->xrp();
std::uint32_t const count =
ownerCount(view, sponsorSle ? sponsorSle : sleAccount, journal);
std::uint32_t const count = objectOwnerCount(sponsorSle ? sponsorSle : sleAccount);
// See also TrustSet::doApply() and MPTokenAuthorize::authorize()
if (count >= 2)
{

View File

@@ -636,7 +636,7 @@ Payment::doApply()
// the number of reserves in this ledger for this account that require a
// reserve.
auto const reserve = accountReserve(view(), sleSrc, j_);
auto const reserve = totalAccountReserve(view(), sleSrc);
// In a delegated payment, the fee payer is the delegated account,
// not the source account (accountID_).

View File

@@ -332,7 +332,7 @@ TrustSet::doApply()
if (!sponsorSle)
return sponsorSle.error(); // LCOV_EXCL_LINE
std::uint32_t const uOwnerCount = ownerCount(view(), *sponsorSle ? *sponsorSle : sle, j_);
std::uint32_t const uOwnerCount = objectOwnerCount(*sponsorSle ? *sponsorSle : sle);
bool const isSponsoredAndPreFunded = *sponsorSle && !isSponsorReserveCoSigning(ctx_.tx);
// If PreFunded Sponsor, it must be checked whether sufficient

View File

@@ -1094,8 +1094,7 @@ class LoanBroker_test : public beast::unit_test::Suite
env.close();
}
auto const amt =
env.balance(alice) - accountReserve(*env.current(), alice.id(), env.journal);
auto const amt = env.balance(alice) - accountReserve(*env.current(), alice.id());
env(pay(alice, issuer, amt));
// preclaim:: tecINSUFFICIENT_RESERVE

View File

@@ -861,7 +861,7 @@ protected:
auto const totalNeeded = state.totalValue + (serviceFee * state.paymentRemaining) +
(broker.asset.native() ? Number(
baseFee * state.paymentRemaining +
accountReserve(*env.current(), borrower.id(), env.journal))
accountReserve(*env.current(), borrower.id()))
: broker.asset(15).number());
auto const shortage = totalNeeded - borrowerBalance.number();
@@ -4545,8 +4545,7 @@ protected:
BrokerInfo const& brokerInfo,
jtx::Fee const& loanSetFee,
Number const& debtMaximumRequest) {
auto const amt =
env.balance(borrower) - accountReserve(*env.current(), borrower.id(), env.journal);
auto const amt = env.balance(borrower) - accountReserve(*env.current(), borrower.id());
env(pay(borrower, issuer, amt));
// tecINSUFFICIENT_RESERVE

View File

@@ -6,7 +6,10 @@
#include <xrpl/basics/base_uint.h>
#include <xrpl/json/json_value.h>
#include <xrpl/ledger/ReadView.h>
#include <xrpl/ledger/helpers/AccountRootHelpers.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/Quality.h>
#include <xrpl/protocol/STNumber.h>
#include <xrpl/protocol/Units.h>
@@ -516,6 +519,21 @@ txFee(Env const& env, std::uint16_t n);
PrettyAmount
xrpMinusFee(Env const& env, std::int64_t xrpAmount);
/** Returns the base reserve for a non-sponsored account with `count` owned objects. */
inline XRPAmount
baseAccountReserve(ReadView const& view, std::uint32_t count = 0)
{
auto const& fees = view.fees();
return fees.reserve + fees.increment * count;
}
/** Returns the total reserve for an account, read from the ledger. */
inline XRPAmount
accountReserve(ReadView const& view, AccountID const& id)
{
return totalAccountReserve(view, view.read(keylet::account(id)));
}
bool
expectHolding(
Env& env,