refactor: Add std::optional overload for isZeroId

Addresses bthomee's review suggestion: about a third of isZeroId call
sites are the optional-field pattern 'x && isZeroId(*x)'. Add an
overload taking std::optional<T> const& and collapse the 5 direct
call sites (Payment, LoanSet, LoanBrokerCoverClawback, OfferCreate,
PermissionedDomainSet). The remaining if-let uses are left alone
because collapsing them would move unrelated code out of the outer
block.

The negated helpers (!isPositiveAmount etc.) are intentionally not
overloaded: with an optional overload, !isPositiveAmount(opt) would
be true when the optional is empty, which is the opposite of the
existing 'opt && !isPositiveAmount(*opt)' semantics.
This commit is contained in:
Mayukha Vadari
2026-09-23 01:58:38 +05:30
parent 6a7d98621d
commit a1a86bd362
6 changed files with 16 additions and 5 deletions

View File

@@ -5,6 +5,7 @@
#include <xrpl/protocol/UintTypes.h>
#include <cstddef>
#include <optional>
namespace xrpl {
@@ -41,6 +42,16 @@ isZeroId(T const& id)
return id == beast::kZero;
}
// Optional-field overload: true iff the field is present and its value is
// unset/zero. Simplifies the `field && isZeroId(*field)` pattern into a
// single call.
template <class T>
inline bool
isZeroId(std::optional<T> const& id)
{
return id.has_value() && isZeroId(*id);
}
// Checks whether an amount is a strictly positive XRP amount.
inline bool
isPositiveXRPAmount(STAmount const& amount)

View File

@@ -101,7 +101,7 @@ OfferCreate::preflight(PreflightContext const& ctx)
// A zero DomainID is invalid for a PermissionedDomain ledger entry because
// keylet::permissionedDomain(uint256) uses the DomainID as the ledger key.
if (auto const domainID = tx[~sfDomainID];
ctx.rules.enabled(fixCleanup3_2_0) && domainID && isZeroId(*domainID))
ctx.rules.enabled(fixCleanup3_2_0) && isZeroId(domainID))
return temMALFORMED;
bool const bImmediateOrCancel(tx.isFlag(tfImmediateOrCancel));

View File

@@ -49,7 +49,7 @@ LoanBrokerCoverClawback::preflight(PreflightContext const& ctx)
if (!brokerID && !amount)
return temINVALID;
if (brokerID && isZeroId(*brokerID))
if (isZeroId(brokerID))
return temINVALID;
if (amount)

View File

@@ -147,7 +147,7 @@ LoanSet::preflight(PreflightContext const& ctx)
return *ret;
}
if (auto const brokerID = ctx.tx[~sfLoanBrokerID]; brokerID && isZeroId(*brokerID))
if (auto const brokerID = ctx.tx[~sfLoanBrokerID]; isZeroId(brokerID))
return temINVALID;
return tesSUCCESS;

View File

@@ -144,7 +144,7 @@ Payment::preflight(PreflightContext const& ctx)
// A zero DomainID is invalid for a PermissionedDomain ledger entry because
// keylet::permissionedDomain(uint256) uses the DomainID as the ledger key.
if (auto const domainID = tx[~sfDomainID];
ctx.rules.enabled(fixCleanup3_2_0) && domainID && isZeroId(*domainID))
ctx.rules.enabled(fixCleanup3_2_0) && isZeroId(domainID))
return temMALFORMED;
bool const partialPaymentAllowed = tx.isFlag(tfPartialPayment);

View File

@@ -40,7 +40,7 @@ PermissionedDomainSet::preflight(PreflightContext const& ctx)
return err;
auto const domain = ctx.tx.at(~sfDomainID);
if (domain && isZeroId(*domain))
if (isZeroId(domain))
return temMALFORMED;
return tesSUCCESS;