From f6098fd80bb7c9ced3cdfd7b61d6d26599e1766b Mon Sep 17 00:00:00 2001 From: Ed Hennis Date: Tue, 21 Oct 2025 20:01:55 -0400 Subject: [PATCH] Expand use of Asset::integral() --- include/xrpl/protocol/STAmount.h | 11 ++++++++++- src/libxrpl/protocol/STAmount.cpp | 4 ++-- src/test/app/Loan_test.cpp | 6 ++---- src/test/app/Vault_test.cpp | 6 +++--- src/test/jtx/amount.h | 12 ++++++++++++ src/xrpld/app/tx/detail/LoanManage.cpp | 2 +- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/include/xrpl/protocol/STAmount.h b/include/xrpl/protocol/STAmount.h index 560059e49a..eb74a56805 100644 --- a/include/xrpl/protocol/STAmount.h +++ b/include/xrpl/protocol/STAmount.h @@ -174,6 +174,9 @@ public: int exponent() const noexcept; + bool + integral() const noexcept; + bool native() const noexcept; @@ -454,6 +457,12 @@ STAmount::exponent() const noexcept return mOffset; } +inline bool +STAmount::integral() const noexcept +{ + return mAsset.integral(); +} + inline bool STAmount::native() const noexcept { @@ -735,7 +744,7 @@ roundToAsset( { NumberRoundModeGuard mg(rounding); STAmount const ret{asset, value}; - if (ret.asset().native() || !ret.asset().holds()) + if (ret.integral()) return ret; // Not that the ctor will round integral types (XRP, MPT) via canonicalize, // so no extra work is needed for those. diff --git a/src/libxrpl/protocol/STAmount.cpp b/src/libxrpl/protocol/STAmount.cpp index 74f4576831..289abb0707 100644 --- a/src/libxrpl/protocol/STAmount.cpp +++ b/src/libxrpl/protocol/STAmount.cpp @@ -321,7 +321,7 @@ STAmount::xrp() const IOUAmount STAmount::iou() const { - if (native() || !holds()) + if (integral()) Throw("Cannot return non-IOU STAmount as IOUAmount"); auto mantissa = static_cast(mValue); @@ -1513,7 +1513,7 @@ STAmount roundToScale(STAmount value, std::int32_t scale, Number::rounding_mode rounding) { // Nothing to do for intgral types. - if (value.asset().native() || !value.asset().holds()) + if (value.integral()) return value; // If the value's exponent is greater than or equal to the scale, then diff --git a/src/test/app/Loan_test.cpp b/src/test/app/Loan_test.cpp index a4aaf47272..d5b1d6bb41 100644 --- a/src/test/app/Loan_test.cpp +++ b/src/test/app/Loan_test.cpp @@ -193,9 +193,7 @@ class Loan_test : public beast::unit_test::suite auto const available = vaultSle->at(sfAssetsAvailable); env.test.BEAST_EXPECT( total == available || - (!broker.asset.raw().native() && - broker.asset.raw().holds() && - available != 0 && + (!broker.asset.integral() && available != 0 && ((total - available) / available < Number(1, -6)))); env.test.BEAST_EXPECT( @@ -1750,7 +1748,7 @@ class Loan_test : public beast::unit_test::suite BEAST_EXPECT( paymentComponents.final || diff == beast::zero || (diff > beast::zero && - ((broker.asset.raw().integral() && + ((broker.asset.integral() && (static_cast(diff) < 3)) || (totalDue.exponent() - diff.exponent() > 8)))); diff --git a/src/test/app/Vault_test.cpp b/src/test/app/Vault_test.cpp index b5f94f38d1..ce4f88bb38 100644 --- a/src/test/app/Vault_test.cpp +++ b/src/test/app/Vault_test.cpp @@ -84,14 +84,14 @@ class Vault_test : public beast::unit_test::suite this]() -> std::tuple { auto const vault = env.le(keylet); BEAST_EXPECT(vault != nullptr); - if (asset.raw().holds() && !asset.raw().native()) + if (!asset.integral()) BEAST_EXPECT(vault->at(sfScale) == 6); else BEAST_EXPECT(vault->at(sfScale) == 0); auto const shares = env.le(keylet::mptIssuance(vault->at(sfShareMPTID))); BEAST_EXPECT(shares != nullptr); - if (asset.raw().holds() && !asset.raw().native()) + if (!asset.integral()) BEAST_EXPECT(shares->at(sfAssetScale) == 6); else BEAST_EXPECT(shares->at(sfAssetScale) == 0); @@ -446,7 +446,7 @@ class Vault_test : public beast::unit_test::suite } } - if (!asset.raw().native() && asset.raw().holds()) + if (!asset.integral()) { testcase(prefix + " temporary authorization for 3rd party"); env(trust(erin, asset(1000))); diff --git a/src/test/jtx/amount.h b/src/test/jtx/amount.h index f413d87c5a..f25f292531 100644 --- a/src/test/jtx/amount.h +++ b/src/test/jtx/amount.h @@ -233,6 +233,18 @@ public: return {asset_}; } + bool + integral() const + { + return asset_.integral(); + } + + bool + native() const + { + return asset_.native(); + } + template bool holds() const diff --git a/src/xrpld/app/tx/detail/LoanManage.cpp b/src/xrpld/app/tx/detail/LoanManage.cpp index f0a69820a6..00e742f4bb 100644 --- a/src/xrpld/app/tx/detail/LoanManage.cpp +++ b/src/xrpld/app/tx/detail/LoanManage.cpp @@ -203,7 +203,7 @@ LoanManage::defaultLoan( // Capital and any unclaimed funds amount: vaultAssetsAvailableProxy += defaultCovered; if (*vaultAssetsAvailableProxy > *vaultAssetsTotalProxy && - !vaultAsset.native() && vaultAsset.holds()) + !vaultAsset.integral()) { auto const difference = vaultAssetsAvailableProxy - vaultAssetsTotalProxy;