Skip to content

Commit

Permalink
Change access to Issue/MPTIssue in STAmount via get<>() methods only.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Sep 16, 2024
1 parent 08f4cb2 commit eda3de3
Show file tree
Hide file tree
Showing 62 changed files with 468 additions and 398 deletions.
2 changes: 1 addition & 1 deletion include/xrpl/protocol/AmountConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ getIssue(T const& amt)
else if constexpr (std::is_same_v<XRPAmount, T>)
return xrpIssue();
else if constexpr (std::is_same_v<STAmount, T>)
return amt.issue();
return amt.template get<Issue>();
else
{
constexpr bool alwaysFalse = !std::is_same_v<T, T>;
Expand Down
6 changes: 6 additions & 0 deletions include/xrpl/protocol/Issue.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class Issue
AccountID const&
getIssuer() const;

void
setIssuer(AccountID const& issuer);

Currency const&
getCurrency() const;

std::string
getText() const;
};
Expand Down
30 changes: 7 additions & 23 deletions include/xrpl/protocol/STAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,11 @@ class STAmount final : public STBase, public CountedObject<STAmount>
constexpr TIss const&
get() const;

Issue const&
issue() const;
template <ValidIssueType TIss>
TIss&
get();

// These three are deprecated
Currency const&
getCurrency() const;

AccountID const&
getIssuer() const;

Expand Down Expand Up @@ -247,9 +245,6 @@ class STAmount final : public STBase, public CountedObject<STAmount>
void
clear(Asset const& asset);

void
setIssuer(AccountID const& uIssuer);

/** Set the Issue for this amount. */
void
setIssue(Asset const& asset);
Expand Down Expand Up @@ -480,16 +475,11 @@ STAmount::get() const
return mAsset.get<TIss>();
}

inline Issue const&
STAmount::issue() const
{
return get<Issue>();
}

inline Currency const&
STAmount::getCurrency() const
template <ValidIssueType TIss>
TIss&
STAmount::get()
{
return mAsset.get<Issue>().currency;
return mAsset.get<TIss>();
}

inline AccountID const&
Expand Down Expand Up @@ -563,12 +553,6 @@ STAmount::clear(Asset const& asset)
clear();
}

inline void
STAmount::setIssuer(AccountID const& uIssuer)
{
mAsset.get<Issue>().account = uIssuer;
}

inline STAmount const&
STAmount::value() const noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion src/libxrpl/protocol/AMMCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ invalidAMMAmount(
std::optional<std::pair<Issue, Issue>> const& pair,
bool validZero)
{
if (auto const res = invalidAMMAsset(amount.issue(), pair))
if (auto const res = invalidAMMAsset(amount.get<Issue>(), pair))
return res;
if (amount < beast::zero || (!validZero && amount == beast::zero))
return temBAD_AMOUNT;
Expand Down
12 changes: 12 additions & 0 deletions src/libxrpl/protocol/Issue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ Issue::getIssuer() const
return account;
}

void
Issue::setIssuer(AccountID const& accnt)
{
account = accnt;
}

Currency const&
Issue::getCurrency() const
{
return currency;
}

std::string
Issue::getText() const
{
Expand Down
5 changes: 3 additions & 2 deletions src/libxrpl/protocol/Rate2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ divide(STAmount const& amount, Rate const& rate)
if (rate == parityRate)
return amount;

return divide(amount, detail::as_amount(rate), amount.issue());
return divide(amount, detail::as_amount(rate), amount.get<Issue>());
}

STAmount
Expand All @@ -101,7 +101,8 @@ divideRound(STAmount const& amount, Rate const& rate, bool roundUp)
if (rate == parityRate)
return amount;

return divRound(amount, detail::as_amount(rate), amount.issue(), roundUp);
return divRound(
amount, detail::as_amount(rate), amount.get<Issue>(), roundUp);
}

STAmount
Expand Down
11 changes: 6 additions & 5 deletions src/test/app/AMMCalc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class AMMCalc_test : public beast::unit_test::suite
toString(STAmount const& a)
{
std::stringstream str;
str << a.getText() << "/" << to_string(a.issue().currency);
str << a.getText() << "/" << to_string(a.get<Issue>().currency);
return str.str();
}

Expand All @@ -187,8 +187,9 @@ class AMMCalc_test : public beast::unit_test::suite
if (a == b)
return amt;
if (amt.native())
return toSTAmount(mulRatio(amt.xrp(), a, b, round), amt.issue());
return toSTAmount(mulRatio(amt.iou(), a, b, round), amt.issue());
return toSTAmount(
mulRatio(amt.xrp(), a, b, round), amt.get<Issue>());
return toSTAmount(mulRatio(amt.iou(), a, b, round), amt.get<Issue>());
}

void
Expand All @@ -204,7 +205,7 @@ class AMMCalc_test : public beast::unit_test::suite
int limitingStep = vp.size();
STAmount limitStepOut{};
auto trate = [&](auto const& amt) {
auto const currency = to_string(amt.issue().currency);
auto const currency = to_string(amt.template get<Issue>().currency);
return rates.find(currency) != rates.end() ? rates.at(currency)
: QUALITY_ONE;
};
Expand Down Expand Up @@ -269,7 +270,7 @@ class AMMCalc_test : public beast::unit_test::suite
int limitingStep = 0;
STAmount limitStepIn{};
auto trate = [&](auto const& amt) {
auto const currency = to_string(amt.issue().currency);
auto const currency = to_string(amt.template get<Issue>().currency);
return rates.find(currency) != rates.end() ? rates.at(currency)
: QUALITY_ONE;
};
Expand Down
8 changes: 4 additions & 4 deletions src/test/app/AMM_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ struct AMM_test : public jtx::AMMTest

// Make sure asset comparison works.
BEAST_EXPECT(
STIssue(sfAsset, STAmount(XRP(2'000)).issue()) ==
STIssue(sfAsset, STAmount(XRP(2'000)).issue()));
STIssue(sfAsset, STAmount(XRP(2'000)).get<Issue>()) ==
STIssue(sfAsset, STAmount(XRP(2'000)).get<Issue>()));
BEAST_EXPECT(
STIssue(sfAsset, STAmount(XRP(2'000)).issue()) !=
STIssue(sfAsset, STAmount(USD(2'000)).issue()));
STIssue(sfAsset, STAmount(XRP(2'000)).get<Issue>()) !=
STIssue(sfAsset, STAmount(USD(2'000)).get<Issue>()));
}

void
Expand Down
4 changes: 2 additions & 2 deletions src/test/app/Check_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2156,8 +2156,8 @@ class Check_test : public beast::unit_test::suite
return;

BEAST_EXPECT(
offerAmount.issue().account ==
checkAmount.issue().account);
offerAmount.get<Issue>().account ==
checkAmount.get<Issue>().account);
BEAST_EXPECT(
offerAmount.negative() == checkAmount.negative());
BEAST_EXPECT(
Expand Down
12 changes: 7 additions & 5 deletions src/test/app/Offer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2302,19 +2302,21 @@ class OfferBaseUtil_test : public beast::unit_test::suite
jtx::Account const& account,
jtx::PrettyAmount const& expectBalance)
{
auto const sleTrust =
env.le(keylet::line(account.id(), expectBalance.value().issue()));
auto const sleTrust = env.le(
keylet::line(account.id(), expectBalance.value().get<Issue>()));
BEAST_EXPECT(sleTrust);
if (sleTrust)
{
Issue const issue = expectBalance.value().issue();
Issue const issue = expectBalance.value().get<Issue>();
bool const accountLow = account.id() < issue.account;

STAmount low{issue};
STAmount high{issue};

low.setIssuer(accountLow ? account.id() : issue.account);
high.setIssuer(accountLow ? issue.account : account.id());
low.get<Issue>().setIssuer(
accountLow ? account.id() : issue.account);
high.get<Issue>().setIssuer(
accountLow ? issue.account : account.id());

BEAST_EXPECT(sleTrust->getFieldAmount(sfLowLimit) == low);
BEAST_EXPECT(sleTrust->getFieldAmount(sfHighLimit) == high);
Expand Down
12 changes: 6 additions & 6 deletions src/test/app/ReducedOffer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ class ReducedOffer_test : public beast::unit_test::suite
mantissaReduce += 20'000'000ull)
{
STAmount aliceUSD{
bobsOffer.out.issue(),
bobsOffer.out.get<Issue>(),
bobsOffer.out.mantissa() - mantissaReduce,
bobsOffer.out.exponent()};
STAmount aliceXRP{
bobsOffer.in.issue(), bobsOffer.in.mantissa() - 1};
bobsOffer.in.get<Issue>(), bobsOffer.in.mantissa() - 1};
Amounts alicesOffer{aliceUSD, aliceXRP};
blockedCount += exerciseOfferPair(alicesOffer, bobsOffer);
}
Expand Down Expand Up @@ -356,11 +356,11 @@ class ReducedOffer_test : public beast::unit_test::suite
mantissaReduce += 20'000'000ull)
{
STAmount bobUSD{
aliceOffer.out.issue(),
aliceOffer.out.get<Issue>(),
aliceOffer.out.mantissa() - mantissaReduce,
aliceOffer.out.exponent()};
STAmount bobXRP{
aliceOffer.in.issue(), aliceOffer.in.mantissa() - 1};
aliceOffer.in.get<Issue>(), aliceOffer.in.mantissa() - 1};
Amounts bobsOffer{bobUSD, bobXRP};

blockedCount += exerciseOfferPair(aliceOffer, bobsOffer);
Expand Down Expand Up @@ -724,7 +724,7 @@ class ReducedOffer_test : public beast::unit_test::suite
if (badRate == 0)
{
STAmount const tweakedTakerGets(
aliceReducedOffer.in.issue(),
aliceReducedOffer.in.get<Issue>(),
aliceReducedOffer.in.mantissa() + 1,
aliceReducedOffer.in.exponent(),
aliceReducedOffer.in.negative());
Expand Down Expand Up @@ -763,7 +763,7 @@ class ReducedOffer_test : public beast::unit_test::suite
unsigned int blockedCount = 0;
{
STAmount increaseGets = USD(0);
STAmount const step(increaseGets.issue(), 1, -8);
STAmount const step(increaseGets.get<Issue>(), 1, -8);
for (unsigned int i = 0; i < loopCount; ++i)
{
blockedCount += exerciseOfferTrio(
Expand Down
2 changes: 1 addition & 1 deletion src/test/app/Taker_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class Taker_test : public beast::unit_test::suite
{
std::string txt = amount.getText();
txt += "/";
txt += to_string(amount.issue().currency);
txt += to_string(amount.get<Issue>().currency);
return txt;
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/app/TheoreticalQuality_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class TheoreticalQuality_test : public beast::unit_test::suite

auto const sendMaxIssue = [&rcp]() -> std::optional<Issue> {
if (rcp.sendMax)
return rcp.sendMax->issue();
return rcp.sendMax->get<Issue>();
return std::nullopt;
}();

Expand All @@ -260,7 +260,7 @@ class TheoreticalQuality_test : public beast::unit_test::suite
sb,
rcp.srcAccount,
rcp.dstAccount,
rcp.dstAmt.issue(),
rcp.dstAmt.get<Issue>(),
/*limitQuality*/ std::nullopt,
sendMaxIssue,
rcp.paths,
Expand Down
Loading

0 comments on commit eda3de3

Please sign in to comment.