Skip to content

Commit

Permalink
[refactor] escrow test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
dangell7 committed Mar 3, 2025
1 parent 0651264 commit 12f432f
Show file tree
Hide file tree
Showing 12 changed files with 965 additions and 1,077 deletions.
8 changes: 4 additions & 4 deletions src/test/app/AMM_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3387,10 +3387,10 @@ struct AMM_test : public jtx::AMMTest

// Can't pay into AMM with escrow.
testAMM([&](AMM& ammAlice, Env& env) {
env(escrow(carol, ammAlice.ammAccount(), XRP(1)),
condition(cb1),
finish_time(env.now() + 1s),
cancel_time(env.now() + 2s),
env(escrow::create(carol, ammAlice.ammAccount(), XRP(1)),
escrow::condition(escrow::cb1),
escrow::finish_time(env.now() + 1s),
escrow::cancel_time(env.now() + 2s),
fee(1'500),
ter(tecNO_PERMISSION));
});
Expand Down
55 changes: 16 additions & 39 deletions src/test/app/AccountDelete_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,27 +360,11 @@ class AccountDelete_test : public beast::unit_test::suite
env(check::cancel(becky, checkId));
env.close();

// Lambda to create an escrow.
auto escrowCreate = [](jtx::Account const& account,
jtx::Account const& to,
STAmount const& amount,
NetClock::time_point const& cancelAfter) {
Json::Value jv;
jv[jss::TransactionType] = jss::EscrowCreate;
jv[jss::Flags] = tfUniversal;
jv[jss::Account] = account.human();
jv[jss::Destination] = to.human();
jv[jss::Amount] = amount.getJson(JsonOptions::none);
jv[sfFinishAfter.jsonName] =
cancelAfter.time_since_epoch().count() + 1;
jv[sfCancelAfter.jsonName] =
cancelAfter.time_since_epoch().count() + 2;
return jv;
};

using namespace std::chrono_literals;
std::uint32_t const escrowSeq{env.seq(alice)};
env(escrowCreate(alice, becky, XRP(333), env.now() + 2s));
env(escrow::create(alice, becky, XRP(333)),
escrow::finish_time(env.now() + 3s),
escrow::cancel_time(env.now() + 4s));
env.close();

// alice and becky should be unable to delete their accounts because
Expand All @@ -392,46 +376,39 @@ class AccountDelete_test : public beast::unit_test::suite
// Now cancel the escrow, but create a payment channel between
// alice and becky.

// Lambda to cancel an escrow.
auto escrowCancel =
[](Account const& account, Account const& from, std::uint32_t seq) {
Json::Value jv;
jv[jss::TransactionType] = jss::EscrowCancel;
jv[jss::Flags] = tfUniversal;
jv[jss::Account] = account.human();
jv[sfOwner.jsonName] = from.human();
jv[sfOfferSequence.jsonName] = seq;
return jv;
};

bool const withTokenEscrow =
env.current()->rules().enabled(featureTokenEscrow);
if (withTokenEscrow)
{
Account const gw1("gw1");
Account const carol("carol");
auto const USD = gw["USD"];
env.fund(XRP(100000), carol);
env(fset(gw, asfAllowTokenLocking));
auto const USD = gw1["USD"];
env.fund(XRP(100000), carol, gw1);
env(fset(gw1, asfAllowTokenLocking));
env.close();
env.trust(USD(10000), carol);
env.close();
env(pay(gw, carol, USD(100)));
env(pay(gw1, carol, USD(100)));
env.close();

std::uint32_t const escrowSeq{env.seq(carol)};
env(escrowCreate(carol, becky, USD(1), env.now() + 2s));
env(escrow::create(carol, becky, USD(1)),
escrow::finish_time(env.now() + 3s),
escrow::cancel_time(env.now() + 4s));
env.close();

env(acctdelete(gw, becky),
incLgrSeqForAccDel(env, gw1);

env(acctdelete(gw1, becky),
fee(acctDelFee),
ter(tecHAS_OBLIGATIONS));
env.close();

env(escrowCancel(becky, carol, escrowSeq));
env(escrow::cancel(becky, carol, escrowSeq));
env.close();
}

env(escrowCancel(becky, alice, escrowSeq));
env(escrow::cancel(becky, alice, escrowSeq));
env.close();

Keylet const alicePayChanKey{
Expand Down
15 changes: 8 additions & 7 deletions src/test/app/DepositAuth_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,12 +707,12 @@ struct DepositPreauth_test : public beast::unit_test::suite
if (!supportsPreauth)
{
auto const seq1 = env.seq(alice);
env(escrow(alice, becky, XRP(100)),
finish_time(env.now() + 1s));
env(escrow::create(alice, becky, XRP(100)),
escrow::finish_time(env.now() + 1s));
env.close();

// Failed as rule is disabled
env(finish(gw, alice, seq1),
env(escrow::finish(gw, alice, seq1),
fee(1500),
ter(tecNO_PERMISSION));
env.close();
Expand Down Expand Up @@ -1379,12 +1379,13 @@ struct DepositPreauth_test : public beast::unit_test::suite
env.close();

auto const seq = env.seq(alice);
env(escrow(alice, bob, XRP(1000)), finish_time(env.now() + 1s));
env(escrow::create(alice, bob, XRP(1000)),
escrow::finish_time(env.now() + 1s));
env.close();

// zelda can't finish escrow with invalid credentials
{
env(finish(zelda, alice, seq),
env(escrow::finish(zelda, alice, seq),
credentials::ids({}),
ter(temMALFORMED));
env.close();
Expand All @@ -1396,14 +1397,14 @@ struct DepositPreauth_test : public beast::unit_test::suite
"0E0B04ED60588A758B67E21FBBE95AC5A63598BA951761DC0EC9C08D7E"
"01E034";

env(finish(zelda, alice, seq),
env(escrow::finish(zelda, alice, seq),
credentials::ids({invalidIdx}),
ter(tecBAD_CREDENTIALS));
env.close();
}

{ // Ledger closed, time increased, zelda can't finish escrow
env(finish(zelda, alice, seq),
env(escrow::finish(zelda, alice, seq),
credentials::ids({credIdx}),
fee(1500),
ter(tecEXPIRED));
Expand Down
Loading

0 comments on commit 12f432f

Please sign in to comment.