xrpld
Loading...
Searching...
No Matches
VaultTransactorPrecision_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 <array>
17#include <cstddef>
18#include <cstdint>
19#include <optional>
20#include <string>
21#include <tuple>
22
23namespace xrpl::test {
24
25// With fixCleanup3_4_0, deposit/withdraw/clawback apply one amount on the
26// sfAssetsTotal grid. These tests require T, A, and the pseudo-account to
27// change by the same Number; the invariant suite still allows a one-unit gap.
29{
32 {
33 return jtx::Env{*this, jtx::envconfig(), all_, nullptr, beast::Severity::Disabled};
34 }
35
36 bool
37 ready(Fixture const& f)
38 {
39 return BEAST_EXPECT(f.asset && f.broker) && f.asset;
40 }
41
42 void
43 assertEqualDeltas(Numbers const& before, Numbers const& after, std::string const& tag)
44 {
45 Number const tDelta = before.assetsTotal - after.assetsTotal;
46 Number const aDelta = before.assetsAvailable - after.assetsAvailable;
47 Number const pDelta = before.pseudo - after.pseudo;
48 BEAST_EXPECTS(tDelta == aDelta, tag + " tDelta != aDelta");
49 BEAST_EXPECTS(tDelta == pDelta, tag + " tDelta != pDelta");
50 }
51
52 void
54 {
55 using namespace jtx;
56
57 testcase("deposit clamp does not over-credit");
58
59 std::array<int, 4> const kAmounts{1, 7, 1'000, 10'000'000};
60
61 for (auto const amount : kAmounts)
62 {
63 Env env = makeEnv();
64 auto f = setupSingleLoanVault(env, /*impairAndPaySibling=*/false);
65 if (!ready(f))
66 continue;
67 // ready() above guarantees f.asset is engaged; the guard is opaque to clang-tidy.
68 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
69 jtx::PrettyAsset const& asset = f.asset.value();
70
71 auto const before = read(env, f);
72
73 Vault const v{env};
74 env(v.deposit({
75 .depositor = f.depositor,
76 .id = f.vaultKeylet.key,
77 .amount = asset(amount).value(),
78 }),
79 Ter(std::ignore));
80 env.close();
81
82 if (env.ter() != tesSUCCESS)
83 continue;
84
85 auto const after = read(env, f);
86 Number const tDelta = after.assetsTotal - before.assetsTotal;
87 Number const requested = asset(amount).number();
88 BEAST_EXPECTS(
89 tDelta <= requested,
90 "amount=" + std::to_string(amount) + " tDelta exceeds requested");
91
92 Number const sharesMinted = after.sharesTotal - before.sharesTotal;
93 if (before.sharesTotal == Number{0})
94 continue;
95 Number const shareValue = (before.assetsTotal * sharesMinted) / before.sharesTotal;
96 // The depositor is never charged more than the shares they received are worth.
97 BEAST_EXPECTS(
98 tDelta <= shareValue,
99 "amount=" + std::to_string(amount) + " assetsTaken > shareValue");
100 // Discount is strictly less than one ULP of the new AssetsTotal
101 BEAST_EXPECTS(
102 shareValue - tDelta < oneUnit(asset.raw(), after.assetsTotal),
103 "amount=" + std::to_string(amount) + " discount is not below one unit");
104 }
105
106 {
107 Env env = makeEnv();
108 auto f = setupSingleLoanVault(env, /*impairAndPaySibling=*/false);
109 if (!ready(f))
110 return;
111 // ready() above guarantees f.asset is engaged; the guard is opaque to clang-tidy.
112 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
113 jtx::PrettyAsset const& asset = f.asset.value();
114
115 Vault const v{env};
116 env(v.deposit({
117 .depositor = f.depositor,
118 .id = f.vaultKeylet.key,
119 .amount = asset(99'000'000).value(),
120 }),
121 Ter(std::ignore));
122 env.close();
123
124 auto const before = read(env, f);
125 Number const kLowerBound{1, 6};
126 BEAST_EXPECT(before.assetsTotal > kLowerBound);
127
128 auto const tinyAmount = asset(Number{1, -10}).value();
129 env(v.deposit(
130 {.depositor = f.depositor, .id = f.vaultKeylet.key, .amount = tinyAmount}),
131 Ter(std::ignore));
132 env.close();
133
134 BEAST_EXPECTS(
135 env.ter() == tecPRECISION_LOSS,
136 std::string{"expected tecPRECISION_LOSS, got "} + transToken(env.ter()));
137
138 auto const after = read(env, f);
139 BEAST_EXPECT(after.assetsTotal == before.assetsTotal);
140 BEAST_EXPECT(after.assetsAvailable == before.assetsAvailable);
141 BEAST_EXPECT(after.sharesTotal == before.sharesTotal);
142 }
143 }
144
145 void
147 {
148 using namespace jtx;
149
150 testcase("withdraw deltas are equal");
151
152 Env env = makeEnv();
153 auto f = setupSingleLoanVault(env, /*impairAndPaySibling=*/false);
154 if (!ready(f))
155 return;
156 // ready() above guarantees f.asset is engaged; the guard is opaque to clang-tidy.
157 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
158 jtx::PrettyAsset const& asset = f.asset.value();
159
160 Vault const v{env};
161 env(v.deposit({
162 .depositor = f.depositor,
163 .id = f.vaultKeylet.key,
164 .amount = asset(1'000'000).value(),
165 }),
166 Ter(std::ignore));
167 env.close();
168
169 auto const checkSuccess = [&](STAmount const& amount, std::string const& tag) {
170 auto const before = read(env, f);
171 env(v.withdraw({.depositor = f.depositor, .id = f.vaultKeylet.key, .amount = amount}),
172 Ter(std::ignore));
173 env.close();
174 if (env.ter() != tesSUCCESS)
175 return;
176
177 auto const after = read(env, f);
178 assertEqualDeltas(before, after, tag);
179
180 Number const sharesBurned = before.sharesTotal - after.sharesTotal;
181 if (before.sharesTotal == Number{0})
182 return;
183 Number const shareValue = (before.assetsTotal * sharesBurned) / before.sharesTotal;
184 Number const tDelta = before.assetsTotal - after.assetsTotal;
185 BEAST_EXPECTS(tDelta <= shareValue, tag + " payout > shareValue");
186 };
187
188 std::array<std::uint64_t, 3> const kShareCounts{99'999u, 333'333u, 1'234'567u};
189 for (auto const count : kShareCounts)
190 {
191 auto const before = read(env, f);
192 if (before.sharesTotal < count)
193 continue;
194 STAmount const shareAmount{MPTIssue{f.share}, Number{static_cast<std::int64_t>(count)}};
195 checkSuccess(shareAmount, "shares=" + std::to_string(count));
196 }
197
198 std::array<int, 3> const kAssetAmounts{1, 7, 99};
199 for (auto const amount : kAssetAmounts)
200 checkSuccess(asset(amount).value(), "assets=" + std::to_string(amount));
201 }
202
203 // Withdraw more than sfAssetsAvailable must return tecINSUFFICIENT_FUNDS,
204 // not tecPRECISION_LOSS.
205 void
206 testWithdrawInsufficientFundsPrecedence()
207 {
208 using namespace jtx;
209
210 testcase("withdraw over available returns insufficient funds, not precision loss");
211
212 Env env = makeEnv();
213 auto f = setupSingleLoanVault(env, /*impairAndPaySibling=*/false);
214 if (!ready(f))
215 return;
216 // ready() above guarantees f.asset is engaged; the guard is opaque to clang-tidy.
217 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
218 jtx::PrettyAsset const& asset = f.asset.value();
219
220 Vault const v{env};
221 env(v.deposit({
222 .depositor = f.depositor,
223 .id = f.vaultKeylet.key,
224 .amount = asset(1'000'000).value(),
225 }),
226 Ter(std::ignore));
227 env.close();
228
229 auto const before = read(env, f);
230 if (!BEAST_EXPECT(before.assetsAvailable > Number{0}))
231 return;
232
233 STAmount const request = asset(before.assetsAvailable + Number{1}).value();
234 env(v.withdraw({.depositor = f.depositor, .id = f.vaultKeylet.key, .amount = request}),
235 Ter(std::ignore));
236 env.close();
237
238 BEAST_EXPECTS(
239 env.ter() == tecINSUFFICIENT_FUNDS,
240 std::string{"expected tecINSUFFICIENT_FUNDS, got "} + transToken(env.ter()));
241 }
242
243 void
244 testClawback()
245 {
246 using namespace jtx;
247
248 testcase("clawback deltas are equal");
249
250 Env env = makeEnv();
251 auto f = setupSingleLoanVault(
252 env,
253 /*impairAndPaySibling=*/false,
254 /*allowClawback=*/true);
255 if (!ready(f))
256 return;
257 // ready() above guarantees f.asset is engaged; the guard is opaque to clang-tidy.
258 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
259 jtx::PrettyAsset const& asset = f.asset.value();
260
261 Vault const v{env};
262 env(v.deposit({
263 .depositor = f.depositor,
264 .id = f.vaultKeylet.key,
265 .amount = asset(2'000).value(),
266 }),
267 Ter(std::ignore));
268 env.close();
269
270 auto const checkSuccess = [&](std::optional<STAmount> const& amount,
271 std::string const& tag) {
272 auto const before = read(env, f);
273 if (before.sharesTotal == Number{0})
274 return;
275
276 env(v.clawback({
277 .issuer = f.issuer,
278 .id = f.vaultKeylet.key,
279 .holder = f.depositor,
280 .amount = amount,
281 }),
282 Ter(std::ignore));
283 env.close();
284 if (env.ter() != tesSUCCESS)
285 return;
286
287 assertEqualDeltas(before, read(env, f), tag);
288 };
289
290 std::array<int, 3> const kAmounts{1, 7, 99};
291 for (auto const amount : kAmounts)
292 checkSuccess(asset(amount).value(), "amount=" + std::to_string(amount));
293
294 checkSuccess(std::nullopt, "sfAmount absent");
295 }
296
297 void
298 testImpairedVault()
299 {
300 using namespace jtx;
301
302 testcase("impaired vault loss stays within assetsTotal - assetsAvailable");
303
304 Env env = makeEnv();
305 auto f = setupSingleLoanVault(env, /*impairAndPaySibling=*/true);
306 if (!ready(f))
307 return;
308 // ready() above guarantees f.asset is engaged; the guard is opaque to clang-tidy.
309 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
310 jtx::PrettyAsset const& asset = f.asset.value();
311
312 Vault const v{env};
313 env(v.deposit({
314 .depositor = f.depositor,
315 .id = f.vaultKeylet.key,
316 .amount = asset(5'000).value(),
317 }),
318 Ter(std::ignore));
319 env.close();
320
321 auto const checkInvariant = [&](std::string const& tag) {
322 TER const actual = env.ter();
323 BEAST_EXPECTS(actual != tecINVARIANT_FAILED, tag + " unexpected invariant failure");
324 if (actual != tesSUCCESS)
325 return;
326 auto const after = read(env, f);
327 BEAST_EXPECTS(
328 after.lossUnrealized <= after.assetsTotal - after.assetsAvailable,
329 tag + " lossUnrealized exceeds assetsTotal - assetsAvailable");
330 };
331
332 std::array<int, 4> const kAmounts{1, 7, 51, 137};
333 for (std::size_t i = 0; i + 1 < kAmounts.size(); i += 2)
334 {
335 int const depositAmount = kAmounts[i];
336 int const withdrawAmount = kAmounts[i + 1];
337
338 env(v.deposit({
339 .depositor = f.depositor,
340 .id = f.vaultKeylet.key,
341 .amount = asset(depositAmount).value(),
342 }),
343 Ter(std::ignore));
344 env.close();
345 checkInvariant("deposit=" + std::to_string(depositAmount));
346
347 env(v.withdraw({
348 .depositor = f.depositor,
349 .id = f.vaultKeylet.key,
350 .amount = asset(withdrawAmount).value(),
351 }),
352 Ter(std::ignore));
353 env.close();
354 checkInvariant("withdraw=" + std::to_string(withdrawAmount));
355 }
356 }
357
358public:
359 void
360 run() override
361 {
362 testDeposit();
363 testWithdraw();
364 testWithdrawInsufficientFundsPrecedence();
365 testClawback();
366 testImpairedVault();
367 }
368};
369
370BEAST_DEFINE_TESTSUITE(VaultTransactorPrecision, app, xrpl);
371
372} // namespace xrpl::test
bool unexpected(Condition shouldBeFalse, String const &reason)
DEPRECATED.
Definition suite.h:516
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)
void assertEqualDeltas(Numbers const &before, Numbers const &after, std::string const &tag)
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
Deposit preauthorize operations.
Definition deposit.h:16
bool equal(STAmount const &sa1, STAmount const &sa2)
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition envconfig.h:38
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
@ tecINSUFFICIENT_FUNDS
Definition TER.h:336
@ tecPRECISION_LOSS
Definition TER.h:374
@ tesSUCCESS
Definition TER.h:253
std::optional< jtx::PrettyAsset > asset
static json::Value withdraw(WithdrawArgs const &args)
Definition vault.cpp:101
static json::Value deposit(DepositArgs const &args)
Definition vault.cpp:90
T to_string(T... args)