diff --git a/include/xrpl/ledger/helpers/AccountRootHelpers.h b/include/xrpl/ledger/helpers/AccountRootHelpers.h index 30e988c04a..25cb89ef37 100644 --- a/include/xrpl/ledger/helpers/AccountRootHelpers.h +++ b/include/xrpl/ledger/helpers/AccountRootHelpers.h @@ -61,7 +61,7 @@ public: // // @param ownerCountAdj positive to add to count, negative to reduce count. [[nodiscard]] XRPAmount - xrpLiquid(std::int32_t ownerCountAdj, beast::Journal j); + xrpLiquid(std::int32_t ownerCountAdj, beast::Journal j) const; /** Adjust the owner count up or down. */ void diff --git a/include/xrpl/ledger/helpers/RippleStateHelpers.h b/include/xrpl/ledger/helpers/RippleStateHelpers.h index 1ab9fafd17..788da9af1c 100644 --- a/include/xrpl/ledger/helpers/RippleStateHelpers.h +++ b/include/xrpl/ledger/helpers/RippleStateHelpers.h @@ -138,16 +138,16 @@ trustCreate( bool const bSrcHigh, AccountID const& uSrcAccountID, AccountID const& uDstAccountID, - uint256 const& uIndex, // --> ripple state entry - WrappedAccountRoot const& wrappedAcct, // --> the account being set. - bool const bAuth, // --> authorize account. - bool const bNoRipple, // --> others cannot ripple through - bool const bFreeze, // --> funds cannot leave - bool bDeepFreeze, // --> can neither receive nor send funds - STAmount const& saBalance, // --> balance of account being set. - // Issuer should be noAccount() - STAmount const& saLimit, // --> limit for account being set. - // Issuer should be the account being set. + uint256 const& uIndex, // --> ripple state entry + WrappedAccountRoot& wrappedAcct, // --> the account being set. + bool const bAuth, // --> authorize account. + bool const bNoRipple, // --> others cannot ripple through + bool const bFreeze, // --> funds cannot leave + bool bDeepFreeze, // --> can neither receive nor send funds + STAmount const& saBalance, // --> balance of account being set. + // Issuer should be noAccount() + STAmount const& saLimit, // --> limit for account being set. + // Issuer should be the account being set. std::uint32_t uQualityIn, std::uint32_t uQualityOut, beast::Journal j); diff --git a/include/xrpl/ledger/helpers/WrappedSLEBase.h b/include/xrpl/ledger/helpers/WrappedSLEBase.h index 72864bd8ac..e1d95554f9 100644 --- a/include/xrpl/ledger/helpers/WrappedSLEBase.h +++ b/include/xrpl/ledger/helpers/WrappedSLEBase.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include @@ -7,9 +9,6 @@ namespace xrpl { -class ReadView; -class ApplyView; - /** * Base class for all ledger entry view classes. * @@ -78,7 +77,7 @@ public: bool canModify() const { - return applyView_ != nullptr; + return applyView_ != nullptr && mutableSle_ != nullptr; } /** Returns the apply view for write operations @@ -98,6 +97,7 @@ public: STLedgerEntry* operator->() { + XRPL_ASSERT(canModify(), "xrpl::WrappedSLEBase::operator* : can modify"); return mutableSle_.get(); } @@ -110,6 +110,7 @@ public: STLedgerEntry& operator*() { + XRPL_ASSERT(canModify(), "xrpl::WrappedSLEBase::operator* : can modify"); return *mutableSle_; } diff --git a/src/libxrpl/ledger/helpers/AccountRootHelpers.cpp b/src/libxrpl/ledger/helpers/AccountRootHelpers.cpp index b13705efe7..5b213e1f57 100644 --- a/src/libxrpl/ledger/helpers/AccountRootHelpers.cpp +++ b/src/libxrpl/ledger/helpers/AccountRootHelpers.cpp @@ -62,7 +62,7 @@ confineOwnerCount( } XRPAmount -WrappedAccountRoot::xrpLiquid(std::int32_t ownerCountAdj, beast::Journal j) +WrappedAccountRoot::xrpLiquid(std::int32_t ownerCountAdj, beast::Journal j) const { // Return balance minus reserve std::uint32_t const ownerCount = confineOwnerCount( @@ -99,16 +99,14 @@ WrappedAccountRoot::transferRate() const void WrappedAccountRoot::adjustOwnerCount(std::int32_t amount, beast::Journal j) { - if (!sle_) - return; + XRPL_ASSERT(canModify(), "xrpl::adjustOwnerCount : can modify"); XRPL_ASSERT(amount, "xrpl::adjustOwnerCount : nonzero amount input"); - std::uint32_t const current{sle_->getFieldU32(sfOwnerCount)}; - AccountID const id = (*sle_)[sfAccount]; + std::uint32_t const current{mutableSle_->getFieldU32(sfOwnerCount)}; + AccountID const id = (*mutableSle_)[sfAccount]; std::uint32_t const adjusted = confineOwnerCount(current, amount, id, j); applyView_->adjustOwnerCountHook(id_, current, adjusted); - auto mutable_sle = mutableSle(); - mutable_sle->at(sfOwnerCount) = adjusted; - applyView_->update(mutable_sle); + mutableSle_->at(sfOwnerCount) = adjusted; + applyView_->update(mutableSle_); } AccountID diff --git a/src/libxrpl/ledger/helpers/RippleStateHelpers.cpp b/src/libxrpl/ledger/helpers/RippleStateHelpers.cpp index 21fe4f604b..2d8c662b85 100644 --- a/src/libxrpl/ledger/helpers/RippleStateHelpers.cpp +++ b/src/libxrpl/ledger/helpers/RippleStateHelpers.cpp @@ -424,7 +424,7 @@ issueIOU( final_balance.setIssuer(noAccount()); - WrappedAccountRoot const receiverAccount(account, &view); + WrappedAccountRoot receiverAccount(account, &view); if (!receiverAccount) return tefINTERNAL; // LCOV_EXCL_LINE diff --git a/src/test/app/Invariants_test.cpp b/src/test/app/Invariants_test.cpp index e17ef1fd55..dc9afaf91b 100644 --- a/src/test/app/Invariants_test.cpp +++ b/src/test/app/Invariants_test.cpp @@ -260,17 +260,17 @@ class Invariants_test : public beast::unit_test::suite [&](Account const& A1, Account const& A2, ApplyContext& ac) { // Increment A1's owner count, then delete A1 auto const a1 = A1.id(); - auto const sleA1 = ac.view().peek(keylet::account(a1)); - if (!sleA1) + WrappedAccountRoot wrappedA1(a1, &ac.view()); + if (!wrappedA1) return false; // Clear the balance so the "account deletion left behind a // non-zero balance" check doesn't trip earlier than the desired // check. - sleA1->at(sfBalance) = beast::zero; - BEAST_EXPECT(sleA1->at(sfOwnerCount) == 0); - adjustOwnerCount(ac.view(), sleA1, 1, ac.journal); + wrappedA1->at(sfBalance) = beast::zero; + BEAST_EXPECT(wrappedA1->at(sfOwnerCount) == 0); + wrappedA1.adjustOwnerCount(1, ac.journal); - ac.view().erase(sleA1); + ac.view().erase(wrappedA1.mutableSle()); return true; }, diff --git a/src/test/ledger/View_test.cpp b/src/test/ledger/View_test.cpp index bae29445c2..3cb945f30c 100644 --- a/src/test/ledger/View_test.cpp +++ b/src/test/ledger/View_test.cpp @@ -844,13 +844,15 @@ class View_test : public beast::unit_test::suite auto rdView = env.closed(); // Test with no rate set on gw1. - BEAST_EXPECT(transferRate(*rdView, gw1) == parityRate); + WrappedAccountRoot issuer(gw1, rdView.get()); + BEAST_EXPECT(issuer.transferRate() == parityRate); env(rate(gw1, 1.02)); env.close(); rdView = env.closed(); - BEAST_EXPECT(transferRate(*rdView, gw1) == Rate{1020000000}); + WrappedAccountRoot issuerV2(gw1, rdView.get()); + BEAST_EXPECT(issuerV2.transferRate() == Rate{1020000000}); } void