Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/libxrpl/tx/invariants/VaultInvariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ ValidVault::finalize(
result = false;
}

Comment thread
tyalymov marked this conversation as resolved.
if (view.rules().enabled(fixCleanup3_4_0) && afterVault.lossUnrealized < kZero)
{
JLOG(j.fatal()) << "Invariant failed: loss unrealized must not be negative";
result = false;
}

Comment thread
Tapanito marked this conversation as resolved.
if (afterVault.assetsTotal < kZero)
{
JLOG(j.fatal()) << "Invariant failed: assets outstanding must be positive";
Expand Down
35 changes: 35 additions & 0 deletions src/test/app/Invariants_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3374,6 +3374,41 @@ class Invariants_test : public beast::unit_test::Suite
precloseXrp,
TxAccount::A2);

// A negative loss unrealized must trip the invariant. ttLOAN_MANAGE is
// allowed to change loss unrealized, so it isolates this check from the
// "must not change loss unrealized" invariant. Gated behind
// fixCleanup3_4_0 (see below).
doInvariantCheck(
{"loss unrealized must not be negative"},
[&](Account const& a1, Account const& a2, ApplyContext& ac) {
auto const keylet = keylet::vault(a1.id(), ac.view().seq());
return kAdjust(ac.view(), keylet, kArgs(a2.id(), 0, [&](Adjustments& sample) {
sample.lossUnrealized = -1;
}));
},
XRPAmount{},
STTx{ttLOAN_MANAGE, [](STObject& tx) {}},
{tecINVARIANT_FAILED, tecINVARIANT_FAILED},
precloseXrp,
TxAccount::A2);

// Without fixCleanup3_4_0 the same state must NOT trip the invariant,
// preserving pre-amendment behavior (no fork risk).
doInvariantCheck(
makeEnv(defaultAmendments() - fixCleanup3_4_0),
{},
[&](Account const& a1, Account const& a2, ApplyContext& ac) {
auto const keylet = keylet::vault(a1.id(), ac.view().seq());
return kAdjust(ac.view(), keylet, kArgs(a2.id(), 0, [&](Adjustments& sample) {
sample.lossUnrealized = -1;
}));
},
XRPAmount{},
STTx{ttLOAN_MANAGE, [](STObject& tx) {}},
{tesSUCCESS, tesSUCCESS},
precloseXrp,
TxAccount::A2);

doInvariantCheck(
{"set assets outstanding must not exceed assets maximum"},
[&](Account const& a1, Account const& a2, ApplyContext& ac) {
Expand Down
Loading