Skip to content

Commit

Permalink
Add zero tokens variant
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Nov 11, 2024
1 parent 71ec9cb commit 1ef2ce2
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/xrpld/app/tx/detail/InvariantCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1204,11 +1204,19 @@ ValidAMM::finalize(
NumberRoundModeGuard g(Number::upward);
auto const res = root2(amount * amount2);
if (res >= *lptAMMBalance_)
return true;
{
if (*lptAMMBalance_ > beast::zero)
return true;
// last withdraw may have resulted in an empty AMM
if (txType == ttAMM_WITHDRAW &&
*lptAMMBalance_ == beast::zero && amount == beast::zero &&
amount2 == beast::zero)
return true;
}

std::string const txt =
txType == ttAMM_DEPOSIT ? "AMMDeposit" : "AMMWithdraw";
JLOG(j.trace())
JLOG(j.error())
<< txt << " invariant failed: " << amount << " " << amount2
<< " " << res << " " << lptAMMBalance_->getText();
return false;
Expand All @@ -1222,11 +1230,12 @@ ValidAMM::finalize(
tx[sfAmount2].get<Issue>(),
fhIGNORE_FREEZE,
j);
if (ammLPTokens(amount, amount2, lptAMMBalance_->issue()) ==
*lptAMMBalance_)
if (*lptAMMBalance_ > beast::zero &&
ammLPTokens(amount, amount2, lptAMMBalance_->issue()) ==
*lptAMMBalance_)
return true;

JLOG(j.trace()) << "AMMCreate invariant failed: " << amount << " "
JLOG(j.error()) << "AMMCreate invariant failed: " << amount << " "
<< amount2 << " " << *lptAMMBalance_;
return false;
}
Expand All @@ -1241,10 +1250,22 @@ ValidAMM::finalize(
if (auto const root = _view.read(keylet::account(k));
root && root->isFieldPresent(sfAMMID))
{
if (auto const amm =
_view.read(keylet::amm((*root)[sfAMMID]));
amm && (*amm)[sfLPTokenBalance] <= beast::zero)
{
// LCOV_EXCL_START
JLOG(j.error())
<< "AMM swap invariant failed: invalid token "
"balance "
<< to_string(k) << " " << (*amm)[sfLPTokenBalance];
return false;
// LCOV_EXCL_STOP
}
// AMM must have two balances - one for each pool side
if (!balanceAfter_.contains(k) || v.size() != 2)
{
JLOG(j.trace())
JLOG(j.error())
<< "AMM swap invariant failed: inconsistent assets "
<< to_string(k) << " " << v.size();
return false;
Expand All @@ -1255,7 +1276,7 @@ ValidAMM::finalize(
if (!balanceAfter_[k].contains(asset) ||
!balanceAfter_[k].contains(asset2))
{
JLOG(j.trace())
JLOG(j.error())
<< "AMM swap invariant failed: inconsistent assets "
<< to_string(k) << " " << asset << " " << asset2;
return false;
Expand All @@ -1269,7 +1290,7 @@ ValidAMM::finalize(
balanceAfter_[k][asset] * balanceAfter_[k][asset2];
if (productAfter < productBefore)
{
JLOG(j.trace())
JLOG(j.error())
<< "AMM swap invariant failed: old balances "
<< balanceBefore_[k][asset].getText() << " "
<< balanceBefore_[k][asset2].getText()
Expand Down

0 comments on commit 1ef2ce2

Please sign in to comment.