xrpld
Loading...
Searching...
No Matches
InvariantsPermissioned_test.cpp
1#include <test/app/invariants/InvariantsBase.h>
2#include <test/jtx/Account.h>
3#include <test/jtx/Env.h>
4#include <test/jtx/amount.h>
5#include <test/jtx/permissioned_domains.h>
6#include <test/jtx/vault.h>
7#include <test/unit_test/SuiteJournal.h>
8
9#include <xrpl/basics/Slice.h>
10#include <xrpl/basics/base_uint.h>
11#include <xrpl/beast/unit_test/suite.h>
12#include <xrpl/beast/utility/Journal.h>
13#include <xrpl/ledger/ApplyView.h>
14#include <xrpl/ledger/OpenView.h>
15#include <xrpl/protocol/Book.h>
16#include <xrpl/protocol/Feature.h>
17#include <xrpl/protocol/Indexes.h>
18#include <xrpl/protocol/Issue.h>
19#include <xrpl/protocol/Keylet.h>
20#include <xrpl/protocol/LedgerFormats.h>
21#include <xrpl/protocol/Protocol.h>
22#include <xrpl/protocol/Rules.h>
23#include <xrpl/protocol/SField.h>
24#include <xrpl/protocol/STAmount.h>
25#include <xrpl/protocol/STArray.h>
26#include <xrpl/protocol/STLedgerEntry.h>
27#include <xrpl/protocol/STObject.h>
28#include <xrpl/protocol/STTx.h>
29#include <xrpl/protocol/SeqProxy.h>
30#include <xrpl/protocol/TER.h>
31#include <xrpl/protocol/TxFormats.h>
32#include <xrpl/protocol/XRPAmount.h>
33#include <xrpl/tx/ApplyContext.h>
34#include <xrpl/tx/applySteps.h>
35#include <xrpl/tx/invariants/DirectoryInvariant.h>
36#include <xrpl/tx/invariants/PermissionedDEXInvariant.h>
37
38#include <cstddef>
39#include <cstdint>
40#include <functional>
41#include <initializer_list>
42#include <memory>
43#include <optional>
44#include <string>
45#include <utility>
46#include <vector>
47
48namespace xrpl::test {
49
51{
53
54 void
56 {
57 using namespace test::jtx;
58
59 bool const fixEnabled = features[fixCleanup3_1_3];
62
63 testcase << "PermissionedDomain" + std::string(fixEnabled ? " fix" : "");
64
66 makeEnv(features),
67 {{"permissioned domain with no rules."}},
68 [](Account const& a1, Account const& a2, ApplyContext& ac) {
69 return createPermissionedDomain(ac, a1, a2, 0).get();
70 },
71 XRPAmount{},
72 STTx{ttPERMISSIONED_DOMAIN_SET, [](STObject&) {}},
73 fixEnabled ? failTers : badTers);
74
75 testcase << "PermissionedDomain 2";
76
77 static constexpr auto kTooBig = kMaxPermissionedDomainCredentialsArraySize + 1;
79 makeEnv(features),
80 {{"permissioned domain bad credentials size " + std::to_string(kTooBig)}},
81 [](Account const& a1, Account const& a2, ApplyContext& ac) {
82 return !!createPermissionedDomain(ac, a1, a2, kTooBig);
83 },
84 XRPAmount{},
85 STTx{ttPERMISSIONED_DOMAIN_SET, [](STObject&) {}},
86 fixEnabled ? failTers : badTers);
87
88 testcase << "PermissionedDomain 3";
90 makeEnv(features),
91 {{"permissioned domain credentials aren't sorted"}},
92 [](Account const& a1, Account const& a2, ApplyContext& ac) {
93 auto const slePd = createPermissionedDomain(ac, a1, a2, 0);
94
95 STArray credentials(sfAcceptedCredentials, 2);
96 for (std::size_t n = 0; n < 2; ++n)
97 {
98 auto cred = STObject::makeInnerObject(sfCredential);
99 cred.setAccountID(sfIssuer, a2);
100 auto const credType = std::string("cred_type") + std::to_string(9 - n);
101 cred.setFieldVL(sfCredentialType, Slice(credType.c_str(), credType.size()));
102 credentials.pushBack(std::move(cred));
103 }
104 slePd->setFieldArray(sfAcceptedCredentials, credentials);
105 ac.view().update(slePd);
106 return true;
107 },
108 XRPAmount{},
109 STTx{ttPERMISSIONED_DOMAIN_SET, [](STObject&) {}},
110 fixEnabled ? failTers : badTers);
111
112 testcase << "PermissionedDomain 4";
114 makeEnv(features),
115 {{"permissioned domain credentials aren't unique"}},
116 [](Account const& a1, Account const& a2, ApplyContext& ac) {
117 auto const slePd = createPermissionedDomain(ac, a1, a2, 0);
118
119 STArray credentials(sfAcceptedCredentials, 2);
120 for (std::size_t n = 0; n < 2; ++n)
121 {
122 auto cred = STObject::makeInnerObject(sfCredential);
123 cred.setAccountID(sfIssuer, a2);
124 cred.setFieldVL(sfCredentialType, Slice("cred_type", 9));
125 credentials.pushBack(std::move(cred));
126 }
127 slePd->setFieldArray(sfAcceptedCredentials, credentials);
128 ac.view().update(slePd);
129 return true;
130 },
131 XRPAmount{},
132 STTx{ttPERMISSIONED_DOMAIN_SET, [](STObject&) {}},
133 fixEnabled ? failTers : badTers);
134
135 testcase << "PermissionedDomain Set 1";
137 makeEnv(features),
138 {{"permissioned domain with no rules."}},
139 [&](Account const& a1, Account const& a2, ApplyContext& ac) {
140 // create PD
141 auto const slePd = createPermissionedDomain(ac, a1, a2);
142
143 // update PD with empty rules
144 {
145 STArray const credentials(sfAcceptedCredentials, 2);
146 slePd->setFieldArray(sfAcceptedCredentials, credentials);
147 ac.view().update(slePd);
148 }
149
150 return true;
151 },
152 XRPAmount{},
153 STTx{ttPERMISSIONED_DOMAIN_SET, [](STObject&) {}},
154 fixEnabled ? failTers : badTers);
155
156 testcase << "PermissionedDomain Set 2";
158 makeEnv(features),
159 {{"permissioned domain bad credentials size " + std::to_string(kTooBig)}},
160 [&](Account const& a1, Account const& a2, ApplyContext& ac) {
161 // create PD
162 auto const slePd = createPermissionedDomain(ac, a1, a2);
163
164 // update PD
165 {
166 STArray credentials(sfAcceptedCredentials, kTooBig);
167
168 for (std::size_t n = 0; n < kTooBig; ++n)
169 {
170 auto cred = STObject::makeInnerObject(sfCredential);
171 cred.setAccountID(sfIssuer, a2);
172 auto const credType = "cred_type2" + std::to_string(n);
173 cred.setFieldVL(sfCredentialType, Slice(credType.c_str(), credType.size()));
174 credentials.pushBack(std::move(cred));
175 }
176
177 slePd->setFieldArray(sfAcceptedCredentials, credentials);
178 ac.view().update(slePd);
179 }
180
181 return true;
182 },
183 XRPAmount{},
184 STTx{ttPERMISSIONED_DOMAIN_SET, [](STObject&) {}},
185 fixEnabled ? failTers : badTers);
186
187 testcase << "PermissionedDomain Set 3";
189 makeEnv(features),
190 {{"permissioned domain credentials aren't sorted"}},
191 [&](Account const& a1, Account const& a2, ApplyContext& ac) {
192 // create PD
193 auto const slePd = createPermissionedDomain(ac, a1, a2);
194
195 // update PD
196 {
197 STArray credentials(sfAcceptedCredentials, 2);
198 for (std::size_t n = 0; n < 2; ++n)
199 {
200 auto cred = STObject::makeInnerObject(sfCredential);
201 cred.setAccountID(sfIssuer, a2);
202 auto const credType = std::string("cred_type2") + std::to_string(9 - n);
203 cred.setFieldVL(sfCredentialType, Slice(credType.c_str(), credType.size()));
204 credentials.pushBack(std::move(cred));
205 }
206
207 slePd->setFieldArray(sfAcceptedCredentials, credentials);
208 ac.view().update(slePd);
209 }
210
211 return true;
212 },
213 XRPAmount{},
214 STTx{ttPERMISSIONED_DOMAIN_SET, [](STObject&) {}},
215 fixEnabled ? failTers : badTers);
216
217 testcase << "PermissionedDomain Set 4";
219 makeEnv(features),
220 {{"permissioned domain credentials aren't unique"}},
221 [&](Account const& a1, Account const& a2, ApplyContext& ac) {
222 // create PD
223 auto const slePd = createPermissionedDomain(ac, a1, a2);
224
225 // update PD
226 {
227 STArray credentials(sfAcceptedCredentials, 2);
228 for (std::size_t n = 0; n < 2; ++n)
229 {
230 auto cred = STObject::makeInnerObject(sfCredential);
231 cred.setAccountID(sfIssuer, a2);
232 cred.setFieldVL(sfCredentialType, Slice("cred_type", 9));
233 credentials.pushBack(std::move(cred));
234 }
235 slePd->setFieldArray(sfAcceptedCredentials, credentials);
236 ac.view().update(slePd);
237 }
238
239 return true;
240 },
241 XRPAmount{},
242 STTx{ttPERMISSIONED_DOMAIN_SET, [](STObject&) {}},
243 fixEnabled ? failTers : badTers);
244
246
247 std::vector<std::string> const badMoreThan1{
248 {"transaction affected more than 1 permissioned domain entry."},
249 };
250 std::vector<std::string> const emptyV;
251 std::vector<std::string> const badNoDomains{{"no domain objects affected by"}};
252 std::vector<std::string> const badNotDeleted{
253 {"domain object modified, but not deleted by "},
254 };
255 std::vector<std::string> const badDeleted{{"domain object deleted by"}};
256 std::vector<std::string> const badTx{
257 {"domain object(s) affected by an unauthorized transaction."},
258 };
259
260 {
261 testcase << "PermissionedDomain set 2 domains ";
263 makeEnv(features),
264 fixEnabled ? badMoreThan1 : emptyV,
265 [](Account const& a1, Account const& a2, ApplyContext& ac) {
266 createPermissionedDomain(ac, a1, a2);
267 createPermissionedDomain(ac, a1, a2, 2, 11);
268 return true;
269 },
270 XRPAmount{},
271 STTx{ttPERMISSIONED_DOMAIN_SET, [](STObject&) {}},
272 fixEnabled ? failTers : goodTers);
273 }
274
275 {
276 testcase << "PermissionedDomain del 2 domains";
277
278 Env env1(*this, features);
279
280 Account const a1{"A1"};
281 Account const a2{"A2"};
282 env1.fund(XRP(1000), a1, a2);
283 env1.close();
284
285 [[maybe_unused]] auto [seq1, pd1] = createPermissionedDomainEnv(env1, a1, a2);
286 [[maybe_unused]] auto [seq2, pd2] = createPermissionedDomainEnv(env1, a1, a2);
287 env1.close();
288
290 std::move(env1),
291 a1,
292 a2,
293 fixEnabled ? badMoreThan1 : emptyV,
294 [&pd1, &pd2](Account const&, Account const&, ApplyContext& ac) {
295 auto const sle1 = ac.view().peek({ltPERMISSIONED_DOMAIN, pd1});
296 auto const sle2 = ac.view().peek({ltPERMISSIONED_DOMAIN, pd2});
297 ac.view().erase(sle1);
298 ac.view().erase(sle2);
299 return true;
300 },
301 XRPAmount{},
302 STTx{ttPERMISSIONED_DOMAIN_DELETE, [](STObject&) {}},
303 fixEnabled ? failTers : goodTers);
304 }
305
306 {
307 testcase << "PermissionedDomain set 0 domains ";
309 makeEnv(features),
310 fixEnabled ? badNoDomains : emptyV,
311 [](Account const&, Account const&, ApplyContext&) { return true; },
312 XRPAmount{},
313 STTx{ttPERMISSIONED_DOMAIN_SET, [](STObject&) {}},
314 fixEnabled ? badTers : goodTers);
315 }
316
317 {
318 testcase << "PermissionedDomain del 0 domains";
319
320 Env env1(*this, features);
321
322 Account const a1{"A1"};
323 Account const a2{"A2"};
324 env1.fund(XRP(1000), a1, a2);
325 env1.close();
326
327 [[maybe_unused]] auto [seq1, pd1] = createPermissionedDomainEnv(env1, a1, a2);
328 [[maybe_unused]] auto [seq2, pd2] = createPermissionedDomainEnv(env1, a1, a2);
329 env1.close();
330
332 std::move(env1),
333 a1,
334 a2,
335 fixEnabled ? badNoDomains : emptyV,
336 [](Account const&, Account const&, ApplyContext&) { return true; },
337 XRPAmount{},
338 STTx{ttPERMISSIONED_DOMAIN_DELETE, [](STObject&) {}},
339 fixEnabled ? badTers : goodTers);
340 }
341
342 {
343 testcase << "PermissionedDomain set, delete domain";
344
345 Env env1(*this, features);
346
347 Account const a1{"A1"};
348 Account const a2{"A2"};
349 env1.fund(XRP(1000), a1, a2);
350 env1.close();
351
352 [[maybe_unused]] auto [seq1, pd1] = createPermissionedDomainEnv(env1, a1, a2);
353 env1.close();
354
356 std::move(env1),
357 a1,
358 a2,
359 fixEnabled ? badDeleted : emptyV,
360 [&pd1](Account const&, Account const&, ApplyContext& ac) {
361 auto const sle1 = ac.view().peek({ltPERMISSIONED_DOMAIN, pd1});
362 ac.view().erase(sle1);
363 return true;
364 },
365 XRPAmount{},
366 STTx{ttPERMISSIONED_DOMAIN_SET, [](STObject&) {}},
367 fixEnabled ? failTers : goodTers);
368 }
369
370 {
371 testcase << "PermissionedDomain del, create domain ";
373 makeEnv(features),
374 fixEnabled ? badNotDeleted : emptyV,
375 [](Account const& a1, Account const& a2, ApplyContext& ac) {
376 createPermissionedDomain(ac, a1, a2);
377 return true;
378 },
379 XRPAmount{},
380 STTx{ttPERMISSIONED_DOMAIN_DELETE, [](STObject&) {}},
381 fixEnabled ? failTers : goodTers);
382 }
383
384 {
385 testcase << "PermissionedDomain invalid tx";
386
388 fixEnabled ? badTx : emptyV,
389 [&](Account const& a1, Account const& a2, ApplyContext& ac) {
390 createPermissionedDomain(ac, a1, a2);
391 return true;
392 },
393 XRPAmount{},
394 STTx{ttPAYMENT, [](STObject&) {}},
395 failTers);
396 }
397 }
398
399 void
401 {
402 using namespace test::jtx;
403
404 bool const fixEnabled = features[fixCleanup3_1_3];
405
406 testcase << "PermissionedDEX" + std::string(fixEnabled ? " fix" : "");
407
409 makeEnv(features),
410 {{"domain doesn't exist"}},
411 [](Account const& a1, Account const&, ApplyContext& ac) {
412 Keylet const offerKey = keylet::offer(a1.id(), SeqProxy::rawSequence(10));
413 auto const sleOffer = std::make_shared<SLE>(offerKey);
414 sleOffer->setAccountID(sfAccount, a1);
415 sleOffer->setFieldAmount(sfTakerPays, a1["USD"](10));
416 sleOffer->setFieldAmount(sfTakerGets, XRP(1));
417 ac.view().insert(sleOffer);
418 return true;
419 },
420 XRPAmount{},
421 STTx{
422 ttOFFER_CREATE,
423 [](STObject& tx) {
424 tx.setFieldH256(
425 sfDomainID,
426 UInt256{"F10D0CC9A0F9A3CBF585B80BE09A186483668FDBDD39AA7E33"
427 "70F3649CE134E5"});
428 Account const a1{"A1"};
429 tx.setFieldAmount(sfTakerPays, a1["USD"](10));
430 tx.setFieldAmount(sfTakerGets, XRP(1));
431 }},
433
434 // missing domain ID in offer object
436 makeEnv(features),
437 {{"hybrid offer is malformed"}},
438 [&](Account const& a1, Account const& a2, ApplyContext& ac) {
439 Keylet const offerKey = keylet::offer(a2.id(), SeqProxy::rawSequence(10));
440 auto const sleOffer = std::make_shared<SLE>(offerKey);
441 sleOffer->setAccountID(sfAccount, a2);
442 sleOffer->setFieldAmount(sfTakerPays, a1["USD"](10));
443 sleOffer->setFieldAmount(sfTakerGets, XRP(1));
444 sleOffer->setFlag(lsfHybrid);
445
446 STArray bookArr;
447 bookArr.pushBack(STObject::makeInnerObject(sfBook));
448 sleOffer->setFieldArray(sfAdditionalBooks, bookArr);
449 ac.view().insert(sleOffer);
450 return true;
451 },
452 XRPAmount{},
453 STTx{ttOFFER_CREATE, [&](STObject&) {}},
455
456 // more than one entry in sfAdditionalBooks
457 {
458 Env env1(*this, features);
459
460 Account const a1{"A1"};
461 Account const a2{"A2"};
462 env1.fund(XRP(1000), a1, a2);
463 env1.close();
464
465 [[maybe_unused]] auto [seq1, pd1] = createPermissionedDomainEnv(env1, a1, a2);
466 env1.close();
467
469 std::move(env1),
470 a1,
471 a2,
472 {{"hybrid offer is malformed"}},
473 [&pd1](Account const& a1, Account const& a2, ApplyContext& ac) {
474 Keylet const offerKey = keylet::offer(a2.id(), SeqProxy::rawSequence(10));
475 auto const sleOffer = std::make_shared<SLE>(offerKey);
476 sleOffer->setAccountID(sfAccount, a2);
477 sleOffer->setFieldAmount(sfTakerPays, a1["USD"](10));
478 sleOffer->setFieldAmount(sfTakerGets, XRP(1));
479 sleOffer->setFlag(lsfHybrid);
480 sleOffer->setFieldH256(sfDomainID, pd1);
481
482 STArray bookArr;
483 bookArr.pushBack(STObject::makeInnerObject(sfBook));
484 bookArr.pushBack(STObject::makeInnerObject(sfBook));
485 sleOffer->setFieldArray(sfAdditionalBooks, bookArr);
486 ac.view().insert(sleOffer);
487 return true;
488 },
489 XRPAmount{},
490 STTx{ttOFFER_CREATE, [&](STObject&) {}},
492 }
493
494 // empty sfAdditionalBooks (size 0)
495 {
496 Env env1(*this, features);
497
498 Account const a1{"A1"};
499 Account const a2{"A2"};
500 env1.fund(XRP(1000), a1, a2);
501 env1.close();
502
503 [[maybe_unused]] auto [seq1, pd1] = createPermissionedDomainEnv(env1, a1, a2);
504 env1.close();
505
507 std::move(env1),
508 a1,
509 a2,
510 fixEnabled ? std::vector<std::string>{{"hybrid offer is malformed"}}
512 [&pd1](Account const& a1, Account const& a2, ApplyContext& ac) {
513 Keylet const offerKey = keylet::offer(a2.id(), SeqProxy::rawSequence(10));
514 auto const sleOffer = std::make_shared<SLE>(offerKey);
515 sleOffer->setAccountID(sfAccount, a2);
516 sleOffer->setFieldAmount(sfTakerPays, a1["USD"](10));
517 sleOffer->setFieldAmount(sfTakerGets, XRP(1));
518 sleOffer->setFlag(lsfHybrid);
519 sleOffer->setFieldH256(sfDomainID, pd1);
520
521 STArray const bookArr; // empty array, size 0
522 sleOffer->setFieldArray(sfAdditionalBooks, bookArr);
523 ac.view().insert(sleOffer);
524 return true;
525 },
526 XRPAmount{},
527 STTx{ttOFFER_CREATE, [&](STObject&) {}},
530 }
531
532 // hybrid offer missing sfAdditionalBooks
533 {
534 Env env1(*this, features);
535
536 Account const a1{"A1"};
537 Account const a2{"A2"};
538 env1.fund(XRP(1000), a1, a2);
539 env1.close();
540
541 [[maybe_unused]] auto [seq1, pd1] = createPermissionedDomainEnv(env1, a1, a2);
542 env1.close();
543
545 std::move(env1),
546 a1,
547 a2,
548 {{"hybrid offer is malformed"}},
549 [&pd1](Account const& a1, Account const& a2, ApplyContext& ac) {
550 Keylet const offerKey = keylet::offer(a2.id(), SeqProxy::rawSequence(10));
551 auto const sleOffer = std::make_shared<SLE>(offerKey);
552 sleOffer->setAccountID(sfAccount, a2);
553 sleOffer->setFieldAmount(sfTakerPays, a1["USD"](10));
554 sleOffer->setFieldAmount(sfTakerGets, XRP(1));
555 sleOffer->setFlag(lsfHybrid);
556 sleOffer->setFieldH256(sfDomainID, pd1);
557 ac.view().insert(sleOffer);
558 return true;
559 },
560 XRPAmount{},
561 STTx{ttOFFER_CREATE, [&](STObject&) {}},
563 }
564
565 {
566 Env env1(*this, features);
567
568 Account const a1{"A1"};
569 Account const a2{"A2"};
570 env1.fund(XRP(1000), a1, a2);
571 env1.close();
572
573 [[maybe_unused]] auto [seq1, pd1] = createPermissionedDomainEnv(env1, a1, a2);
574 [[maybe_unused]] auto [seq2, pd2] = createPermissionedDomainEnv(env1, a1, a2);
575 env1.close();
576
578 std::move(env1),
579 a1,
580 a2,
581 {{"transaction consumed wrong domains"}},
582 [&pd1](Account const& a1, Account const& a2, ApplyContext& ac) {
583 Keylet const offerKey = keylet::offer(a2.id(), SeqProxy::rawSequence(10));
584 auto const sleOffer = std::make_shared<SLE>(offerKey);
585 sleOffer->setAccountID(sfAccount, a2);
586 sleOffer->setFieldAmount(sfTakerPays, a1["USD"](10));
587 sleOffer->setFieldAmount(sfTakerGets, XRP(1));
588 sleOffer->setFieldH256(sfDomainID, pd1);
589 ac.view().insert(sleOffer);
590 return true;
591 },
592 XRPAmount{},
593 STTx{
594 ttOFFER_CREATE,
595 [&pd2, &a1](STObject& tx) {
596 tx.setFieldH256(sfDomainID, pd2);
597 tx.setFieldAmount(sfTakerPays, a1["USD"](10));
598 tx.setFieldAmount(sfTakerGets, XRP(1));
599 }},
601 }
602
603 {
604 Env env1(*this, features);
605
606 Account const a1{"A1"};
607 Account const a2{"A2"};
608 env1.fund(XRP(1000), a1, a2);
609 env1.close();
610
611 [[maybe_unused]] auto [seq1, pd1] = createPermissionedDomainEnv(env1, a1, a2);
612 env1.close();
613
615 std::move(env1),
616 a1,
617 a2,
618 {{"domain transaction affected regular offers"}},
619 [&](Account const& a1, Account const& a2, ApplyContext& ac) {
620 Keylet const offerKey = keylet::offer(a2.id(), SeqProxy::rawSequence(10));
621 auto const sleOffer = std::make_shared<SLE>(offerKey);
622 sleOffer->setAccountID(sfAccount, a2);
623 sleOffer->setFieldAmount(sfTakerPays, a1["USD"](10));
624 sleOffer->setFieldAmount(sfTakerGets, XRP(1));
625 ac.view().insert(sleOffer);
626 return true;
627 },
628 XRPAmount{},
629 STTx{
630 ttOFFER_CREATE,
631 [&](STObject& tx) {
632 Account const a1{"A1"};
633 tx.setFieldH256(sfDomainID, pd1);
634 tx.setFieldAmount(sfTakerPays, a1["USD"](10));
635 tx.setFieldAmount(sfTakerGets, XRP(1));
636 }},
638 }
639 }
640
641 void
643 {
644 using namespace test::jtx;
645
646 testcase << "PermissionedDEX null after";
647
648 // Tx is OfferCreate on pd2. Tracking pd1 fails the invariant iff that
649 // domain lands in the set finalize consults. after == null is never
650 // tracked (pre-340: after-only; post-340: early return) — same result,
651 // both sides are coverage/regression that we do not fall back to before.
652 auto const check = [this](
653 FeatureBitset features,
654 bool const afterIsNull,
655 bool const isDelete,
656 bool const expectInvariantFailure) {
657 Env env(*this, features);
658
659 Account const a1{"A1"};
660 Account const a2{"A2"};
661 env.fund(XRP(1000), a1, a2);
662 env.close();
663
664 [[maybe_unused]] auto [seq1, pd1] = createPermissionedDomainEnv(env, a1, a2);
665 [[maybe_unused]] auto [seq2, pd2] = createPermissionedDomainEnv(env, a1, a2);
666 env.close();
667
668 auto const sleOffer =
670 sleOffer->setAccountID(sfAccount, a2);
671 sleOffer->setFieldAmount(sfTakerPays, a1["USD"](10));
672 sleOffer->setFieldAmount(sfTakerGets, XRP(1));
673 sleOffer->setFieldH256(sfDomainID, pd1);
674
675 CurrentTransactionRulesGuard const rulesGuard(env.current()->rules());
676
677 ValidPermissionedDEX invariant;
678 if (afterIsNull)
679 {
680 // Defensive path: after is null. Must not fall back to before.
681 invariant.visitEntry(isDelete, sleOffer, nullptr);
682 }
683 else
684 {
685 // Normal / real-erase path: after is the offer on pd1.
686 invariant.visitEntry(isDelete, nullptr, sleOffer);
687 }
688
689 STTx const tx{ttOFFER_CREATE, [&pd2, &a1](STObject& tx) {
690 tx.setFieldH256(sfDomainID, pd2);
691 tx.setFieldAmount(sfTakerPays, a1["USD"](10));
692 tx.setFieldAmount(sfTakerGets, XRP(1));
693 }};
694
696 beast::Journal const jlog{sink};
697 bool const passed =
698 invariant.finalize(tx, tesSUCCESS, XRPAmount{}, *env.current(), jlog);
699 BEAST_EXPECT(passed != expectInvariantFailure);
700 if (expectInvariantFailure)
701 {
702 BEAST_EXPECT(sink.messages().str().contains("transaction consumed wrong domains"));
703 }
704 else
705 {
706 BEAST_EXPECT(sink.messages().str().empty());
707 }
708 };
709
710 auto const pre = all_ - fixCleanup3_4_0;
711 auto const post = all_;
712
713 // after == null: not tracked
714 check(pre, true, true, false);
715 check(post, true, true, false);
716
717 // after == offer on pd1
718 // pre-340: domainsOld_ (delete still inserted) → fail
719 check(pre, false, true, true);
720 // post-340: isDelete → only domainsOld_ → pass; !isDelete → domains_ → fail
721 check(post, false, true, false);
722 check(post, false, false, true);
723 }
724
725 void
727 {
728 using namespace test::jtx;
729 testcase << "book directory exchange rate";
730
731 auto const getBookRootKey = [](Account const& account, std::uint64_t quality) {
732 Book const book{xrpIssue(), account["USD"], std::nullopt};
733 return keylet::quality(keylet::book(book), quality);
734 };
735
736 // Root book-directory pages carry exchange-rate metadata that must
737 // match the quality encoded in the directory key.
738 auto const makeRootPage = [](Keylet const& dir, std::uint64_t exchangeRate) {
739 auto sleDir = std::make_shared<SLE>(dir);
740 sleDir->setFieldH256(sfRootIndex, dir.key);
741 STVector256 indexes;
742 indexes.pushBack(UInt256{1});
743 sleDir->setFieldV256(sfIndexes, indexes);
744 sleDir->setFieldU64(sfExchangeRate, exchangeRate);
745 return sleDir;
746 };
747
748 // Child pages do not carry quality metadata; they only point back to
749 // the root directory.
750 auto const makeChildPage = [](Keylet const& rootDir) {
751 auto sleDir = std::make_shared<SLE>(keylet::page(rootDir, 1));
752 sleDir->setFieldH256(sfRootIndex, rootDir.key);
753 STVector256 indexes;
754 indexes.pushBack(UInt256{2});
755 sleDir->setFieldV256(sfIndexes, indexes);
756 return sleDir;
757 };
758
759 auto const makeOfferCreateTx = [] {
760 return STTx{ttOFFER_CREATE, [](STObject& tx) {
761 Account const account{"A1"};
762 tx.setFieldAmount(sfTakerPays, XRP(1));
763 tx.setFieldAmount(sfTakerGets, account["USD"](1));
764 }};
765 };
767
768 // Creating a root book directory with mismatched exchange-rate
769 // metadata violates the invariant.
771 {{"book directory exchange rate does not match directory quality"}},
772 [&](Account const& a1, Account const&, ApplyContext& ac) {
773 auto const directoryQuality = STAmount::kURateOne;
774 auto const dir = getBookRootKey(a1, directoryQuality);
775 ac.view().insert(makeRootPage(dir, directoryQuality + 1));
776 return true;
777 },
778 XRPAmount{},
779 makeOfferCreateTx(),
780 failTers);
781
782 // A new child page must point to an existing root page.
784 {{"book directory root missing"}},
785 [&](Account const& a1, Account const&, ApplyContext& ac) {
786 auto const directoryQuality = STAmount::kURateOne;
787 auto const rootDir = getBookRootKey(a1, directoryQuality);
788 // Insert only the child page. It points at rootDir, but the
789 // corresponding root page is intentionally missing.
790 ac.view().insert(makeChildPage(rootDir));
791 return true;
792 },
793 XRPAmount{},
794 makeOfferCreateTx(),
795 failTers);
796
797 // Legacy bad-root tolerance:
798 // - The view contains a pre-existing root page with bad sfExchangeRate
799 // metadata.
800 // - The simulated transaction only creates a child page pointing to
801 // that root.
802 // - The invariant must pass because this transaction did not create
803 // the bad root, only adding a child page.
804 {
805 Env env{*this, all_};
806 Account const a1{"A1"};
807 env.fund(XRP(1000), a1);
808 env.close();
809
810 OpenView view{*env.current()};
811 auto const directoryQuality = STAmount::kURateOne;
812 auto const rootDir = getBookRootKey(a1, directoryQuality);
813 view.rawInsert(makeRootPage(rootDir, directoryQuality + 1));
814
815 ValidBookDirectory invariant;
816 invariant.visitEntry(false, nullptr, makeChildPage(rootDir));
817
819 beast::Journal const jlog{sink};
820 BEAST_EXPECT(
821 invariant.finalize(makeOfferCreateTx(), tesSUCCESS, XRPAmount{}, view, jlog));
822 }
823
824 // A bad root is rejected when added, ignored when a legacy bad root is
825 // modified without changing sfRootIndex or deleted, and checked when a
826 // modified directory changes sfRootIndex.
827 {
828 Env env{*this, all_};
829 Account const a1{"A1"};
830 env.fund(XRP(1000), a1);
831 env.close();
832
833 OpenView view{*env.current()};
834 auto const directoryQuality = STAmount::kURateOne;
835 auto const rootDir = getBookRootKey(a1, directoryQuality);
836 auto const missingRootDir = getBookRootKey(a1, directoryQuality + 1);
837 auto const badRoot = makeRootPage(rootDir, directoryQuality + 1);
838 view.rawInsert(badRoot);
839
841 beast::Journal const jlog{sink};
842
843 {
844 // add
845 ValidBookDirectory invariant;
846 invariant.visitEntry(false, nullptr, badRoot);
847
848 BEAST_EXPECT(
849 !invariant.finalize(makeOfferCreateTx(), tesSUCCESS, XRPAmount{}, view, jlog));
850 }
851 {
852 // modify (without changing the sfRootIndex)
853 ValidBookDirectory invariant;
854 invariant.visitEntry(false, badRoot, badRoot);
855
856 BEAST_EXPECT(
857 invariant.finalize(makeOfferCreateTx(), tesSUCCESS, XRPAmount{}, view, jlog));
858 }
859 {
860 // modify (changing sfRootIndex to a missing root)
861 auto const childBefore = makeChildPage(rootDir);
862 auto const childAfter = std::make_shared<SLE>(*childBefore, childBefore->key());
863 childAfter->setFieldH256(sfRootIndex, missingRootDir.key);
864
865 ValidBookDirectory invariant;
866 invariant.visitEntry(false, childBefore, childAfter);
867
869 beast::Journal const missingRootJlog{missingRootSink};
870 BEAST_EXPECT(!invariant.finalize(
871 makeOfferCreateTx(), tesSUCCESS, XRPAmount{}, view, missingRootJlog));
872 BEAST_EXPECT(
873 missingRootSink.messages().str().contains("book directory root missing"));
874 }
875 {
876 // delete
877 view.rawErase(badRoot);
878 BEAST_EXPECT(!view.exists(rootDir));
879
880 ValidBookDirectory invariant;
881 invariant.visitEntry(true, badRoot, badRoot);
882 BEAST_EXPECT(
883 invariant.finalize(makeOfferCreateTx(), tesSUCCESS, XRPAmount{}, view, jlog));
884 }
885 }
886 }
887
888 static SLE::pointer
890 ApplyContext& ac,
891 test::jtx::Account const& a1,
892 test::jtx::Account const& a2,
893 std::uint32_t numCreds = 2,
894 std::uint32_t seq = 10)
895 {
896 Keylet const pdKeylet = keylet::permissionedDomain(a1.id(), SeqProxy::rawSequence(seq));
897 auto sle = std::make_shared<SLE>(pdKeylet);
898
899 sle->setAccountID(sfOwner, a1);
900 sle->setFieldU32(sfSequence, seq);
901
902 if (numCreds != 0u)
903 {
904 // This array is sorted naturally, but if you are going to change
905 // this behavior, don't forget to use credentials::makeSorted
906 STArray credentials(sfAcceptedCredentials, numCreds);
907 for (std::size_t n = 0; n < numCreds; ++n)
908 {
909 auto cred = STObject::makeInnerObject(sfCredential);
910 cred.setAccountID(sfIssuer, a2);
911 auto const credType = "cred_type" + std::to_string(n);
912 cred.setFieldVL(sfCredentialType, Slice(credType.c_str(), credType.size()));
913 credentials.pushBack(std::move(cred));
914 }
915 sle->setFieldArray(sfAcceptedCredentials, credentials);
916 }
917
918 ac.view().insert(sle);
919 return sle;
920 }
921
924 test::jtx::Env& env,
925 test::jtx::Account const& a1,
926 test::jtx::Account const& a2,
927 std::uint32_t numCreds = 2)
928 {
929 using namespace test::jtx;
930
932
933 for (std::size_t n = 0; n < numCreds; ++n)
934 {
935 auto const credType = "cred_type" + std::to_string(n);
936 credentials.push_back({.issuer = a2, .credType = credType});
937 }
938
939 std::uint32_t const seq = env.seq(a1);
940 env(pdomain::setTx(a1, credentials));
941 UInt256 const key = pdomain::getNewDomain(env.meta());
942
943 return {seq, key};
944 }
945
946 void
956};
957
958BEAST_DEFINE_TESTSUITE(InvariantsPermissioned, app, xrpl);
959
960} // namespace xrpl::test
A generic endpoint for log messages.
Definition Journal.h:44
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
State information when applying a tx.
ApplyView & view()
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void insert(SLE::Ref sle)=0
Insert a new state SLE.
virtual void erase(SLE::Ref sle)=0
Remove a peeked SLE.
Specifies an order book.
Definition Book.h:28
RAII class to set and restore the current transaction rules.
Definition Rules.h:113
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
void rawInsert(SLE::Ref sle) override
Unconditionally insert a state item.
Definition OpenView.cpp:240
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition OpenView.cpp:157
void rawErase(SLE::Ref sle) override
Delete an existing state item.
Definition OpenView.cpp:234
static std::uint64_t const kURateOne
Definition STAmount.h:78
void pushBack(STObject const &object)
Definition STArray.h:212
std::shared_ptr< STLedgerEntry > pointer
void setFieldAmount(SField const &field, STAmount const &)
Definition STObject.cpp:803
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:79
void setFieldH256(SField const &field, UInt256 const &)
Definition STObject.cpp:767
void pushBack(UInt256 const &v)
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
An immutable linear range of bytes.
Definition Slice.h:28
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, std::shared_ptr< SLE const > const &, std::shared_ptr< SLE const > const &)
bool finalize(STTx const &, TER const, XRPAmount const, ReadView const &, beast::Journal const &)
void visitEntry(bool, SLE::ConstRef, SLE::ConstRef)
test::jtx::Env makeEnv(FeatureBitset features)
void doInvariantCheck(std::vector< std::string > const &expectLogs, Precheck const &precheck, XRPAmount fee=XRPAmount{}, STTx tx=STTx{ttACCOUNT_SET, [](STObject &) {}}, std::initializer_list< TER > ters={tecINVARIANT_FAILED, tefINVARIANT_FAILED}, Preclose const &preclose={}, TxAccount setTxAccount=TxAccount::None, std::source_location const &loc=std::source_location::current(), TER initialResult=tesSUCCESS)
Run a specific test case to put the ledger into a state that will be detected by an invariant.
static std::pair< std::uint32_t, UInt256 > createPermissionedDomainEnv(test::jtx::Env &env, test::jtx::Account const &a1, test::jtx::Account const &a2, std::uint32_t numCreds=2)
static SLE::pointer createPermissionedDomain(ApplyContext &ac, test::jtx::Account const &a1, test::jtx::Account const &a2, std::uint32_t numCreds=2, std::uint32_t seq=10)
void testPermissionedDomainInvariants(FeatureBitset features)
std::stringstream const & messages() const
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
AccountID id() const
Returns the Account ID.
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
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
std::uint32_t seq(Account const &account) const
Returns the next sequence number on account.
Definition Env.cpp:302
std::shared_ptr< STObject const > meta()
Return metadata for the last JTx.
Definition Env.cpp:538
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
T get(T... args)
T make_shared(T... args)
Keylet quality(Keylet const &k, std::uint64_t const q) noexcept
The initial directory page for a specific quality.
Definition Indexes.cpp:316
Keylet offer(AccountID const &id, SeqProxy const &seq) noexcept
An offer from an account.
Definition Indexes.cpp:310
Keylet book(Book const &b)
The beginning of an order book.
Definition Indexes.cpp:281
Keylet permissionedDomain(AccountID const &account, SeqProxy const &seq) noexcept
Definition Indexes.cpp:583
Keylet page(UInt256 const &root, std::uint64_t const index=0) noexcept
A page in a directory.
Definition Indexes.cpp:421
Check operations.
Definition check.h:18
std::vector< Credential > Credentials
UInt256 getNewDomain(std::shared_ptr< STObject const > const &meta)
json::Value setTx(AccountID const &account, Credentials const &credentials, std::optional< UInt256 > domain)
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
FeatureBitset testableAmendments()
Definition Env.h:92
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
constexpr XRPAmount
Convert XRP to drops (integral types).
Definition TxTest.h:53
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:108
@ tefINVARIANT_FAILED
Definition TER.h:177
BaseUInt< 256 > UInt256
Definition base_uint.h:581
constexpr std::size_t kMaxPermissionedDomainCredentialsArraySize
The maximum number of credentials can be passed in array for permissioned domain.
Definition Protocol.h:287
@ tecINVARIANT_FAILED
Definition TER.h:324
@ tesSUCCESS
Definition TER.h:253
T str(T... args)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
UInt256 key
Definition Keylet.h:21
T to_string(T... args)