xrpld
Loading...
Searching...
No Matches
VaultCoarsenedFixedPrecision_test.cpp
1#include <test/app/lending/LoanPayFixedPrecisionBase.h>
2#include <test/jtx/Env.h>
3#include <test/jtx/amount.h>
4#include <test/jtx/ter.h>
5#include <test/jtx/vault.h>
6
7#include <xrpl/basics/Number.h>
8#include <xrpl/basics/chrono.h>
9#include <xrpl/beast/unit_test/suite.h>
10#include <xrpl/beast/utility/Zero.h>
11#include <xrpl/ledger/helpers/VaultHelpers.h>
12#include <xrpl/protocol/Keylet.h>
13#include <xrpl/protocol/STAmount.h>
14#include <xrpl/protocol/TER.h>
15
16#include <chrono>
17#include <string>
18
19namespace xrpl {
20
21// Vault transactions (withdraw, clawback, deposit, ValidVault) on a
22// FixedPrecision Vault that LoanPay late interest has coarsened past its base
23// grid. The coarsening needs FixedPrecision LoanPay, so these live with it.
25{
26 void
28 {
29 using namespace test::jtx;
30
32 "Lending: VaultWithdraw and VaultClawback of an amount with a digit on AA's grid "
33 "but not AT's succeed exactly");
34
35 Env env(*this, features());
36 auto const [issuer, owner, depositor, asset] = setupCoarsenIou(env, true);
37
38 auto const coarsened = coarsenVault(env, owner, depositor, asset, {.overdueDays = 200});
39 auto const& vaultKeylet = coarsened.fixture.vaultKeylet;
40 expectCoarsened(env, coarsened);
41
42 // Withdrawals need the Redemption phase.
43 env.close(coarsened.fixture.redemptionDate + std::chrono::seconds{1});
44
45 struct Row
46 {
48 char const* label = nullptr;
49 // Off-grid at AA's base grid (1e-10).
50 Number amount;
51 };
52 Row const rows[] = {
53 {.kind = Debit::Withdraw, .label = "withdraw", .amount = Number{123'0000000007LL, -10}},
54 {.kind = Debit::Clawback, .label = "clawback", .amount = Number{111'0000000003LL, -10}},
55 };
56 for (auto const& row : rows)
57 {
58 Number const delta =
59 debitAvailable(env, coarsened, row.kind, issuer, depositor, asset, row.amount);
60 // The share round-trip can trim the delta, but it must keep AA's finer grid
61 // precision, not collapse to the AssetsTotal grid (-9).
62 BEAST_EXPECT(delta > beast::kZero);
63 BEAST_EXPECT(delta <= row.amount);
64 BEAST_EXPECT(delta != roundToAsset(asset, delta, -9, Number::RoundingMode::Downward));
65 auto const after = snapshotVault(env, vaultKeylet, asset);
66 BEAST_EXPECT(after.available <= after.total);
67
69 env,
70 coarsened,
71 std::string{"testCoarsenedVaultWithdrawClawbackKeepAvailableGridDigit ("} +
72 row.label + ")");
73 }
74 }
75
76 void
78 {
79 using namespace test::jtx;
80
82 "Lending: VaultWithdraw and VaultClawback smaller than half a unit of the "
83 "AssetsTotal cache's grid still succeed");
84
85 Env env(*this, features());
86 auto const [issuer, owner, depositor, asset] = setupCoarsenIou(env, true);
87
88 auto const coarsened = coarsenVault(env, owner, depositor, asset, {.overdueDays = 200});
89 auto const vaultSle0 = env.le(coarsened.fixture.vaultKeylet);
90 if (!BEAST_EXPECT(vaultSle0))
91 return;
92 // The AssetsTotal cache grid (-9) is coarser than the base grid, and AA
93 // itself still sits on the base grid (-10).
94 BEAST_EXPECT(getVaultScale(vaultSle0) == -9);
95 BEAST_EXPECT(getVaultBaseScale(vaultSle0) == -10);
96
97 // Withdrawals need the Redemption phase.
98 env.close(coarsened.fixture.redemptionDate + std::chrono::seconds{1});
99
100 struct Row
101 {
102 Debit kind = Debit::Withdraw;
103 char const* label = nullptr;
104 // On AA's base grid (-10) but under half a unit of AssetsTotal's grid (5e-10).
105 Number amount;
106 };
107 Row const rows[] = {
108 {.kind = Debit::Withdraw, .label = "withdraw", .amount = Number{2, -10}},
109 {.kind = Debit::Clawback, .label = "clawback", .amount = Number{3, -10}},
110 };
111 for (auto const& row : rows)
112 {
113 // Rounding Downward at -9 gives exactly zero.
114 BEAST_EXPECT(
115 roundToAsset(asset, row.amount, -9, Number::RoundingMode::Downward) ==
117
118 Number const delta =
119 debitAvailable(env, coarsened, row.kind, issuer, depositor, asset, row.amount);
120 // The delta may be trimmed by the share round-trip. It must stay strictly
121 // positive and sub-(AT-grid); the bug rejected it outright.
122 BEAST_EXPECT(delta > beast::kZero);
123 BEAST_EXPECT(delta <= row.amount);
124 BEAST_EXPECT(
126
128 env,
129 coarsened,
130 std::string{"testCoarsenedVaultSubUnitWithdrawClawbackSucceeds ("} + row.label +
131 ")");
132 }
133 }
134
135 void
137 {
138 using namespace test::jtx;
139
140 testcase(
141 "Lending: VaultWithdraw and VaultClawback debits that cross a power of ten keep "
142 "AssetsAvailable at 16 digits");
143
144 Env env(*this, features());
145 auto const [issuer, owner, depositor, asset] = setupCoarsenIou(env, true);
146
147 auto const coarsened = coarsenVault(env, owner, depositor, asset, {.overdueDays = 200});
148 auto const& vaultKeylet = coarsened.fixture.vaultKeylet;
149 expectCoarsened(env, coarsened);
150
151 // Withdrawals need the Redemption phase.
152 env.close(coarsened.fixture.redemptionDate + std::chrono::seconds{1});
153
154 // A margin, since the share round-trip can trim the delta short of the
155 // requested amount.
156 Number const margin{100};
157
158 // The clawback runs on the post-withdraw vault, crossing a fresh boundary.
159 struct Row
160 {
161 Debit kind = Debit::Withdraw;
162 char const* label = nullptr;
163 };
164 Row const rows[] = {
165 {.kind = Debit::Withdraw, .label = "withdraw"},
166 {.kind = Debit::Clawback, .label = "clawback"},
167 };
168 for (auto const& row : rows)
169 {
170 Number const availableBefore = snapshotVault(env, vaultKeylet, asset).available;
171
172 // The largest power of ten below AssetsAvailable; crossing it downward drops
173 // a digit from the integer part.
174 Number const powerOfTen = largestPowerOfTenAtMost(availableBefore);
175
176 // One transaction spans the whole gap; the AssetsAvailable-delta check is
177 // exact against the real transfer.
178 Number const amount = availableBefore - powerOfTen + margin;
179 BEAST_EXPECT(amount > beast::kZero);
180 BEAST_EXPECT(amount < availableBefore);
181
182 debitAvailable(env, coarsened, row.kind, issuer, depositor, asset, amount);
183
184 BEAST_EXPECT(expectAvailableAtSixteenDigits(env, vaultKeylet, asset) < powerOfTen);
185
187 env,
188 coarsened,
189 std::string{"testCoarsenedVaultDebitCrossesPowerOfTenDownward ("} + row.label +
190 ")");
191 }
192 }
193
194 void
196 {
197 using namespace test::jtx;
198
199 testcase(
200 "Lending: a single large VaultWithdraw/VaultClawback across the AssetsTotal grid "
201 "boundary succeeds (ValidVault false-positive regression)");
202
203 Number const margin{1'000};
204 Number const powerOfTen{100'000};
205
206 struct Row
207 {
208 Debit kind = Debit::Withdraw;
209 char const* label = nullptr;
210 };
211 // Each debit runs on a fresh vault so AssetsTotal starts above its boundary
212 // (after a shrink it would be dominated by AssetsDeployed).
213 Row const rows[] = {
214 {.kind = Debit::Withdraw, .label = "withdraw"},
215 {.kind = Debit::Clawback, .label = "clawback"},
216 };
217 for (auto const& row : rows)
218 {
219 Env env(*this, features());
220 auto const [issuer, owner, depositor, asset] = setupCoarsenIou(env, true);
221
222 // Setup from the original report: AssetsAvailable 723562.1976154236,
223 // AssetsDeployed 560000.
224 auto const coarsened = coarsenVault(env, owner, depositor, asset, {.overdueDays = 200});
225 auto const& vaultKeylet = coarsened.fixture.vaultKeylet;
226 expectCoarsened(env, coarsened);
227
228 env.close(coarsened.fixture.redemptionDate + std::chrono::seconds{1});
229
230 auto const before = snapshotVault(env, vaultKeylet, asset);
231 // The stored AssetsTotal crosses its own power-of-ten boundary, not
232 // necessarily AssetsAvailable's, since AssetsDeployed adds magnitude.
233 Number const powerOfTenAT = largestPowerOfTenAtMost(before.total);
234 BEAST_EXPECT(before.total >= powerOfTenAT);
235
236 // One debit takes AssetsAvailable below its boundary and carries AssetsTotal
237 // across its own boundary in the same transaction.
239 env,
240 coarsened,
241 row.kind,
242 issuer,
243 depositor,
244 asset,
245 before.available - powerOfTen + margin);
246
247 BEAST_EXPECT(snapshotVault(env, vaultKeylet, asset).total < powerOfTenAT);
249 env,
250 coarsened,
252 "testValidVaultLargeSingleDebitAcrossAssetsTotalGridDoesNotFalsePositive ("} +
253 row.label + ")");
254 }
255 }
256
257 void
259 {
260 using namespace test::jtx;
261
262 testcase(
263 "Lending: VaultDeposit is refused on a vault coarsened via AssetsDeployed while "
264 "AssetsAvailable is still uncoarsened -- always via the Investment-phase gate, "
265 "not the precision-loss check");
266
267 Env env(*this, features());
268 auto const [issuer, owner, depositor, asset] = setupCoarsenIou(env);
269
270 auto const coarsened = coarsenVault(env, owner, depositor, asset, {.overdueDays = 200});
271 auto const& vaultKeylet = coarsened.fixture.vaultKeylet;
272
273 auto const vaultSle = env.le(vaultKeylet);
274 if (!BEAST_EXPECT(vaultSle))
275 return;
276 BEAST_EXPECT(getVaultScale(vaultSle) == -9);
277 BEAST_EXPECT(getVaultBaseScale(vaultSle) == -10);
278
279 // Off-grid at the coarsened live scale (-9) but on AA's base grid (-10).
280 Number const depositAmount{2, -10};
281 BEAST_EXPECT(
282 roundToAsset(asset, depositAmount, -9, Number::RoundingMode::Downward) == beast::kZero);
283
284 // An AssetsDeployed-coarsened vault always has a LoanBroker, so it is closed-ended
285 // and in the Investment phase while debt is outstanding. VaultDeposit refuses
286 // there before the precision-loss check, so this amount fails with tecEXPIRED,
287 // not tecPRECISION_LOSS. See the VaultBalance.* gtest cases for the bare-SLE
288 // precision check.
289 env(coarsened.fixture.vault.deposit(
290 {.depositor = depositor, .id = vaultKeylet.key, .amount = asset(depositAmount)}),
291 Ter(tecEXPIRED));
292 env.close();
293
294 checkVaultLoanSums(env, coarsened, "testDepositRefusedOnDebtCoarsenedVault");
295 }
296
297public:
298 void
307};
308
309BEAST_DEFINE_TESTSUITE(VaultCoarsenedFixedPrecision, app, xrpl);
310
311} // namespace xrpl
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
CoarsenedVault coarsenVault(test::jtx::Env &env, test::jtx::Account const &owner, test::jtx::Account const &depositor, PrettyAsset const &asset)
static AssetAccounts setupCoarsenIou(test::jtx::Env &env, bool clawback=false)
void expectCoarsened(test::jtx::Env const &env, CoarsenedVault const &coarsened)
Number debitAvailable(test::jtx::Env &env, CoarsenedVault const &coarsened, Debit kind, test::jtx::Account const &issuer, test::jtx::Account const &depositor, PrettyAsset const &asset, Number const &amount)
void checkVaultLoanSums(test::jtx::Env &env, CoarsenedVault const &coarsened, std::string const &label={})
Number is a floating point type that can represent a wide range of values.
Definition Number.h:351
VaultSnapshot snapshotVault(test::jtx::Env &env, Keylet const &vaultKeylet, PrettyAsset const &asset)
static Number largestPowerOfTenAtMost(Number const &x)
Number expectAvailableAtSixteenDigits(test::jtx::Env const &env, Keylet const &vaultKeylet, PrettyAsset const &asset)
constexpr Zero kZero
Definition Zero.h:30
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
int getVaultScale(SLE::ConstRef vault)
Return the Vault's current live exponent.
bool after(NetClock::time_point now, std::uint32_t mark)
Has the specified time passed?
Definition View.cpp:668
int getVaultBaseScale(SLE::ConstRef vault)
Return the Vault's base exponent.
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
Definition STAmount.h:745
@ tecEXPIRED
Definition TER.h:325
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, xrpl)