Skip to content

Commit

Permalink
check
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnxie999 committed Dec 4, 2024
1 parent 4a4acfc commit 876d941
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 21 deletions.
106 changes: 98 additions & 8 deletions src/test/app/LPTokenTransfer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ namespace test {
class LPTokenTransfer_test : public jtx::AMMTest
{
void
testRipplingFrozenAsset(FeatureBitset features)
testDirectStep(FeatureBitset features)
{
testcase("Rippling Frozen Asset");
testcase("DirectStep");

using namespace jtx;
Env env{*this, features};
Expand Down Expand Up @@ -100,9 +100,9 @@ class LPTokenTransfer_test : public jtx::AMMTest
}

void
testOrderBookFrozenAsset(FeatureBitset features)
testBookStep(FeatureBitset features)
{
testcase("Rippling Order Book");
testcase("BookStep");

using namespace jtx;
Env env{*this, features};
Expand All @@ -118,6 +118,7 @@ class LPTokenTransfer_test : public jtx::AMMTest
ammAlice.deposit(bob, 1'000);

auto const lpIssue = ammAlice.lptIssue();

env(offer(carol, XRP(10), STAmount{lpIssue, 10}), txflags(tfPassive));
env.close();
BEAST_EXPECT(expectOffers(env, carol, 1));
Expand All @@ -132,8 +133,8 @@ class LPTokenTransfer_test : public jtx::AMMTest
env.close();

// bob can still send to lptoken to carol even tho carol's USD is
// frozen, regardless of whether fixLPTokenTransfer is enabled or not
// Note: Deep freeze is not considered for LPToken transfer
// frozen, regardless of whether fixLPTokenTransfer is enabled or
// not Note: Deep freeze is not considered for LPToken transfer
env(pay(bob, carol, STAmount{lpIssue, 1}));
env.close();

Expand Down Expand Up @@ -193,6 +194,93 @@ class LPTokenTransfer_test : public jtx::AMMTest
// expectOffers(env, alice, 0) && expectOffers(env, carol, 0));
// });

void
testOfferCreation(FeatureBitset features)
{
testcase("Create offer");

using namespace jtx;
Env env{*this, features};

fund(
env,
gw,
{alice, bob, carol},
{USD(10'000), EUR(10'000)},
Fund::All);
AMM ammAlice(env, alice, USD(10'000), EUR(10'000));
ammAlice.deposit(carol, 1'000);
ammAlice.deposit(bob, 1'000);

auto const lpIssue = ammAlice.lptIssue();

if (features[fixLPTokenTransfer])
{
// gateway freezes carol's USD
env(trust(gw, carol["USD"](0), tfSetFreeze));
env.close();

env(offer(carol, XRP(10), STAmount{lpIssue, 10}),
txflags(tfPassive),
ter(tecUNFUNDED_OFFER));
env.close();
BEAST_EXPECT(expectOffers(env, carol, 0));

// gateway freezes carol's USD
env(trust(gw, carol["USD"](1'000'000'000), tfClearFreeze));
env.close();

env(offer(carol, XRP(10), STAmount{lpIssue, 10}),
txflags(tfPassive));
env.close();
BEAST_EXPECT(expectOffers(env, carol, 1));
}
else
{
env(offer(carol, XRP(10), STAmount{lpIssue, 10}),
txflags(tfPassive));
env.close();
BEAST_EXPECT(expectOffers(env, carol, 1));
}
}

void
testCheck(FeatureBitset features)
{
testcase("Check");

using namespace jtx;
Env env{*this, features};

fund(
env,
gw,
{alice, bob, carol},
{USD(10'000), EUR(10'000)},
Fund::All);
AMM ammAlice(env, alice, USD(10'000), EUR(10'000));
ammAlice.deposit(carol, 1'000);
ammAlice.deposit(bob, 1'000);

auto const lpIssue = ammAlice.lptIssue();

// gateway freezes carol's USD
env(trust(gw, carol["USD"](0), tfSetFreeze));
env.close();

uint256 const chkId{keylet::check(carol, env.seq(carol)).key};
env(check::create(carol, bob, STAmount{lpIssue, 10}));
env.close();

if (features[fixLPTokenTransfer])
env(check::cash(bob, chkId, STAmount{lpIssue, 10}),
ter(tecPATH_PARTIAL));
else
env(check::cash(bob, chkId, STAmount{lpIssue, 10}));

env.close();
}

public:
void
run() override
Expand All @@ -202,8 +290,10 @@ class LPTokenTransfer_test : public jtx::AMMTest

for (auto const features : {all, all - fixLPTokenTransfer})
{
// testRipplingFrozenAsset(features);
testOrderBookFrozenAsset(features);
testDirectStep(features);
testBookStep(features);
testOfferCreation(features);
testCheck(features);
}
}
};
Expand Down
35 changes: 22 additions & 13 deletions src/xrpld/ledger/detail/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ accountHolds(

// IOU: Return balance on trust line modulo freeze
auto const sle = view.read(keylet::line(account, issuer, currency));
auto const sleIssuer = view.read(keylet::account(issuer));
if (!sle || !sleIssuer)
if (!sle)
{
amount.clear(Issue{currency, issuer});
}
Expand All @@ -316,22 +315,31 @@ accountHolds(

// if it's a LPToken, also need to check if issuers of the asset pair
// has frozen holder's trustline
if (view.rules().enabled(fixLPTokenTransfer) &&
sleIssuer->isFieldPresent(sfAMMID))
if (view.rules().enabled(fixLPTokenTransfer))
{
auto const sleAmm = view.read(keylet::amm((*sleIssuer)[sfAMMID]));

assert(sleAmm);

if ((zeroIfFrozen == fhZERO_IF_FROZEN) &&
isLPTokenFrozen(
view, account, (*sleAmm)[sfAsset], (*sleAmm)[sfAsset2]))
auto const sleIssuer = view.read(keylet::account(issuer));
if (!sleIssuer)
{
amount.clear(Issue{currency, issuer});
}
else if (sleIssuer->isFieldPresent(sfAMMID))
{
auto const sleAmm =
view.read(keylet::amm((*sleIssuer)[sfAMMID]));

assert(sleAmm);

if ((zeroIfFrozen == fhZERO_IF_FROZEN) &&
isLPTokenFrozen(
view, account, (*sleAmm)[sfAsset], (*sleAmm)[sfAsset2]))
{
amount.clear(Issue{currency, issuer});
}
}
}
}
JLOG(j.trace()) << "accountHolds:" << " account=" << to_string(account)
JLOG(j.trace()) << "accountHolds:"
<< " account=" << to_string(account)
<< " amount=" << amount.getFullText();

return view.balanceHook(account, issuer, amount);
Expand Down Expand Up @@ -480,7 +488,8 @@ xrpLiquid(
STAmount const amount =
(balance < reserve) ? STAmount{0} : balance - reserve;

JLOG(j.trace()) << "accountHolds:" << " account=" << to_string(id)
JLOG(j.trace()) << "accountHolds:"
<< " account=" << to_string(id)
<< " amount=" << amount.getFullText()
<< " fullBalance=" << fullBalance.getFullText()
<< " balance=" << balance.getFullText()
Expand Down

0 comments on commit 876d941

Please sign in to comment.