Skip to content

Commit

Permalink
Merge pull request stellar#3212 from sisuresh/large-withdraw-test
Browse files Browse the repository at this point in the history
Additional CAP-0038 deposit/withdraw test

Reviewed-by: MonsieurNicolas
  • Loading branch information
latobarita authored Sep 24, 2021
2 parents 879789d + 6ef240c commit e381447
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
80 changes: 80 additions & 0 deletions src/test/TxTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1459,5 +1459,85 @@ executeUpgrade(Application& app, LedgerUpgrade const& lupgrade)
{
return executeUpgrades(app, {toUpgradeType(lupgrade)});
};

// trades is a vector of pairs, where the bool indicates if assetA or assetB is
// sent in the payment, and the int64_t is the amount
void
depositTradeWithdrawTest(Application& app, TestAccount& root, int depositSize,
std::vector<std::pair<bool, int64_t>> const& trades)
{
struct Deposit
{
TestAccount acc;
int64_t numPoolShares;
};
std::vector<Deposit> deposits;

int64_t total = 0;

auto cur1 = makeAsset(root, "CUR1");
auto cur2 = makeAsset(root, "CUR2");
auto share12 =
makeChangeTrustAssetPoolShare(cur1, cur2, LIQUIDITY_POOL_FEE_V18);
auto pool12 = xdrSha256(share12.liquidityPool());

auto deposit = [&](int accNum, int size) {
auto acc = root.create(fmt::format("account{}", accNum),
app.getLedgerManager().getLastMinBalance(10));
acc.changeTrust(cur1, INT64_MAX);
acc.changeTrust(cur2, INT64_MAX);
acc.changeTrust(share12, INT64_MAX);

root.pay(acc, cur1, size);
root.pay(acc, cur2, size);

acc.liquidityPoolDeposit(pool12, size, size, Price{1, INT32_MAX},
Price{INT32_MAX, 1});

total += size;

checkLiquidityPool(app, pool12, total, total, total, accNum + 1);

deposits.emplace_back(Deposit{acc, size});
};

// deposit
deposit(0, depositSize);
deposit(1, depositSize);

for (auto const& trade : trades)
{
auto const& sendAsset = trade.first ? cur1 : cur2;
auto const& recvAsset = trade.first ? cur2 : cur1;
root.pay(root, sendAsset, INT64_MAX, recvAsset, trade.second, {});
}

// withdraw in reverse order
for (auto rit = deposits.rbegin(); rit != deposits.rend(); ++rit)
{
auto& deposit = *rit;
deposit.acc.liquidityPoolWithdraw(pool12, deposit.numPoolShares, 0, 0);

total -= deposit.numPoolShares;
}

checkLiquidityPool(app, pool12, 0, 0, 0, 2);

// delete trustlines
int i = 2;
for (auto& deposit : deposits)
{
deposit.acc.changeTrust(share12, 0);

if (--i > 0)
{
checkLiquidityPool(app, pool12, 0, 0, 0, i);
}
}

LedgerTxn ltx(app.getLedgerTxnRoot());
REQUIRE(!loadLiquidityPool(ltx, pool12));
}

}
}
4 changes: 4 additions & 0 deletions src/test/TxTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,9 @@ LedgerHeader executeUpgrades(Application& app,

LedgerHeader executeUpgrade(Application& app, LedgerUpgrade const& lupgrade);

void
depositTradeWithdrawTest(Application& app, TestAccount& root, int depositSize,
std::vector<std::pair<bool, int64_t>> const& trades);

} // end txtest namespace
}
26 changes: 25 additions & 1 deletion src/transactions/test/LiquidityPoolWithdrawTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

#include "ledger/LedgerTxn.h"
#include "lib/catch.hpp"
#include "main/Application.h"
#include "test/TestAccount.h"
Expand Down Expand Up @@ -387,5 +388,28 @@ TEST_CASE("liquidity pool withdraw", "[tx][liquiditypool]")
REQUIRE(acc1.getTrustlineBalance(poolNative1) == 0);
checkLiquidityPool(*app, poolNative1, 0, 0, 0, 1);
}

SECTION("large deposit/withdraw test")
{
// balances between 1000 and 1050, with # of trades and trade sizes
// between 5 and 25
for (int i = 1000; i < 1050; ++i)
{
std::uniform_int_distribution<int64_t> dist(5, 25);

SECTION(fmt::format("deposit amount = {}", i))
{
std::vector<std::pair<bool, int64_t>> trades;
int64_t numTrades = dist(gRandomEngine);
for (int j = 0; j < numTrades; ++j)
{
int64_t tradeSize = dist(gRandomEngine);
trades.emplace_back(tradeSize % 2 == 0, tradeSize);
}

depositTradeWithdrawTest(*app, root, i, trades);
}
}
}
});
}
}

0 comments on commit e381447

Please sign in to comment.