mirror of
https://github.com/XRPLF/rippled.git
synced 2026-09-26 23:19:07 +00:00
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:
@@ -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)
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user