xrpld
Loading...
Searching...
No Matches
VaultInvariantPrecision_test.cpp
1#include <test/app/vault/VaultPrecisionFixture.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/amount.h>
4#include <test/jtx/envconfig.h>
5#include <test/jtx/ter.h>
6#include <test/jtx/vault.h>
7
8#include <xrpl/basics/Number.h>
9#include <xrpl/beast/unit_test/suite.h>
10#include <xrpl/beast/utility/Journal.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/MPTIssue.h>
13#include <xrpl/protocol/STAmount.h>
14#include <xrpl/protocol/TER.h>
15
16#include <algorithm>
17#include <array>
18#include <cstdint>
19#include <string>
20#include <tuple>
21
22namespace xrpl::test {
23
24// With fixCleanup3_4_0 disabled the six delta invariants and the
25// lossUnrealized > (assetsTotal - assetsAvailable) gap invariant spuriously
26// fire on legitimate flows; with the amendment enabled the one-unit
27// tolerance absorbs the sub-ULP drift and every one of these transactions
28// must succeed. Exactness (assetsTotal delta == assetsAvailable delta
29// exactly) is covered by VaultTransactorPrecision_test.
31{
32 // Deposit small integer amounts into an A-1 vault. Pre-amendment,
33 // deposits of 1, 7, and 10'000'000 land on assetsTotal/assetsAvailable
34 // grids that disagree by one ULP and the invariant fires. Post-
35 // amendment the tolerance-widened check accepts the same states.
36 void
38 {
39 using namespace jtx;
40
41 bool const fixEnabled = features[fixCleanup3_4_0];
43 std::string("A-1 deposit boundary invariant") +
44 (fixEnabled ? " (fixCleanup3_4_0)" : " (pre-fix)"));
45
46 std::array<int, 3> const kAmounts{1, 7, 10'000'000};
47
48 for (auto const amount : kAmounts)
49 {
50 Env env{*this, envconfig(), features, nullptr, beast::Severity::Disabled};
51 auto f = setupSingleLoanVault(env, /*impairAndPaySibling=*/false);
52 if (!f.asset || !f.broker)
53 {
54 BEAST_EXPECT(f.asset && f.broker);
55 continue;
56 }
57 auto const& asset = *f.asset;
58
59 auto const before = read(env, f);
60
61 Vault const v{env};
62 env(v.deposit({
63 .depositor = f.depositor,
64 .id = f.vaultKeylet.key,
65 .amount = asset(amount).value(),
66 }),
67 Ter(std::ignore));
68 env.close();
69
70 TER const actual = env.ter();
71
72 if (fixEnabled)
73 {
74 BEAST_EXPECTS(
75 actual == tesSUCCESS,
76 "amount=" + std::to_string(amount) + " expected tesSUCCESS, got " +
77 transToken(actual));
78
79 auto const after = read(env, f);
80 Number const tDelta = after.assetsTotal - before.assetsTotal;
81 Number const aDelta = after.assetsAvailable - before.assetsAvailable;
82 Number const requested = asset(amount).number();
83
84 BEAST_EXPECT(tDelta <= requested);
85
86 Number const gap = tDelta > aDelta ? tDelta - aDelta : aDelta - tDelta;
87 BEAST_EXPECT(gap <= oneUnit(asset, after.assetsTotal));
88 }
89 else
90 {
91 BEAST_EXPECTS(
92 actual == tecINVARIANT_FAILED,
93 "amount=" + std::to_string(amount) + " expected tecINVARIANT_FAILED, got " +
94 transToken(actual));
95 }
96 }
97 }
98
99 // Withdraw long-mantissa share counts from an A-1 vault. Pre-fix
100 // some counts trip the withdraw delta invariants; post-fix none does.
101 void
103 {
104 using namespace jtx;
105
106 bool const fixEnabled = features[fixCleanup3_4_0];
107 testcase(
108 std::string("A-1 withdraw boundary invariant") +
109 (fixEnabled ? " (fixCleanup3_4_0)" : " (pre-fix)"));
110
111 std::array<std::uint64_t, 6> const kShareCounts{
112 99'999u,
113 100'001u,
114 333'333u,
115 1'234'567u,
116 142'857'142u,
117 333'333'333u,
118 };
119
120 // Fill the vault with enough shares that every count below is
121 // available to the depositor.
122 Env env{*this, envconfig(), features, nullptr, beast::Severity::Disabled};
123 auto f = setupSingleLoanVault(env, /*impairAndPaySibling=*/false);
124 if (!f.asset || !f.broker)
125 {
126 BEAST_EXPECT(f.asset && f.broker);
127 return;
128 }
129 auto const& asset = *f.asset;
130
131 Vault const v{env};
132 // Deposit a large amount so we can afford every withdrawal below.
133 env(v.deposit({
134 .depositor = f.depositor,
135 .id = f.vaultKeylet.key,
136 .amount = asset(1'000'000).value(),
137 }),
138 Ter(std::ignore));
139 env.close();
140
141 for (auto const count : kShareCounts)
142 {
143 auto const before = read(env, f);
144 if (before.sharesTotal < count)
145 continue;
146
147 STAmount const shareAmount{MPTIssue{f.share}, Number{static_cast<std::int64_t>(count)}};
148 env(v.withdraw(
149 {.depositor = f.depositor, .id = f.vaultKeylet.key, .amount = shareAmount}),
150 Ter(std::ignore));
151 env.close();
152
153 TER const actual = env.ter();
154
155 if (fixEnabled)
156 {
157 BEAST_EXPECTS(
158 actual != tecINVARIANT_FAILED,
159 "shares=" + std::to_string(count) + " unexpected invariant failure");
160
161 if (actual == tesSUCCESS)
162 {
163 auto const after = read(env, f);
164 Number const tDelta = before.assetsTotal - after.assetsTotal;
165 Number const pDelta = before.pseudo - after.pseudo;
166 Number const gap = tDelta > pDelta ? tDelta - pDelta : pDelta - tDelta;
167 // VaultTransactorPrecision_test tightens this to strict
168 // equality.
169 BEAST_EXPECT(gap <= oneUnit(asset, before.assetsTotal));
170 }
171 }
172 // Pre-fix behaviour is fixture-dependent: some share counts may
173 // succeed even without the amendment. The important property is
174 // that post-fix no legitimate withdrawal is rejected by the
175 // widened invariant.
176 }
177 }
178
179 // Clawback of small IOU amounts against a live-loan vault. Pre-fix
180 // some amounts trip the clawback delta invariants; post-fix none does.
181 // Also assert the owner force-burn path returns tecNO_PERMISSION
182 // under both amendment states (it never enters assetsToClawback).
183 void
185 {
186 using namespace jtx;
187
188 bool const fixEnabled = features[fixCleanup3_4_0];
189 testcase(
190 std::string("A-1 clawback boundary invariant") +
191 (fixEnabled ? " (fixCleanup3_4_0)" : " (pre-fix)"));
192
193 std::array<int, 6> const kAmounts{1, 7, 99, 333, 993, 2000};
194
195 Env env{*this, envconfig(), features, nullptr, beast::Severity::Disabled};
196 auto f = setupSingleLoanVault(env, /*impairAndPaySibling=*/false, /*allowClawback=*/true);
197 if (!f.asset || !f.broker)
198 {
199 BEAST_EXPECT(f.asset && f.broker);
200 return;
201 }
202 auto const& asset = *f.asset;
203
204 Vault const v{env};
205
206 // Give the depositor a stake so that the issuer has something to
207 // claw back.
208 env(v.deposit({
209 .depositor = f.depositor,
210 .id = f.vaultKeylet.key,
211 .amount = asset(2'000).value(),
212 }),
213 Ter(std::ignore));
214 env.close();
215
216 for (auto const amount : kAmounts)
217 {
218 auto const before = read(env, f);
219 if (before.sharesTotal == 0)
220 continue;
221
222 env(v.clawback({
223 .issuer = f.issuer,
224 .id = f.vaultKeylet.key,
225 .holder = f.depositor,
226 .amount = asset(amount).value(),
227 }),
228 Ter(std::ignore));
229 env.close();
230
231 TER const actual = env.ter();
232
233 if (fixEnabled)
234 {
235 BEAST_EXPECTS(
236 actual != tecINVARIANT_FAILED,
237 "amount=" + std::to_string(amount) + " unexpected invariant failure");
238 }
239 // Pre-fix behaviour is fixture-dependent: some clawback amounts
240 // may succeed even without the amendment. The important
241 // property is that post-fix no legitimate clawback is rejected
242 // by the widened invariant.
243 }
244
245 // Owner force-burn only succeeds against an EMPTY vault (see
246 // VaultClawback::preclaim). Our fixture keeps a live loan, so
247 // this must return tecNO_PERMISSION regardless of the amendment.
248 env(v.clawback({.issuer = f.lender, .id = f.vaultKeylet.key, .holder = f.depositor}),
250 env.close();
251 }
252
253 // Deposit into an A-3 vault where the impaired-loan gap plus the
254 // interest earned from the sibling repayment lands L > (T - A) by
255 // sub-ULP. Pre-fix the loss invariant fires; post-fix it does not.
256 void
258 {
259 using namespace jtx;
260
261 bool const fixEnabled = features[fixCleanup3_4_0];
262 testcase(
263 std::string("A-3 loss invariant sweep") +
264 (fixEnabled ? " (fixCleanup3_4_0)" : " (pre-fix)"));
265
266 std::array<int, 3> const kAmounts{1, 7, 10'000'000};
267
268 for (auto const amount : kAmounts)
269 {
270 Env env{*this, envconfig(), features, nullptr, beast::Severity::Disabled};
271 auto f = setupSingleLoanVault(env, /*impairAndPaySibling=*/true);
272 if (!f.asset || !f.broker)
273 {
274 BEAST_EXPECT(f.asset && f.broker);
275 continue;
276 }
277 auto const& asset = *f.asset;
278
279 Vault const v{env};
280 env(v.deposit({
281 .depositor = f.depositor,
282 .id = f.vaultKeylet.key,
283 .amount = asset(amount).value(),
284 }),
285 Ter(std::ignore));
286 env.close();
287
288 TER const actual = env.ter();
289
290 if (fixEnabled)
291 {
292 BEAST_EXPECTS(
293 actual == tesSUCCESS,
294 "amount=" + std::to_string(amount) + " expected tesSUCCESS, got " +
295 transToken(actual));
296
297 auto const after = read(env, f);
298 BEAST_EXPECT(
299 after.lossUnrealized <= (after.assetsTotal - after.assetsAvailable) +
300 oneUnit(asset, after.assetsTotal));
301 }
302 else
303 {
304 BEAST_EXPECTS(
305 actual == tecINVARIANT_FAILED,
306 "amount=" + std::to_string(amount) + " expected tecINVARIANT_FAILED, got " +
307 transToken(actual));
308 }
309 }
310 }
311
312 // Full 17-magnitude A-1 deposit sweep. Pre-fix {1, 7, 10'000'000}
313 // are the boundary amounts that fail; post-fix every amount succeeds.
314 void
316 {
317 using namespace jtx;
318
319 bool const fixEnabled = features[fixCleanup3_4_0];
320 testcase(
321 std::string("A-1 deposit magnitude sweep") +
322 (fixEnabled ? " (fixCleanup3_4_0)" : " (pre-fix)"));
323
324 std::array<int, 17> const kAmounts{
325 1,
326 2,
327 5,
328 7,
329 10,
330 50,
331 100,
332 500,
333 1'000,
334 5'000,
335 10'000,
336 50'000,
337 100'000,
338 500'000,
339 1'000'000,
340 5'000'000,
341 10'000'000,
342 };
343 std::array<int, 3> const kPreFixFailures{1, 7, 10'000'000};
344
345 for (auto const amount : kAmounts)
346 {
347 Env env{*this, envconfig(), features, nullptr, beast::Severity::Disabled};
348 auto f = setupSingleLoanVault(env, /*impairAndPaySibling=*/false);
349 if (!f.asset || !f.broker)
350 {
351 BEAST_EXPECT(f.asset && f.broker);
352 continue;
353 }
354 auto const& asset = *f.asset;
355
356 Vault const v{env};
357 env(v.deposit({
358 .depositor = f.depositor,
359 .id = f.vaultKeylet.key,
360 .amount = asset(amount).value(),
361 }),
362 Ter(std::ignore));
363 env.close();
364
365 TER const actual = env.ter();
366
367 if (fixEnabled)
368 {
369 BEAST_EXPECTS(
370 actual == tesSUCCESS,
371 "amount=" + std::to_string(amount) + " expected tesSUCCESS, got " +
372 transToken(actual));
373 }
374 else
375 {
376 bool const shouldFail =
377 std::ranges::find(kPreFixFailures, amount) != kPreFixFailures.end();
378 if (shouldFail)
379 {
380 BEAST_EXPECTS(
381 actual == tecINVARIANT_FAILED,
382 "pre-fix amount=" + std::to_string(amount) +
383 " expected tecINVARIANT_FAILED, got " + transToken(actual));
384 }
385 // For other amounts pre-fix, we accept any outcome; the
386 // interesting property is only asserted for the known-failing
387 // ones.
388 }
389 }
390 }
391
392 // A-3 deposit sweep. Pre-fix {1, 7, 10'000, 10'000'000} fail; post-fix
393 // every amount succeeds. 99'999 (delta tolerance) and 10'000'000
394 // (loss tolerance) are the two boundary cases that motivate this PR.
395 void
397 {
398 using namespace jtx;
399
400 bool const fixEnabled = features[fixCleanup3_4_0];
401 testcase(
402 std::string("A-3 deposit magnitude sweep") +
403 (fixEnabled ? " (fixCleanup3_4_0)" : " (pre-fix)"));
404
405 std::array<int, 9> const kAmounts{
406 1,
407 7,
408 100,
409 1'000,
410 10'000,
411 100'000,
412 1'000'000,
413 10'000'000,
414 99'999,
415 };
416
417 std::array<int, 4> const kPreFixFailures{1, 7, 10'000, 10'000'000};
418
419 for (auto const amount : kAmounts)
420 {
421 Env env{*this, envconfig(), features, nullptr, beast::Severity::Disabled};
422 auto f = setupSingleLoanVault(env, /*impairAndPaySibling=*/true);
423 if (!f.asset || !f.broker)
424 {
425 BEAST_EXPECT(f.asset && f.broker);
426 continue;
427 }
428 auto const& asset = *f.asset;
429
430 Vault const v{env};
431 env(v.deposit({
432 .depositor = f.depositor,
433 .id = f.vaultKeylet.key,
434 .amount = asset(amount).value(),
435 }),
436 Ter(std::ignore));
437 env.close();
438
439 TER const actual = env.ter();
440
441 if (fixEnabled)
442 {
443 BEAST_EXPECTS(
444 actual == tesSUCCESS,
445 "amount=" + std::to_string(amount) + " expected tesSUCCESS, got " +
446 transToken(actual));
447 }
448 else
449 {
450 bool const shouldFail =
451 std::ranges::find(kPreFixFailures, amount) != kPreFixFailures.end();
452 if (shouldFail)
453 {
454 BEAST_EXPECTS(
455 actual == tecINVARIANT_FAILED,
456 "pre-fix amount=" + std::to_string(amount) +
457 " expected tecINVARIANT_FAILED, got " + transToken(actual));
458 }
459 }
460 }
461 }
462
463public:
464 void
465 run() override
466 {
467 for (auto const& features : {all_ - fixCleanup3_4_0, all_})
468 {
472 testLossInvariantA3(features);
473 testA1DepositMagnitudes(features);
474 testA3DepositMagnitudes(features);
475 }
476 }
477};
478
479BEAST_DEFINE_TESTSUITE(VaultInvariantPrecision, app, xrpl);
480
481} // namespace xrpl::test
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
FeatureBitset const all_
static Number oneUnit(Asset const &asset, Number const &assetsTotalAfter)
static Numbers read(jtx::Env const &env, Fixture const &f)
static Fixture setupSingleLoanVault(jtx::Env &env, bool impairAndPaySibling, bool allowClawback=false)
A transaction testing environment.
Definition Env.h:161
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
TER ter() const
Return the TER for the last JTx.
Definition Env.h:844
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
T end(T... args)
T find(T... args)
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string transToken(TER code)
Definition TER.cpp:259
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:668
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecINVARIANT_FAILED
Definition TER.h:324
@ tecNO_PERMISSION
Definition TER.h:316
@ tesSUCCESS
Definition TER.h:253
static json::Value withdraw(WithdrawArgs const &args)
Definition vault.cpp:101
static json::Value clawback(ClawbackArgs const &args)
Definition vault.cpp:112
static json::Value deposit(DepositArgs const &args)
Definition vault.cpp:90
T to_string(T... args)