Skip to content

Commit 1208b57

Browse files
committed
fixes LoanBrokerSet debtMaximum limits
1 parent 5224636 commit 1208b57

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/test/app/LoanBroker_test.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,10 +1436,83 @@ class LoanBroker_test : public beast::unit_test::suite
14361436
});
14371437
}
14381438

1439+
void
1440+
testLoanBrokerSetDebtMaximum()
1441+
{
1442+
testcase("testLoanBrokerSetDebtMaximum");
1443+
using namespace jtx;
1444+
using namespace loanBroker;
1445+
Account const issuer{"issuer"};
1446+
Account const alice{"alice"};
1447+
Env env(*this);
1448+
Vault vault{env};
1449+
1450+
env.fund(XRP(100'000), issuer, alice);
1451+
env.close();
1452+
1453+
PrettyAsset const asset = [&]() {
1454+
env(trust(alice, issuer["IOU"](1'000'000)), THISLINE);
1455+
env.close();
1456+
return PrettyAsset(issuer["IOU"]);
1457+
}();
1458+
1459+
env(pay(issuer, alice, asset(100'000)), THISLINE);
1460+
env.close();
1461+
1462+
auto [tx, vaultKeylet] = vault.create({.owner = alice, .asset = asset});
1463+
env(tx, THISLINE);
1464+
env.close();
1465+
auto const le = env.le(vaultKeylet);
1466+
VaultInfo vaultInfo = [&]() {
1467+
if (BEAST_EXPECT(le))
1468+
return VaultInfo{asset, vaultKeylet.key, le->at(sfAccount)};
1469+
return VaultInfo{asset, {}, {}};
1470+
}();
1471+
if (vaultInfo.vaultID == uint256{})
1472+
return;
1473+
1474+
env(vault.deposit(
1475+
{.depositor = alice,
1476+
.id = vaultKeylet.key,
1477+
.amount = asset(50)}),
1478+
THISLINE);
1479+
env.close();
1480+
1481+
auto const brokerKeylet =
1482+
keylet::loanbroker(alice.id(), env.seq(alice));
1483+
env(set(alice, vaultInfo.vaultID), THISLINE);
1484+
env.close();
1485+
1486+
Account const borrower{"borrower"};
1487+
env.fund(XRP(1'000), borrower);
1488+
env(loan::set(borrower, brokerKeylet.key, asset(50).value()),
1489+
sig(sfCounterpartySignature, alice),
1490+
fee(env.current()->fees().base * 2),
1491+
THISLINE);
1492+
auto const broker = env.le(brokerKeylet);
1493+
if (!BEAST_EXPECT(broker))
1494+
return;
1495+
1496+
BEAST_EXPECT(broker->at(sfDebtTotal) == 50);
1497+
auto debtTotal = broker->at(sfDebtTotal);
1498+
1499+
auto tx2 = set(alice, vaultInfo.vaultID);
1500+
tx2[sfLoanBrokerID] = to_string(brokerKeylet.key);
1501+
tx2[sfDebtMaximum] = debtTotal - 1;
1502+
env(tx2, ter(tecLIMIT_EXCEEDED), THISLINE);
1503+
1504+
tx2[sfDebtMaximum] = debtTotal + 1;
1505+
env(tx2, ter(tesSUCCESS), THISLINE);
1506+
1507+
tx2[sfDebtMaximum] = 0;
1508+
env(tx2, ter(tesSUCCESS), THISLINE);
1509+
}
1510+
14391511
public:
14401512
void
14411513
run() override
14421514
{
1515+
testLoanBrokerSetDebtMaximum();
14431516
testLoanBrokerCoverDepositNullVault();
14441517

14451518
testDisabled();

src/xrpld/app/tx/detail/LoanBrokerSet.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ LoanBrokerSet::preclaim(PreclaimContext const& ctx)
8989
JLOG(ctx.j.warn()) << "Account is not the owner of the LoanBroker.";
9090
return tecNO_PERMISSION;
9191
}
92+
93+
if (auto const debtMax = tx[~sfDebtMaximum]; debtMax)
94+
{
95+
// Can't reduce the debt maximum below the current total debt
96+
auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
97+
if (*debtMax != 0 && *debtMax < currentDebtTotal)
98+
{
99+
JLOG(ctx.j.warn())
100+
<< "Cannot reduce DebtMaximum below current DebtTotal.";
101+
return tecLIMIT_EXCEEDED;
102+
}
103+
}
92104
}
93105
else
94106
{

0 commit comments

Comments
 (0)