xrpld
Loading...
Searching...
No Matches
PermissionedDEX_test.cpp
1#include <test/jtx/AMM.h>
2#include <test/jtx/AMMTest.h>
3#include <test/jtx/Account.h>
4#include <test/jtx/Env.h>
5#include <test/jtx/TestHelpers.h>
6#include <test/jtx/amount.h>
7#include <test/jtx/balance.h>
8#include <test/jtx/credentials.h>
9#include <test/jtx/domain.h>
10#include <test/jtx/fee.h>
11#include <test/jtx/jtx_json.h>
12#include <test/jtx/ledgerStateFix.h>
13#include <test/jtx/offer.h>
14#include <test/jtx/owners.h> // IWYU pragma: keep
15#include <test/jtx/paths.h>
16#include <test/jtx/pay.h>
17#include <test/jtx/permissioned_dex.h>
18#include <test/jtx/permissioned_domains.h>
19#include <test/jtx/sendmax.h>
20#include <test/jtx/ter.h>
21#include <test/jtx/trust.h>
22#include <test/jtx/txflags.h>
23
24#include <xrpl/basics/Slice.h>
25#include <xrpl/basics/base_uint.h>
26#include <xrpl/beast/unit_test/suite.h>
27#include <xrpl/beast/utility/Journal.h>
28#include <xrpl/ledger/OpenView.h>
29#include <xrpl/protocol/Book.h>
30#include <xrpl/protocol/Feature.h>
31#include <xrpl/protocol/Indexes.h>
32#include <xrpl/protocol/Issue.h>
33#include <xrpl/protocol/Keylet.h>
34#include <xrpl/protocol/LedgerFormats.h>
35#include <xrpl/protocol/SField.h>
36#include <xrpl/protocol/STAmount.h>
37#include <xrpl/protocol/STArray.h>
38#include <xrpl/protocol/STLedgerEntry.h>
39#include <xrpl/protocol/SeqProxy.h>
40#include <xrpl/protocol/TER.h>
41#include <xrpl/protocol/TxFlags.h>
42#include <xrpl/protocol/jss.h>
43
44#include <algorithm>
45#include <chrono>
46#include <cstddef>
47#include <cstdint>
48#include <map>
49#include <memory>
50#include <optional>
51#include <string>
52#include <utility>
53#include <vector>
54
55namespace xrpl::test {
56
57using namespace jtx;
58
60{
61 [[nodiscard]] static bool
62 offerExists(Env const& env, Account const& account, std::uint32_t offerSeq)
63 {
64 return static_cast<bool>(
65 env.le(keylet::offer(account.id(), SeqProxy::rawSequence(offerSeq))));
66 }
67
68 [[nodiscard]] static bool
70 Env const& env,
71 Account const& account,
72 std::uint32_t offerSeq,
73 STAmount const& takerPays,
74 STAmount const& takerGets,
75 uint32_t const flags = 0,
76 bool const domainOffer = false)
77 {
78 auto const offerInDir = [&](UInt256 const& directory,
79 uint64_t const pageIndex,
80 std::optional<UInt256> domain = std::nullopt) -> bool {
81 auto const page = env.le(keylet::page(directory, pageIndex));
82 if (!page)
83 return false;
84
85 if (domain != (*page)[~sfDomainID])
86 return false;
87
88 auto const& indexes = page->getFieldV256(sfIndexes);
89 return std::ranges::any_of(indexes, [&](auto const& index) {
90 return index == keylet::offer(account, SeqProxy::rawSequence(offerSeq)).key;
91 });
92 };
93
94 auto const sle = env.le(keylet::offer(account.id(), SeqProxy::rawSequence(offerSeq)));
95 if (!sle)
96 return false;
97 if (sle->getFieldAmount(sfTakerGets) != takerGets)
98 return false;
99 if (sle->getFieldAmount(sfTakerPays) != takerPays)
100 return false;
101 if (sle->getFlags() != flags)
102 return false;
103 if (domainOffer && !sle->isFieldPresent(sfDomainID))
104 return false;
105 if (!domainOffer && sle->isFieldPresent(sfDomainID))
106 return false;
107 if (!offerInDir(
108 sle->getFieldH256(sfBookDirectory),
109 sle->getFieldU64(sfBookNode),
110 (*sle)[~sfDomainID]))
111 return false;
112
113 if (sle->isFlag(lsfHybrid))
114 {
115 if (!sle->isFieldPresent(sfDomainID))
116 return false;
117 if (!sle->isFieldPresent(sfAdditionalBooks))
118 return false;
119 if (sle->getFieldArray(sfAdditionalBooks).size() != 1)
120 return false;
121
122 auto const& additionalBookDirs = sle->getFieldArray(sfAdditionalBooks);
123
124 for (auto const& bookDir : additionalBookDirs)
125 {
126 auto const& dirIndex = bookDir.getFieldH256(sfBookDirectory);
127 auto const& dirNode = bookDir.getFieldU64(sfBookNode);
128
129 // the directory is for the open order book, so the dir
130 // doesn't have domainID
131 if (!offerInDir(dirIndex, dirNode, std::nullopt))
132 return false;
133 }
134 }
135 else
136 {
137 if (sle->isFieldPresent(sfAdditionalBooks))
138 return false;
139 }
140
141 return true;
142 }
143
144 static UInt256
145 getBookDirKey(Book const& book, STAmount const& takerPays, STAmount const& takerGets)
146 {
147 return keylet::quality(keylet::book(book), getRate(takerGets, takerPays)).key;
148 }
149
151 getDefaultOfferDirKey(Env const& env, Account const& account, std::uint32_t offerSeq)
152 {
153 if (auto const sle = env.le(keylet::offer(account.id(), SeqProxy::rawSequence(offerSeq))))
154 return Keylet(ltDIR_NODE, (*sle)[sfBookDirectory]).key;
155
156 return {};
157 }
158
159 [[nodiscard]] static bool
161 {
162 std::optional<std::uint64_t> pageIndex{0};
163 std::uint32_t dirCnt = 0;
164
165 do
166 {
167 auto const page = env.le(
168 keylet::page(directory, *pageIndex)); // NOLINT(bugprone-unchecked-optional-access)
169 if (!page)
170 break;
171
172 pageIndex = (*page)[~sfIndexNext];
173 dirCnt += (*page)[sfIndexes].size();
174
175 } while (pageIndex.value_or(0) != 0u);
176
177 return dirCnt == dirSize;
178 }
179
180 void
182 {
183 bool const fixEnabled = features[fixCleanup3_4_0];
184
185 testcase << "OfferCreate"
186 << (fixEnabled ? " (Cleanup3_4_0 enabled)" : " (Cleanup3_4_0 disabled)");
187
188 // test preflight
189 {
190 Env env(*this, features - featurePermissionedDEX);
191 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
192 PermissionedDEX(env);
193
194 env(offer(bob, XRP(10), usd(10)), Domain(domainID), Ter(temDISABLED));
195 env.close();
196
197 env.enableFeature(featurePermissionedDEX);
198 env.close();
199 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
200 env.close();
201 }
202
203 // test preflight - malformed DomainID being zero
204 // Only test this with fixCleanup3_2_0 enabled. Without the fix,
205 // an assert-enabled build can crash when Ledger::read() receives
206 // a zero-key PermissionedDomain keylet.
207 if (features[fixCleanup3_2_0])
208 {
209 Env env(*this, features);
210 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
211 PermissionedDEX(env);
212
213 env(offer(bob, XRP(10), usd(10)), Domain(UInt256{}), Ter(temMALFORMED));
214 env.close();
215 }
216
217 // preclaim - someone outside of the domain cannot create domain offer
218 {
219 Env env(*this, features);
220 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
221 PermissionedDEX(env);
222
223 // create devin account who is not part of the domain
224 Account const devin("devin");
225 env.fund(XRP(1000), devin);
226 env.close();
227 env.trust(usd(1000), devin);
228 env.close();
229 env(pay(gw, devin, usd(100)));
230 env.close();
231
232 env(offer(devin, XRP(10), usd(10)), Domain(domainID), Ter(tecNO_PERMISSION));
233 env.close();
234
235 // domain owner also issues a credential for devin
236 env(credentials::create(devin, domainOwner, credType));
237 env.close();
238
239 // devin still cannot create offer since he didn't accept credential
240 env(offer(devin, XRP(10), usd(10)), Domain(domainID), Ter(tecNO_PERMISSION));
241 env.close();
242
243 env(credentials::accept(devin, domainOwner, credType));
244 env.close();
245
246 env(offer(devin, XRP(10), usd(10)), Domain(domainID));
247 env.close();
248 }
249
250 // preclaim - someone with expired cred cannot create domain offer
251 {
252 Env env(*this, features);
253 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
254 PermissionedDEX(env);
255
256 // create devin account who is not part of the domain
257 Account const devin("devin");
258 env.fund(XRP(1000), devin);
259 env.close();
260 env.trust(usd(1000), devin);
261 env.close();
262 env(pay(gw, devin, usd(100)));
263 env.close();
264
265 auto jv = credentials::create(devin, domainOwner, credType);
266 uint32_t const t = env.current()->header().parentCloseTime.time_since_epoch().count();
267 jv[sfExpiration.jsonName] = t + 20;
268 env(jv);
269
270 env(credentials::accept(devin, domainOwner, credType));
271 env.close();
272
273 // devin can still create offer while his cred is not expired
274 env(offer(devin, XRP(10), usd(10)), Domain(domainID));
275 env.close();
276
277 // time advance
279
280 // Devin cannot create offer with expired cred. After fixCleanup3_4_0,
281 // doApply deletes the expired credential SLE and returns tecEXPIRED.
282 TER const expectedExpiredCredTer = fixEnabled ? tecEXPIRED : tecNO_PERMISSION;
283 env(offer(devin, XRP(10), usd(10)), Domain(domainID), Ter(expectedExpiredCredTer));
284 env.close();
285 }
286
287 // preclaim - cannot create an offer in a non existent domain
288 {
289 Env env(*this, features);
290 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
291 PermissionedDEX(env);
292 UInt256 const badDomain{
293 "F10D0CC9A0F9A3CBF585B80BE09A186483668FDBDD39AA7E3370F3649CE134"
294 "E5"};
295
296 env(offer(bob, XRP(10), usd(10)), Domain(badDomain), Ter(tecNO_PERMISSION));
297 env.close();
298 }
299
300 // apply - offer can be created even if takergets issuer is not in
301 // domain
302 {
303 Env env(*this, features);
304 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
305 PermissionedDEX(env);
306
307 env(credentials::deleteCred(domainOwner, gw, domainOwner, credType));
308 env.close();
309
310 auto const bobOfferSeq{env.seq(bob)};
311 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
312 env.close();
313
314 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), 0, true));
315 }
316
317 // apply - offer can be created even if takerpays issuer is not in
318 // domain
319 {
320 Env env(*this, features);
321 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
322 PermissionedDEX(env);
323
324 env(credentials::deleteCred(domainOwner, gw, domainOwner, credType));
325 env.close();
326
327 auto const bobOfferSeq{env.seq(bob)};
328 env(offer(bob, usd(10), XRP(10)), Domain(domainID));
329 env.close();
330
331 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, usd(10), XRP(10), 0, true));
332 }
333
334 // apply - two domain offers cross with each other
335 {
336 Env env(*this, features);
337 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
338 PermissionedDEX(env);
339
340 auto const bobOfferSeq{env.seq(bob)};
341 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
342 env.close();
343
344 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), 0, true));
345 BEAST_EXPECT(ownerCount(env, bob) == 3);
346
347 // a non domain offer cannot cross with domain offer
348 env(offer(carol, usd(10), XRP(10)));
349 env.close();
350
351 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), 0, true));
352
353 auto const aliceOfferSeq{env.seq(alice)};
354 env(offer(alice, usd(10), XRP(10)), Domain(domainID));
355 env.close();
356
357 BEAST_EXPECT(!offerExists(env, alice, aliceOfferSeq));
358 BEAST_EXPECT(!offerExists(env, bob, bobOfferSeq));
359 BEAST_EXPECT(ownerCount(env, alice) == 2);
360 }
361
362 // apply - create lots of domain offers
363 {
364 Env env(*this, features);
365 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
366 PermissionedDEX(env);
367
369 offerSeqs.reserve(100);
370
371 for (size_t i = 0; i <= 100; i++)
372 {
373 auto const bobOfferSeq{env.seq(bob)};
374 offerSeqs.emplace_back(bobOfferSeq);
375
376 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
377 env.close();
378 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), 0, true));
379 }
380
381 for (auto const offerSeq : offerSeqs)
382 {
383 env(offerCancel(bob, offerSeq));
384 env.close();
385 BEAST_EXPECT(!offerExists(env, bob, offerSeq));
386 }
387 }
388 }
389
390 void
392 {
393 testcase("Payment");
394
395 // test preflight - without enabling featurePermissionedDEX amendment
396 {
397 Env env(*this, features - featurePermissionedDEX);
398 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
399 PermissionedDEX(env);
400
401 env(pay(bob, alice, usd(10)),
402 Path(~usd),
403 Sendmax(XRP(10)),
404 Domain(domainID),
406 env.close();
407
408 env.enableFeature(featurePermissionedDEX);
409 env.close();
410
411 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
412 env.close();
413
414 env(pay(bob, alice, usd(10)), Path(~usd), Sendmax(XRP(10)), Domain(domainID));
415 env.close();
416 }
417
418 // test preflight - malformed DomainID being zero
419 // Only test this with fixCleanup3_2_0 enabled. Without the fix,
420 // an assert-enabled build can crash when Ledger::read() receives
421 // a zero-key PermissionedDomain keylet.
422 if (features[fixCleanup3_2_0])
423 {
424 Env env(*this, features);
425 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
426 PermissionedDEX(env);
427
428 env(pay(bob, alice, usd(10)),
429 Path(~usd),
430 Sendmax(XRP(10)),
431 Domain(UInt256{}),
433 env.close();
434 }
435
436 // preclaim - cannot send payment with non existent domain
437 {
438 Env env(*this, features);
439 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
440 PermissionedDEX(env);
441 UInt256 const badDomain{
442 "F10D0CC9A0F9A3CBF585B80BE09A186483668FDBDD39AA7E3370F3649CE134"
443 "E5"};
444
445 env(pay(bob, alice, usd(10)),
446 Path(~usd),
447 Sendmax(XRP(10)),
448 Domain(badDomain),
450 env.close();
451 }
452
453 // preclaim - payment with non-domain destination fails
454 {
455 Env env(*this, features);
456 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
457 PermissionedDEX(env);
458
459 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
460 env.close();
461
462 // create devin account who is not part of the domain
463 Account const devin("devin");
464 env.fund(XRP(1000), devin);
465 env.close();
466 env.trust(usd(1000), devin);
467 env.close();
468 env(pay(gw, devin, usd(100)));
469 env.close();
470
471 // devin is not part of domain
472 env(pay(alice, devin, usd(10)),
473 Path(~usd),
474 Sendmax(XRP(10)),
475 Domain(domainID),
477 env.close();
478
479 // domain owner also issues a credential for devin
480 env(credentials::create(devin, domainOwner, credType));
481 env.close();
482
483 // devin has not yet accepted cred
484 env(pay(alice, devin, usd(10)),
485 Path(~usd),
486 Sendmax(XRP(10)),
487 Domain(domainID),
489 env.close();
490
491 env(credentials::accept(devin, domainOwner, credType));
492 env.close();
493
494 // devin can now receive payment after he is in domain
495 env(pay(alice, devin, usd(10)), Path(~usd), Sendmax(XRP(10)), Domain(domainID));
496 env.close();
497 }
498
499 // preclaim - non-domain sender cannot send payment
500 {
501 Env env(*this, features);
502 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
503 PermissionedDEX(env);
504
505 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
506 env.close();
507
508 // create devin account who is not part of the domain
509 Account const devin("devin");
510 env.fund(XRP(1000), devin);
511 env.close();
512 env.trust(usd(1000), devin);
513 env.close();
514 env(pay(gw, devin, usd(100)));
515 env.close();
516
517 // devin tries to send domain payment
518 env(pay(devin, alice, usd(10)),
519 Path(~usd),
520 Sendmax(XRP(10)),
521 Domain(domainID),
523 env.close();
524
525 // domain owner also issues a credential for devin
526 env(credentials::create(devin, domainOwner, credType));
527 env.close();
528
529 // devin has not yet accepted cred
530 env(pay(devin, alice, usd(10)),
531 Path(~usd),
532 Sendmax(XRP(10)),
533 Domain(domainID),
535 env.close();
536
537 env(credentials::accept(devin, domainOwner, credType));
538 env.close();
539
540 // devin can now send payment after he is in domain
541 env(pay(devin, alice, usd(10)), Path(~usd), Sendmax(XRP(10)), Domain(domainID));
542 env.close();
543 }
544
545 // apply - domain owner can always send and receive domain payment
546 {
547 Env env(*this, features);
548 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
549 PermissionedDEX(env);
550
551 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
552 env.close();
553
554 // domain owner can always be destination
555 env(pay(alice, domainOwner, usd(10)), Path(~usd), Sendmax(XRP(10)), Domain(domainID));
556 env.close();
557
558 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
559 env.close();
560
561 // domain owner can send
562 env(pay(domainOwner, alice, usd(10)), Path(~usd), Sendmax(XRP(10)), Domain(domainID));
563 env.close();
564 }
565 }
566
567 void
569 {
570 testcase("Book step");
571
572 // test domain cross currency payment consuming one offer
573 {
574 Env env(*this, features);
575 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
576 PermissionedDEX(env);
577
578 // create a regular offer without domain
579 auto const regularOfferSeq{env.seq(bob)};
580 env(offer(bob, XRP(10), usd(10)));
581 env.close();
582 BEAST_EXPECT(checkOffer(env, bob, regularOfferSeq, XRP(10), usd(10)));
583
584 auto const regularDirKey = getDefaultOfferDirKey(env, bob, regularOfferSeq);
585 BEAST_EXPECT(regularDirKey);
586 BEAST_EXPECT(checkDirectorySize(
587 env, *regularDirKey, 1)); // NOLINT(bugprone-unchecked-optional-access)
588
589 // a domain payment cannot consume regular offers
590 env(pay(alice, carol, usd(10)),
591 Path(~usd),
592 Sendmax(XRP(10)),
593 Domain(domainID),
595 env.close();
596
597 // create a domain offer
598 auto const domainOfferSeq{env.seq(bob)};
599 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
600 env.close();
601
602 BEAST_EXPECT(checkOffer(env, bob, domainOfferSeq, XRP(10), usd(10), 0, true));
603
604 auto const domainDirKey = getDefaultOfferDirKey(env, bob, domainOfferSeq);
605 BEAST_EXPECT(domainDirKey);
606 BEAST_EXPECT(checkDirectorySize(
607 env, *domainDirKey, 1)); // NOLINT(bugprone-unchecked-optional-access)
608
609 // cross-currency permissioned payment consumed
610 // domain offer instead of regular offer
611 env(pay(alice, carol, usd(10)), Path(~usd), Sendmax(XRP(10)), Domain(domainID));
612 env.close();
613 BEAST_EXPECT(!offerExists(env, bob, domainOfferSeq));
614 BEAST_EXPECT(checkOffer(env, bob, regularOfferSeq, XRP(10), usd(10)));
615
616 // domain directory is empty
617 BEAST_EXPECT(checkDirectorySize(
618 env, *domainDirKey, 0)); // NOLINT(bugprone-unchecked-optional-access)
619 BEAST_EXPECT(checkDirectorySize(
620 env, *regularDirKey, 1)); // NOLINT(bugprone-unchecked-optional-access)
621 }
622
623 // test domain payment consuming two offers in the path
624 {
625 Env env(*this, features);
626 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
627 PermissionedDEX(env);
628
629 auto const eur = gw["EUR"];
630 env.trust(eur(1000), alice);
631 env.close();
632 env.trust(eur(1000), bob);
633 env.close();
634 env.trust(eur(1000), carol);
635 env.close();
636 env(pay(gw, bob, eur(100)));
637 env.close();
638
639 // create XRP/USD domain offer
640 auto const usdOfferSeq{env.seq(bob)};
641 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
642 env.close();
643
644 BEAST_EXPECT(checkOffer(env, bob, usdOfferSeq, XRP(10), usd(10), 0, true));
645
646 // payment fail because there isn't eur offer
647 env(pay(alice, carol, eur(10)),
648 Path(~usd, ~eur),
649 Sendmax(XRP(10)),
650 Domain(domainID),
652 env.close();
653 BEAST_EXPECT(checkOffer(env, bob, usdOfferSeq, XRP(10), usd(10), 0, true));
654
655 // bob creates a regular USD/EUR offer
656 auto const regularOfferSeq{env.seq(bob)};
657 env(offer(bob, usd(10), eur(10)));
658 env.close();
659 BEAST_EXPECT(checkOffer(env, bob, regularOfferSeq, usd(10), eur(10)));
660
661 // alice tries to pay again, but still fails because the regular
662 // offer cannot be consumed
663 env(pay(alice, carol, eur(10)),
664 Path(~usd, ~eur),
665 Sendmax(XRP(10)),
666 Domain(domainID),
668 env.close();
669
670 // bob creates a domain USD/EUR offer
671 auto const eurOfferSeq{env.seq(bob)};
672 env(offer(bob, usd(10), eur(10)), Domain(domainID));
673 env.close();
674 BEAST_EXPECT(checkOffer(env, bob, eurOfferSeq, usd(10), eur(10), 0, true));
675
676 // alice successfully consume two domain offers: xrp/usd and usd/eur
677 env(pay(alice, carol, eur(5)), Sendmax(XRP(5)), Domain(domainID), Path(~usd, ~eur));
678 env.close();
679
680 BEAST_EXPECT(checkOffer(env, bob, usdOfferSeq, XRP(5), usd(5), 0, true));
681 BEAST_EXPECT(checkOffer(env, bob, eurOfferSeq, usd(5), eur(5), 0, true));
682
683 // alice successfully consume two domain offers and deletes them
684 // we compute path this time using `paths`
685 env(pay(alice, carol, eur(5)), Sendmax(XRP(5)), Domain(domainID), Paths(XRP));
686 env.close();
687
688 BEAST_EXPECT(!offerExists(env, bob, usdOfferSeq));
689 BEAST_EXPECT(!offerExists(env, bob, eurOfferSeq));
690
691 // regular offer is not consumed
692 BEAST_EXPECT(checkOffer(env, bob, regularOfferSeq, usd(10), eur(10)));
693 }
694
695 // domain payment cannot consume offer from another domain
696 {
697 Env env(*this, features);
698 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
699 PermissionedDEX(env);
700
701 // Fund devin and create USD trustline
702 Account const badDomainOwner("badDomainOwner");
703 Account const devin("devin");
704 env.fund(XRP(1000), badDomainOwner, devin);
705 env.close();
706 env.trust(usd(1000), devin);
707 env.close();
708 env(pay(gw, devin, usd(100)));
709 env.close();
710
711 auto const badCredType = "badCred";
713 {.issuer = badDomainOwner, .credType = badCredType},
714 };
715 env(pdomain::setTx(badDomainOwner, credentials));
716
717 auto objects = pdomain::getObjects(badDomainOwner, env);
718 auto const badDomainID = objects.begin()->first;
719
720 env(credentials::create(devin, badDomainOwner, badCredType));
721 env.close();
722 env(credentials::accept(devin, badDomainOwner, badCredType));
723
724 // devin creates a domain offer in another domain
725 env(offer(devin, XRP(10), usd(10)), Domain(badDomainID));
726 env.close();
727
728 // domain payment can't consume an offer from another domain
729 env(pay(alice, carol, usd(10)),
730 Path(~usd),
731 Sendmax(XRP(10)),
732 Domain(domainID),
734 env.close();
735
736 // bob creates an offer under the right domain
737 auto const bobOfferSeq{env.seq(bob)};
738 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
739 env.close();
740 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), 0, true));
741
742 // domain payment now consumes from the right domain
743 env(pay(alice, carol, usd(10)), Path(~usd), Sendmax(XRP(10)), Domain(domainID));
744 env.close();
745
746 BEAST_EXPECT(!offerExists(env, bob, bobOfferSeq));
747 }
748
749 // sanity check: devin, who is part of the domain but doesn't have a
750 // trustline with USD issuer, can successfully make a payment using
751 // offer
752 {
753 Env env(*this, features);
754 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
755 PermissionedDEX(env);
756
757 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
758 env.close();
759
760 // fund devin but don't create a USD trustline with gateway
761 Account const devin("devin");
762 env.fund(XRP(1000), devin);
763 env.close();
764
765 // domain owner also issues a credential for devin
766 env(credentials::create(devin, domainOwner, credType));
767 env.close();
768
769 env(credentials::accept(devin, domainOwner, credType));
770 env.close();
771
772 // successful payment because offer is consumed
773 env(pay(devin, alice, usd(10)), Sendmax(XRP(10)), Domain(domainID));
774 env.close();
775 }
776
777 // offer becomes unfunded when offer owner's cred expires
778 {
779 Env env(*this, features);
780 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
781 PermissionedDEX(env);
782
783 // create devin account who is not part of the domain
784 Account const devin("devin");
785 env.fund(XRP(1000), devin);
786 env.close();
787 env.trust(usd(1000), devin);
788 env.close();
789 env(pay(gw, devin, usd(100)));
790 env.close();
791
792 auto jv = credentials::create(devin, domainOwner, credType);
793 uint32_t const t = env.current()->header().parentCloseTime.time_since_epoch().count();
794 jv[sfExpiration.jsonName] = t + 20;
795 env(jv);
796
797 env(credentials::accept(devin, domainOwner, credType));
798 env.close();
799
800 // devin can still create offer while his cred is not expired
801 auto const offerSeq{env.seq(devin)};
802 env(offer(devin, XRP(10), usd(10)), Domain(domainID));
803 env.close();
804
805 // devin's offer can still be consumed while his cred isn't expired
806 env(pay(alice, carol, usd(5)), Path(~usd), Sendmax(XRP(5)), Domain(domainID));
807 env.close();
808 BEAST_EXPECT(checkOffer(env, devin, offerSeq, XRP(5), usd(5), 0, true));
809
810 // advance time
812
813 // devin's offer is unfunded now due to expired cred
814 env(pay(alice, carol, usd(5)),
815 Path(~usd),
816 Sendmax(XRP(5)),
817 Domain(domainID),
819 env.close();
820 BEAST_EXPECT(checkOffer(env, devin, offerSeq, XRP(5), usd(5), 0, true));
821 }
822
823 // offer becomes unfunded when offer owner's cred is removed
824 {
825 Env env(*this, features);
826 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
827 PermissionedDEX(env);
828
829 auto const offerSeq{env.seq(bob)};
830 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
831 env.close();
832
833 // bob's offer can still be consumed while his cred exists
834 env(pay(alice, carol, usd(5)), Path(~usd), Sendmax(XRP(5)), Domain(domainID));
835 env.close();
836 BEAST_EXPECT(checkOffer(env, bob, offerSeq, XRP(5), usd(5), 0, true));
837
838 // remove bob's cred
839 env(credentials::deleteCred(domainOwner, bob, domainOwner, credType));
840 env.close();
841
842 // bob's offer is unfunded now due to expired cred
843 env(pay(alice, carol, usd(5)),
844 Path(~usd),
845 Sendmax(XRP(5)),
846 Domain(domainID),
848 env.close();
849 BEAST_EXPECT(checkOffer(env, bob, offerSeq, XRP(5), usd(5), 0, true));
850 }
851 }
852
853 void
855 {
856 testcase("Rippling");
857
858 // test a non-domain account can still be part of rippling in a domain
859 // payment. If the domain wishes to control who is allowed to ripple
860 // through, they should set the rippling individually
861 Env env(*this, features);
862 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
863 PermissionedDEX(env);
864
865 auto const eura = alice["EUR"];
866 auto const eurb = bob["EUR"];
867
868 env.trust(eura(100), bob);
869 env.trust(eurb(100), carol);
870 env.close();
871
872 // remove bob from domain
873 env(credentials::deleteCred(domainOwner, bob, domainOwner, credType));
874 env.close();
875
876 // alice can still ripple through bob even though he's not part
877 // of the domain, this is intentional
878 env(pay(alice, carol, eurb(10)), Paths(eura), Domain(domainID));
879 env.close();
880 env.require(Balance(bob, eura(10)), Balance(carol, eurb(10)));
881
882 // carol sets no ripple on bob
883 env(trust(carol, bob["EUR"](0), bob, tfSetNoRipple));
884 env.close();
885
886 // payment no longer works because carol has no ripple on bob
887 env(pay(alice, carol, eurb(5)), Paths(eura), Domain(domainID), Ter(tecPATH_DRY));
888 env.close();
889 env.require(Balance(bob, eura(10)), Balance(carol, eurb(10)));
890 }
891
892 void
894 {
895 testcase("Offer token issuer in domain");
896
897 // whether the issuer is in the domain should NOT affect whether an
898 // offer can be consumed in domain payment
899 Env env(*this, features);
900 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
901 PermissionedDEX(env);
902
903 // create an xrp/usd offer with usd as takergets
904 auto const bobOffer1Seq{env.seq(bob)};
905 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
906 env.close();
907
908 // create an usd/xrp offer with usd as takerpays
909 auto const bobOffer2Seq{env.seq(bob)};
910 env(offer(bob, usd(10), XRP(10)), Domain(domainID), Txflags(tfPassive));
911 env.close();
912
913 BEAST_EXPECT(checkOffer(env, bob, bobOffer1Seq, XRP(10), usd(10), 0, true));
914 BEAST_EXPECT(checkOffer(env, bob, bobOffer2Seq, usd(10), XRP(10), lsfPassive, true));
915
916 // remove gateway from domain
917 env(credentials::deleteCred(domainOwner, gw, domainOwner, credType));
918 env.close();
919
920 // payment succeeds even if issuer is not in domain
921 // xrp/usd offer is consumed
922 env(pay(alice, carol, usd(10)), Path(~usd), Sendmax(XRP(10)), Domain(domainID));
923 env.close();
924 BEAST_EXPECT(!offerExists(env, bob, bobOffer1Seq));
925
926 // payment succeeds even if issuer is not in domain
927 // usd/xrp offer is consumed
928 env(pay(alice, carol, XRP(10)), Path(~XRP), Sendmax(usd(10)), Domain(domainID));
929 env.close();
930 BEAST_EXPECT(!offerExists(env, bob, bobOffer2Seq));
931 }
932
933 void
935 {
936 testcase("Remove unfunded offer");
937
938 // checking that an unfunded offer will be implicitly removed by a
939 // successful payment tx
940 Env env(*this, features);
941 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
942 PermissionedDEX(env);
943
944 auto const aliceOfferSeq{env.seq(alice)};
945 env(offer(alice, XRP(100), usd(100)), Domain(domainID));
946 env.close();
947
948 auto const bobOfferSeq{env.seq(bob)};
949 env(offer(bob, XRP(20), usd(20)), Domain(domainID));
950 env.close();
951
952 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(20), usd(20), 0, true));
953 BEAST_EXPECT(checkOffer(env, alice, aliceOfferSeq, XRP(100), usd(100), 0, true));
954
955 auto const domainDirKey = getDefaultOfferDirKey(env, bob, bobOfferSeq);
956 BEAST_EXPECT(domainDirKey);
957 BEAST_EXPECT(checkDirectorySize(
958 env, *domainDirKey, 2)); // NOLINT(bugprone-unchecked-optional-access)
959
960 // remove alice from domain and thus alice's offer becomes unfunded
961 env(credentials::deleteCred(domainOwner, alice, domainOwner, credType));
962 env.close();
963
964 env(pay(gw, carol, usd(10)), Path(~usd), Sendmax(XRP(10)), Domain(domainID));
965 env.close();
966
967 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), 0, true));
968
969 // alice's unfunded offer is removed implicitly
970 BEAST_EXPECT(!offerExists(env, alice, aliceOfferSeq));
971 BEAST_EXPECT(checkDirectorySize(
972 env, *domainDirKey, 1)); // NOLINT(bugprone-unchecked-optional-access)
973 }
974
975 void
977 {
978 testcase("AMM not used");
979
980 Env env(*this, features);
981 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
982 PermissionedDEX(env);
983 AMM const amm(env, alice, XRP(10), usd(50));
984
985 // a domain payment isn't able to consume AMM
986 env(pay(bob, carol, usd(5)),
987 Path(~usd),
988 Sendmax(XRP(5)),
989 Domain(domainID),
991 env.close();
992
993 // a non domain payment can use AMM
994 env(pay(bob, carol, usd(5)), Path(~usd), Sendmax(XRP(5)));
995 env.close();
996
997 // USD amount in AMM is changed
998 auto [xrp, usdBalance, lpt] = amm.balances(XRP, usd);
999 BEAST_EXPECT(usdBalance == usd(45));
1000 }
1001
1002 void
1004 {
1005 bool const excludesAmmFromDomainQuality = features[fixCleanup3_3_0];
1006
1007 testcase << "AMM quality not leaked into domain BookStep"
1008 << (excludesAmmFromDomainQuality ? " (Cleanup3_3_0 enabled)"
1009 : " (Cleanup3_3_0 disabled)");
1010
1011 Env env(*this, features);
1012 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1013 PermissionedDEX(env);
1014 auto const eur = gw["EUR"];
1015
1016 env.trust(eur(1000), bob, domainOwner);
1017 env.close();
1018 env(pay(gw, bob, eur(100)));
1019 env.close();
1020
1021 env(pay(gw, alice, usd(500)));
1022 env.close();
1023
1024 // The AMM makes the direct XRP->USD book look much better than it
1025 // really is for domain payments. The domain LOB direct path is 1:1,
1026 // while the competing XRP->EUR->USD path is 2:1.
1027 AMM const amm(env, alice, XRP(10), usd(500));
1028
1029 auto const directOfferSeq{env.seq(bob)};
1030 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
1031 env.close();
1032
1033 auto const xrpEurOfferSeq{env.seq(bob)};
1034 env(offer(bob, XRP(10), eur(20)), Domain(domainID));
1035 env.close();
1036
1037 auto const eurUsdOfferSeq{env.seq(domainOwner)};
1038 env(offer(domainOwner, eur(20), usd(20)), Domain(domainID));
1039 env.close();
1040
1041 auto const carolBalBefore = env.balance(carol, usd);
1042
1043 // Both paths compete for the same XRP(10) sendmax. If AMM quality leaks
1044 // into the direct domain book, the engine ranks direct XRP->USD first
1045 // but crossing can only consume the 1:1 LOB offer. With the fix, the
1046 // direct book is ranked by its domain LOB quality, so the 2:1
1047 // XRP->EUR->USD path executes first.
1048 env(pay(alice, carol, usd(100)),
1049 Path(~usd),
1050 Path(~eur, ~usd),
1051 Sendmax(XRP(10)),
1052 Txflags(tfPartialPayment | tfNoRippleDirect),
1053 Domain(domainID));
1054 env.close();
1055
1056 auto const delivered = env.balance(carol, usd) - carolBalBefore;
1057 if (excludesAmmFromDomainQuality)
1058 {
1059 BEAST_EXPECT(delivered == usd(20));
1060
1061 BEAST_EXPECT(checkOffer(env, bob, directOfferSeq, XRP(10), usd(10), 0, true));
1062 BEAST_EXPECT(!offerExists(env, bob, xrpEurOfferSeq));
1063 BEAST_EXPECT(!offerExists(env, domainOwner, eurUsdOfferSeq));
1064 }
1065 else
1066 {
1067 BEAST_EXPECT(delivered == usd(10));
1068
1069 BEAST_EXPECT(!offerExists(env, bob, directOfferSeq));
1070 BEAST_EXPECT(checkOffer(env, bob, xrpEurOfferSeq, XRP(10), eur(20), 0, true));
1071 BEAST_EXPECT(checkOffer(env, domainOwner, eurUsdOfferSeq, eur(20), usd(20), 0, true));
1072 }
1073
1074 auto [xrp, usdBalance, lpt] = amm.balances(XRP, usd);
1075 BEAST_EXPECT(xrp == XRP(10));
1076 BEAST_EXPECT(usdBalance == usd(500));
1077 }
1078
1079 void
1081 {
1082 testcase("Hybrid offer create");
1083
1084 // test preflight - invalid hybrid flag
1085 {
1086 Env env(*this, features - featurePermissionedDEX);
1087 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1088 PermissionedDEX(env);
1089
1090 env(offer(bob, XRP(10), usd(10)),
1091 Domain(domainID),
1092 Txflags(tfHybrid),
1093 Ter(temDISABLED));
1094 env.close();
1095
1096 env(offer(bob, XRP(10), usd(10)), Txflags(tfHybrid), Ter(temINVALID_FLAG));
1097 env.close();
1098
1099 env.enableFeature(featurePermissionedDEX);
1100 env.close();
1101
1102 // hybrid offer must have domainID
1103 env(offer(bob, XRP(10), usd(10)), Txflags(tfHybrid), Ter(temINVALID_FLAG));
1104 env.close();
1105
1106 // hybrid offer must have domainID
1107 auto const offerSeq{env.seq(bob)};
1108 env(offer(bob, XRP(10), usd(10)), Txflags(tfHybrid), Domain(domainID));
1109 env.close();
1110 BEAST_EXPECT(checkOffer(env, bob, offerSeq, XRP(10), usd(10), lsfHybrid, true));
1111 }
1112
1113 // apply - domain offer can cross with hybrid
1114 {
1115 Env env(*this, features);
1116 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1117 PermissionedDEX(env);
1118
1119 auto const bobOfferSeq{env.seq(bob)};
1120 env(offer(bob, XRP(10), usd(10)), Txflags(tfHybrid), Domain(domainID));
1121 env.close();
1122
1123 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), lsfHybrid, true));
1124 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
1125 BEAST_EXPECT(ownerCount(env, bob) == 3);
1126
1127 auto const aliceOfferSeq{env.seq(alice)};
1128 env(offer(alice, usd(10), XRP(10)), Domain(domainID));
1129 env.close();
1130
1131 BEAST_EXPECT(!offerExists(env, alice, aliceOfferSeq));
1132 BEAST_EXPECT(!offerExists(env, bob, bobOfferSeq));
1133 BEAST_EXPECT(ownerCount(env, alice) == 2);
1134 }
1135
1136 // apply - open offer can cross with hybrid
1137 {
1138 Env env(*this, features);
1139 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1140 PermissionedDEX(env);
1141
1142 auto const bobOfferSeq{env.seq(bob)};
1143 env(offer(bob, XRP(10), usd(10)), Txflags(tfHybrid), Domain(domainID));
1144 env.close();
1145
1146 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
1147 BEAST_EXPECT(ownerCount(env, bob) == 3);
1148 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), lsfHybrid, true));
1149
1150 auto const aliceOfferSeq{env.seq(alice)};
1151 env(offer(alice, usd(10), XRP(10)));
1152 env.close();
1153
1154 BEAST_EXPECT(!offerExists(env, alice, aliceOfferSeq));
1155 BEAST_EXPECT(!offerExists(env, bob, bobOfferSeq));
1156 BEAST_EXPECT(ownerCount(env, alice) == 2);
1157 }
1158
1159 // apply - by default, hybrid offer tries to cross with offers in the
1160 // domain book
1161 {
1162 Env env(*this, features);
1163 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1164 PermissionedDEX(env);
1165
1166 auto const bobOfferSeq{env.seq(bob)};
1167 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
1168 env.close();
1169
1170 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), 0, true));
1171 BEAST_EXPECT(ownerCount(env, bob) == 3);
1172
1173 // hybrid offer auto crosses with domain offer
1174 auto const aliceOfferSeq{env.seq(alice)};
1175 env(offer(alice, usd(10), XRP(10)), Domain(domainID), Txflags(tfHybrid));
1176 env.close();
1177
1178 BEAST_EXPECT(!offerExists(env, alice, aliceOfferSeq));
1179 BEAST_EXPECT(!offerExists(env, bob, bobOfferSeq));
1180 BEAST_EXPECT(ownerCount(env, alice) == 2);
1181 }
1182
1183 // apply - hybrid offer does not automatically cross with open offers
1184 // because by default, it only tries to cross domain offers
1185 {
1186 Env env(*this, features);
1187 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1188 PermissionedDEX(env);
1189
1190 auto const bobOfferSeq{env.seq(bob)};
1191 env(offer(bob, XRP(10), usd(10)));
1192 env.close();
1193
1194 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), 0, false));
1195 BEAST_EXPECT(ownerCount(env, bob) == 3);
1196
1197 // hybrid offer auto crosses with domain offer
1198 auto const aliceOfferSeq{env.seq(alice)};
1199 env(offer(alice, usd(10), XRP(10)), Domain(domainID), Txflags(tfHybrid));
1200 env.close();
1201
1202 BEAST_EXPECT(offerExists(env, alice, aliceOfferSeq));
1203 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
1204 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), 0, false));
1205 BEAST_EXPECT(checkOffer(env, alice, aliceOfferSeq, usd(10), XRP(10), lsfHybrid, true));
1206 BEAST_EXPECT(ownerCount(env, alice) == 3);
1207 }
1208 }
1209
1210 void
1212 {
1213 testcase("Hybrid invalid offer");
1214
1215 // bob has a hybrid offer and then he is removed from the domain.
1216 // Domain payments must not consume the offer; regular open-book
1217 // payments follow the fixCleanup3_3_0 behavior checked below.
1218 Env env(*this, features);
1219 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1220 PermissionedDEX(env);
1221
1222 auto const hybridOfferSeq{env.seq(bob)};
1223 env(offer(bob, XRP(50), usd(50)), Txflags(tfHybrid), Domain(domainID));
1224 env.close();
1225
1226 // remove bob from domain
1227 env(credentials::deleteCred(domainOwner, bob, domainOwner, credType));
1228 env.close();
1229
1230 // bob's hybrid offer is unfunded and can not be consumed in a domain
1231 // payment
1232 env(pay(alice, carol, usd(5)),
1233 Path(~usd),
1234 Sendmax(XRP(5)),
1235 Domain(domainID),
1237 env.close();
1238 BEAST_EXPECT(checkOffer(env, bob, hybridOfferSeq, XRP(50), usd(50), lsfHybrid, true));
1239
1240 if (features[fixCleanup3_3_0])
1241 {
1242 // Post-fixCleanup3_3_0: hybrid offer can still be consumed via a regular
1243 // open-book payment even though the domain credential was revoked.
1244 auto const carolBalBefore = env.balance(carol, usd);
1245 env(pay(alice, carol, usd(5)), Path(~usd), Sendmax(XRP(5)));
1246 env.close();
1247 BEAST_EXPECT(env.balance(carol, usd) - carolBalBefore == usd(5));
1248 BEAST_EXPECT(checkOffer(env, bob, hybridOfferSeq, XRP(45), usd(45), lsfHybrid, true));
1249
1250 // create a regular offer alongside the hybrid one
1251 auto const regularOfferSeq{env.seq(bob)};
1252 env(offer(bob, XRP(10), usd(10)));
1253 env.close();
1254 BEAST_EXPECT(checkOffer(env, bob, regularOfferSeq, XRP(10), usd(10)));
1255
1256 auto const sleHybridOffer =
1257 env.le(keylet::offer(bob.id(), SeqProxy::rawSequence(hybridOfferSeq)));
1258 if (!BEAST_EXPECT(sleHybridOffer))
1259 return;
1260 auto const openDir =
1261 sleHybridOffer->getFieldArray(sfAdditionalBooks)[0].getFieldH256(sfBookDirectory);
1262 // both offers are in the open book directory
1263 BEAST_EXPECT(checkDirectorySize(env, openDir, 2));
1264
1265 // A regular payment crosses the hybrid offer first (FIFO, older
1266 // offer), then stops; the regular offer is untouched.
1267 env(pay(alice, carol, usd(5)), Path(~usd), Sendmax(XRP(5)));
1268 env.close();
1269
1270 BEAST_EXPECT(checkOffer(env, bob, hybridOfferSeq, XRP(40), usd(40), lsfHybrid, true));
1271 BEAST_EXPECT(checkOffer(env, bob, regularOfferSeq, XRP(10), usd(10)));
1272 BEAST_EXPECT(checkDirectorySize(env, openDir, 2));
1273 }
1274 else
1275 {
1276 // Pre-fixCleanup3_3_0: the open-book traversal
1277 // also runs the offerInDomain eviction check, so the hybrid offer
1278 // is treated as unfunded and the regular payment fails.
1279 env(pay(alice, carol, usd(5)), Path(~usd), Sendmax(XRP(5)), Ter(tecPATH_PARTIAL));
1280 env.close();
1281 BEAST_EXPECT(checkOffer(env, bob, hybridOfferSeq, XRP(50), usd(50), lsfHybrid, true));
1282
1283 // create a regular offer
1284 auto const regularOfferSeq{env.seq(bob)};
1285 env(offer(bob, XRP(10), usd(10)));
1286 env.close();
1287 BEAST_EXPECT(offerExists(env, bob, regularOfferSeq));
1288 BEAST_EXPECT(checkOffer(env, bob, regularOfferSeq, XRP(10), usd(10)));
1289
1290 auto const sleHybridOffer =
1291 env.le(keylet::offer(bob.id(), SeqProxy::rawSequence(hybridOfferSeq)));
1292 if (!BEAST_EXPECT(sleHybridOffer))
1293 return;
1294 auto const openDir =
1295 sleHybridOffer->getFieldArray(sfAdditionalBooks)[0].getFieldH256(sfBookDirectory);
1296 BEAST_EXPECT(checkDirectorySize(env, openDir, 2));
1297
1298 // This payment crosses the regular offer and permanently evicts the
1299 // hybrid offer from the open book (since the payment succeeds, the
1300 // sandbox, including the hybrid eviction, is committed).
1301 env(pay(alice, carol, usd(5)), Path(~usd), Sendmax(XRP(5)));
1302 env.close();
1303
1304 BEAST_EXPECT(!offerExists(env, bob, hybridOfferSeq));
1305 BEAST_EXPECT(checkOffer(env, bob, regularOfferSeq, XRP(5), usd(5)));
1306 BEAST_EXPECT(checkDirectorySize(env, openDir, 1));
1307 }
1308 }
1309
1310 void
1312 {
1313 testcase("Hybrid book step");
1314
1315 // both non domain and domain payments can consume hybrid offer
1316 {
1317 Env env(*this, features);
1318 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1319 PermissionedDEX(env);
1320
1321 auto const hybridOfferSeq{env.seq(bob)};
1322 env(offer(bob, XRP(10), usd(10)), Txflags(tfHybrid), Domain(domainID));
1323 env.close();
1324
1325 env(pay(alice, carol, usd(5)), Path(~usd), Sendmax(XRP(5)), Domain(domainID));
1326 env.close();
1327 BEAST_EXPECT(checkOffer(env, bob, hybridOfferSeq, XRP(5), usd(5), lsfHybrid, true));
1328
1329 // hybrid offer can't be consumed since bob is not in domain anymore
1330 env(pay(alice, carol, usd(5)), Path(~usd), Sendmax(XRP(5)));
1331 env.close();
1332
1333 BEAST_EXPECT(!offerExists(env, bob, hybridOfferSeq));
1334 }
1335
1336 // someone from another domain can't cross hybrid if they specified
1337 // wrong domainID
1338 {
1339 Env env(*this, features);
1340 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1341 PermissionedDEX(env);
1342
1343 // Fund accounts
1344 Account const badDomainOwner("badDomainOwner");
1345 Account const devin("devin");
1346 env.fund(XRP(1000), badDomainOwner, devin);
1347 env.close();
1348
1349 auto const badCredType = "badCred";
1351 {.issuer = badDomainOwner, .credType = badCredType},
1352 };
1353 env(pdomain::setTx(badDomainOwner, credentials));
1354
1355 auto objects = pdomain::getObjects(badDomainOwner, env);
1356 auto const badDomainID = objects.begin()->first;
1357
1358 env(credentials::create(devin, badDomainOwner, badCredType));
1359 env.close();
1360 env(credentials::accept(devin, badDomainOwner, badCredType));
1361 env.close();
1362
1363 auto const hybridOfferSeq{env.seq(bob)};
1364 env(offer(bob, XRP(10), usd(10)), Txflags(tfHybrid), Domain(domainID));
1365 env.close();
1366
1367 // other domains can't consume the offer
1368 env(pay(devin, badDomainOwner, usd(5)),
1369 Path(~usd),
1370 Sendmax(XRP(5)),
1371 Domain(badDomainID),
1372 Ter(tecPATH_DRY));
1373 env.close();
1374 BEAST_EXPECT(checkOffer(env, bob, hybridOfferSeq, XRP(10), usd(10), lsfHybrid, true));
1375
1376 env(pay(alice, carol, usd(5)), Path(~usd), Sendmax(XRP(5)), Domain(domainID));
1377 env.close();
1378 BEAST_EXPECT(checkOffer(env, bob, hybridOfferSeq, XRP(5), usd(5), lsfHybrid, true));
1379
1380 // hybrid offer can't be consumed since bob is not in domain anymore
1381 env(pay(alice, carol, usd(5)), Path(~usd), Sendmax(XRP(5)));
1382 env.close();
1383
1384 BEAST_EXPECT(!offerExists(env, bob, hybridOfferSeq));
1385 }
1386
1387 // test domain payment consuming two offers w/ hybrid offer
1388 {
1389 Env env(*this, features);
1390 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1391 PermissionedDEX(env);
1392
1393 auto const eur = gw["EUR"];
1394 env.trust(eur(1000), alice);
1395 env.close();
1396 env.trust(eur(1000), bob);
1397 env.close();
1398 env.trust(eur(1000), carol);
1399 env.close();
1400 env(pay(gw, bob, eur(100)));
1401 env.close();
1402
1403 auto const usdOfferSeq{env.seq(bob)};
1404 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
1405 env.close();
1406
1407 BEAST_EXPECT(checkOffer(env, bob, usdOfferSeq, XRP(10), usd(10), 0, true));
1408
1409 // payment fail because there isn't eur offer
1410 env(pay(alice, carol, eur(5)),
1411 Path(~usd, ~eur),
1412 Sendmax(XRP(5)),
1413 Domain(domainID),
1415 env.close();
1416 BEAST_EXPECT(checkOffer(env, bob, usdOfferSeq, XRP(10), usd(10), 0, true));
1417
1418 // bob creates a hybrid eur offer
1419 auto const eurOfferSeq{env.seq(bob)};
1420 env(offer(bob, usd(10), eur(10)), Domain(domainID), Txflags(tfHybrid));
1421 env.close();
1422 BEAST_EXPECT(checkOffer(env, bob, eurOfferSeq, usd(10), eur(10), lsfHybrid, true));
1423
1424 // alice successfully consume two domain offers: xrp/usd and usd/eur
1425 env(pay(alice, carol, eur(5)), Path(~usd, ~eur), Sendmax(XRP(5)), Domain(domainID));
1426 env.close();
1427
1428 BEAST_EXPECT(checkOffer(env, bob, usdOfferSeq, XRP(5), usd(5), 0, true));
1429 BEAST_EXPECT(checkOffer(env, bob, eurOfferSeq, usd(5), eur(5), lsfHybrid, true));
1430 }
1431
1432 // test regular payment using a regular offer and a hybrid offer
1433 {
1434 Env env(*this, features);
1435 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1436 PermissionedDEX(env);
1437
1438 auto const eur = gw["EUR"];
1439 env.trust(eur(1000), alice);
1440 env.close();
1441 env.trust(eur(1000), bob);
1442 env.close();
1443 env.trust(eur(1000), carol);
1444 env.close();
1445 env(pay(gw, bob, eur(100)));
1446 env.close();
1447
1448 // bob creates a regular usd offer
1449 auto const usdOfferSeq{env.seq(bob)};
1450 env(offer(bob, XRP(10), usd(10)));
1451 env.close();
1452
1453 BEAST_EXPECT(checkOffer(env, bob, usdOfferSeq, XRP(10), usd(10), 0, false));
1454
1455 // bob creates a hybrid eur offer
1456 auto const eurOfferSeq{env.seq(bob)};
1457 env(offer(bob, usd(10), eur(10)), Domain(domainID), Txflags(tfHybrid));
1458 env.close();
1459 BEAST_EXPECT(checkOffer(env, bob, eurOfferSeq, usd(10), eur(10), lsfHybrid, true));
1460
1461 // alice successfully consume two offers: xrp/usd and usd/eur
1462 env(pay(alice, carol, eur(5)), Path(~usd, ~eur), Sendmax(XRP(5)));
1463 env.close();
1464
1465 BEAST_EXPECT(checkOffer(env, bob, usdOfferSeq, XRP(5), usd(5), 0, false));
1466 BEAST_EXPECT(checkOffer(env, bob, eurOfferSeq, usd(5), eur(5), lsfHybrid, true));
1467 }
1468 }
1469
1470 // Test that a hybrid offer remains crossable in the open book after the
1471 // owner's domain credential expires. A domain payment after expiry should
1472 // fail (domain book evicts the offer in its sandbox), but the open book
1473 // remains usable.
1474 void
1476 {
1477 testcase("Hybrid open book after credential expiry");
1478
1479 Env env(*this, features);
1480 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1481 PermissionedDEX(env);
1482
1483 Account const devin("devin");
1484 env.fund(XRP(100000), devin);
1485 env.close();
1486 env.trust(usd(1000), devin);
1487 env.close();
1488 env(pay(gw, devin, usd(100)));
1489 env.close();
1490
1491 // Give devin a credential that expires far enough in the future to
1492 // survive the setup env.close() calls.
1493 auto jv = credentials::create(devin, domainOwner, credType);
1494 uint32_t const t = env.current()->header().parentCloseTime.time_since_epoch().count();
1495 jv[sfExpiration.jsonName] = t + 100;
1496 env(jv);
1497 env.close();
1498 env(credentials::accept(devin, domainOwner, credType));
1499 env.close();
1500
1501 // Devin creates a hybrid offer: sell USD(10) for XRP(10).
1502 // The offer is placed in both the domain book and the open book.
1503 auto const hybridOfferSeq{env.seq(devin)};
1504 env(offer(devin, XRP(10), usd(10)), Txflags(tfHybrid), Domain(domainID));
1505 env.close();
1506
1507 BEAST_EXPECT(checkOffer(env, devin, hybridOfferSeq, XRP(10), usd(10), lsfHybrid, true));
1508
1509 // A non-domain open-book payment partially crosses the offer while
1510 // devin's credential is still valid.
1511 auto carolBalance = env.balance(carol, usd);
1512 env(pay(alice, carol, usd(5)), Path(~usd), Sendmax(XRP(5)));
1513 env.close();
1514 BEAST_EXPECT(env.balance(carol, usd) - carolBalance == usd(5));
1515 BEAST_EXPECT(checkOffer(env, devin, hybridOfferSeq, XRP(5), usd(5), lsfHybrid, true));
1516
1517 // Advance time so that devin's credential expires.
1518 env.close(std::chrono::seconds(100));
1519
1520 // Confirm devin can no longer create domain offers.
1521 // After fixCleanup3_4_0, OfferCreate deletes the expired credential and
1522 // returns tecEXPIRED (covered in depth by testExpiredCredentialCleanup).
1523 env(offer(devin, XRP(1), usd(1)), Domain(domainID), Ter(tecEXPIRED));
1524 env.close();
1525
1526 // The hybrid offer must still exist in the open book after expiry.
1527 BEAST_EXPECT(offerExists(env, devin, hybridOfferSeq));
1528
1529 // A non-domain open-book payment must cross (not evict) the
1530 // remaining portion of devin's hybrid offer.
1531 carolBalance = env.balance(carol, usd);
1532 env(pay(alice, carol, usd(2)), Path(~usd), Sendmax(XRP(2)));
1533 env.close();
1534
1535 // Carol received USD; the offer was crossed, not evicted.
1536 BEAST_EXPECT(env.balance(carol, usd) - carolBalance == usd(2));
1537 // Offer still exists with 3 USD / 3 XRP remaining.
1538 BEAST_EXPECT(checkOffer(env, devin, hybridOfferSeq, XRP(3), usd(3), lsfHybrid, true));
1539
1540 // A domain payment now fails because the domain book evicts devin's
1541 // offer (his credential has expired). The eviction is rolled back with
1542 // the failed sandbox, so the offer is NOT permanently removed.
1543 env(pay(alice, carol, usd(1)),
1544 Path(~usd),
1545 Sendmax(XRP(1)),
1546 Domain(domainID),
1548 env.close();
1549
1550 // Offer still intact in the open book; domain payment did not
1551 // permanently delete it.
1552 BEAST_EXPECT(checkOffer(env, devin, hybridOfferSeq, XRP(3), usd(3), lsfHybrid, true));
1553
1554 // The open book can still fully consume the remaining portion.
1555 carolBalance = env.balance(carol, usd);
1556 env(pay(alice, carol, usd(3)), Path(~usd), Sendmax(XRP(3)));
1557 env.close();
1558 BEAST_EXPECT(env.balance(carol, usd) - carolBalance == usd(3));
1559 BEAST_EXPECT(!offerExists(env, devin, hybridOfferSeq));
1560 }
1561
1562 void
1564 {
1565 Env env(*this, features);
1566 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1567 PermissionedDEX(env);
1568
1570 offerSeqs.reserve(100);
1571
1572 Book const domainBook{Issue(XRP), Issue(usd), domainID};
1573 Book const openBook{Issue(XRP), Issue(usd), std::nullopt};
1574
1575 auto const domainDir = getBookDirKey(domainBook, XRP(10), usd(10));
1576 auto const openDir = getBookDirKey(openBook, XRP(10), usd(10));
1577
1578 size_t dirCnt = 100;
1579
1580 for (size_t i = 1; i <= dirCnt; i++)
1581 {
1582 auto const bobOfferSeq{env.seq(bob)};
1583 offerSeqs.emplace_back(bobOfferSeq);
1584 env(offer(bob, XRP(10), usd(10)), Txflags(tfHybrid), Domain(domainID));
1585 env.close();
1586
1587 auto const sleOffer =
1588 env.le(keylet::offer(bob.id(), SeqProxy::rawSequence(bobOfferSeq)));
1589 BEAST_EXPECT(sleOffer);
1590 BEAST_EXPECT(sleOffer->getFieldH256(sfBookDirectory) == domainDir);
1591 BEAST_EXPECT(sleOffer->getFieldArray(sfAdditionalBooks).size() == 1);
1592 BEAST_EXPECT(
1593 sleOffer->getFieldArray(sfAdditionalBooks)[0].getFieldH256(sfBookDirectory) ==
1594 openDir);
1595
1596 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), lsfHybrid, true));
1597 BEAST_EXPECT(checkDirectorySize(env, domainDir, i));
1598 BEAST_EXPECT(checkDirectorySize(env, openDir, i));
1599 }
1600
1601 for (auto const offerSeq : offerSeqs)
1602 {
1603 env(offerCancel(bob, offerSeq));
1604 env.close();
1605 dirCnt--;
1606 BEAST_EXPECT(!offerExists(env, bob, offerSeq));
1607 BEAST_EXPECT(checkDirectorySize(env, domainDir, dirCnt));
1608 BEAST_EXPECT(checkDirectorySize(env, openDir, dirCnt));
1609 }
1610 }
1611
1612 void
1614 {
1615 testcase("Auto bridge");
1616
1617 Env env(*this, features);
1618 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1619 PermissionedDEX(env);
1620 auto const eur = gw["EUR"];
1621
1622 for (auto const& account : {alice, bob, carol})
1623 {
1624 env(trust(account, eur(10000)));
1625 env.close();
1626 }
1627
1628 env(pay(gw, carol, eur(1)));
1629 env.close();
1630
1631 auto const aliceOfferSeq{env.seq(alice)};
1632 auto const bobOfferSeq{env.seq(bob)};
1633 env(offer(alice, XRP(100), usd(1)), Domain(domainID));
1634 env(offer(bob, eur(1), XRP(100)), Domain(domainID));
1635 env.close();
1636
1637 // carol's offer should cross bob and alice's offers due to auto
1638 // bridging
1639 auto const carolOfferSeq{env.seq(carol)};
1640 env(offer(carol, usd(1), eur(1)), Domain(domainID));
1641 env.close();
1642
1643 BEAST_EXPECT(!offerExists(env, bob, aliceOfferSeq));
1644 BEAST_EXPECT(!offerExists(env, bob, bobOfferSeq));
1645 BEAST_EXPECT(!offerExists(env, bob, carolOfferSeq));
1646 }
1647
1648 void
1650 {
1651 bool const fixEnabled = features[fixCleanup3_4_0];
1652
1653 testcase << "Expired credential cleanup"
1654 << (fixEnabled ? " (Cleanup3_4_0 enabled)" : " (Cleanup3_4_0 disabled)");
1655
1656 TER const expectedExpiredCredTer = fixEnabled ? tecEXPIRED : tecNO_PERMISSION;
1657
1658 auto const fundAccount =
1659 [](Env& env, Account const& account, Account const& gw, IOU const& usd) {
1660 env.fund(XRP(1000), account);
1661 env.close();
1662 env.trust(usd(1000), account);
1663 env.close();
1664 env(pay(gw, account, usd(100)));
1665 env.close();
1666 };
1667
1668 auto const fundDevin = [&](Env& env, Account const& gw, IOU const& usd) {
1669 Account const devin("devin");
1670 fundAccount(env, devin, gw, usd);
1671 return devin;
1672 };
1673
1674 auto const createExpiringCredential = [](Env& env,
1675 Account const& subject,
1676 Account const& issuer,
1677 std::string const& credType) {
1678 auto jv = credentials::create(subject, issuer, credType);
1679 uint32_t const t = env.current()->header().parentCloseTime.time_since_epoch().count();
1680 jv[sfExpiration.jsonName] = t + 20;
1681 env(jv);
1682 env(credentials::accept(subject, issuer, credType));
1683 env.close();
1684
1685 return keylet::credential(subject.id(), issuer.id(), makeSlice(credType));
1686 };
1687
1688 auto const expectExpiredCredentialState = [&](Env const& env, Keylet const& credKey) {
1689 if (fixEnabled)
1690 {
1691 BEAST_EXPECT(!env.le(credKey));
1692 }
1693 else
1694 {
1695 BEAST_EXPECT(env.le(credKey));
1696 }
1697 };
1698
1699 // A payment referencing a non-existent domain is rejected in preclaim.
1700 {
1701 Env env(*this, features);
1702 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1703 PermissionedDEX(env);
1704
1705 UInt256 const badDomain{
1706 "F10D0CC9A0F9A3CBF585B80BE09A186483668FDBDD39AA7E3370F3649CE134"
1707 "E5"};
1708
1709 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
1710 env.close();
1711
1712 env(pay(alice, bob, usd(10)),
1713 Path(~usd),
1714 Sendmax(XRP(10)),
1715 Domain(badDomain),
1717 env.close();
1718 }
1719
1720 // OfferCreate with an expired credential.
1721 {
1722 Env env(*this, features);
1723 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1724 PermissionedDEX(env);
1725
1726 Account const devin = fundDevin(env, gw, usd);
1727 auto const credKey = createExpiringCredential(env, devin, domainOwner, credType);
1728 BEAST_EXPECT(env.le(credKey)); // credential exists before expiry
1729
1730 env.close(std::chrono::seconds(20));
1731
1732 env(offer(devin, XRP(10), usd(10)), Domain(domainID), Ter(expectedExpiredCredTer));
1733 env.close();
1734
1735 expectExpiredCredentialState(env, credKey);
1736 }
1737
1738 // Payment where the sender's credential is expired.
1739 {
1740 Env env(*this, features);
1741 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1742 PermissionedDEX(env);
1743
1744 Account const devin = fundDevin(env, gw, usd);
1745 auto const credKey = createExpiringCredential(env, devin, domainOwner, credType);
1746
1747 auto const bobOfferSeq{env.seq(bob)};
1748 auto const bobCredKey =
1749 keylet::credential(bob.id(), domainOwner.id(), makeSlice(credType));
1750 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
1751 env.close();
1752
1753 BEAST_EXPECT(env.le(credKey));
1754 BEAST_EXPECT(env.le(bobCredKey));
1755 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
1756
1757 env.close(std::chrono::seconds(20));
1758
1759 env(pay(devin, alice, usd(10)),
1760 Path(~usd),
1761 Sendmax(XRP(10)),
1762 Domain(domainID),
1763 Ter(expectedExpiredCredTer));
1764 env.close();
1765
1766 expectExpiredCredentialState(env, credKey);
1767 BEAST_EXPECT(env.le(bobCredKey));
1768 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
1769 }
1770
1771 // Payment where the destination's credential is expired.
1772 {
1773 Env env(*this, features);
1774 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1775 PermissionedDEX(env);
1776
1777 Account const devin = fundDevin(env, gw, usd);
1778 auto const credKey = createExpiringCredential(env, devin, domainOwner, credType);
1779
1780 auto const bobOfferSeq{env.seq(bob)};
1781 auto const bobCredKey =
1782 keylet::credential(bob.id(), domainOwner.id(), makeSlice(credType));
1783 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
1784 env.close();
1785
1786 BEAST_EXPECT(env.le(credKey));
1787 BEAST_EXPECT(env.le(bobCredKey));
1788 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
1789
1790 env.close(std::chrono::seconds(20));
1791
1792 env(pay(alice, devin, usd(10)),
1793 Path(~usd),
1794 Sendmax(XRP(10)),
1795 Domain(domainID),
1796 Ter(expectedExpiredCredTer));
1797 env.close();
1798
1799 expectExpiredCredentialState(env, credKey);
1800 BEAST_EXPECT(env.le(bobCredKey));
1801 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
1802 }
1803
1804 // Payment where both sender and destination credentials are expired.
1805 {
1806 Env env(*this, features);
1807 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1808 PermissionedDEX(env);
1809
1810 Account const devin = fundDevin(env, gw, usd);
1811 Account const erin("erin");
1812 fundAccount(env, erin, gw, usd);
1813
1814 auto const devinCredKey = createExpiringCredential(env, devin, domainOwner, credType);
1815 auto const erinCredKey = createExpiringCredential(env, erin, domainOwner, credType);
1816
1817 auto const bobOfferSeq{env.seq(bob)};
1818 auto const bobCredKey =
1819 keylet::credential(bob.id(), domainOwner.id(), makeSlice(credType));
1820 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
1821 env.close();
1822
1823 BEAST_EXPECT(env.le(devinCredKey));
1824 BEAST_EXPECT(env.le(erinCredKey));
1825 BEAST_EXPECT(env.le(bobCredKey));
1826 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
1827
1828 env.close(std::chrono::seconds(20));
1829
1830 env(pay(devin, erin, usd(10)),
1831 Path(~usd),
1832 Sendmax(XRP(10)),
1833 Domain(domainID),
1834 Ter(expectedExpiredCredTer));
1835 env.close();
1836
1837 expectExpiredCredentialState(env, devinCredKey);
1838 expectExpiredCredentialState(env, erinCredKey);
1839 BEAST_EXPECT(env.le(bobCredKey));
1840 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
1841 }
1842 }
1843
1844 void
1846 {
1847 bool const fixEnabled = features[fixCleanup3_1_3];
1848
1849 testcase << "Hybrid offer with empty AdditionalBooks"
1850 << (fixEnabled ? " (fixCleanup3_1_3 enabled)" : " (fixCleanup3_1_3 disabled)");
1851
1852 // offerInDomain has two code paths gated by fixCleanup3_1_3:
1853 //
1854 // pre-fix: only rejects a hybrid offer when sfAdditionalBooks is
1855 // entirely absent — an empty array (size 0) passes through.
1856 // post-fix: also rejects a hybrid offer whose sfAdditionalBooks array
1857 // has size != 1 (i.e. 0 or >1 entries).
1858 //
1859 // We create a valid hybrid offer, then directly manipulate its SLE to
1860 // produce the size==0 case that cannot occur via normal transactions,
1861 // and verify that the two code paths produce the expected outcomes.
1862 //
1863 // Note: the PermissionedDEX invariant checker (ValidPermissionedDEX)
1864 // does not flag this malformation for ttPAYMENT — only for
1865 // ttOFFER_CREATE — so the without-fix payment completes as tesSUCCESS.
1866
1867 Env env(*this, features);
1868 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1869 PermissionedDEX(env);
1870
1871 // Create a valid hybrid offer (sfAdditionalBooks has exactly 1 entry)
1872 auto const bobOfferSeq{env.seq(bob)};
1873 env(offer(bob, XRP(10), usd(10)), Txflags(tfHybrid), Domain(domainID));
1874 env.close();
1875 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
1876
1877 // Directly manipulate the offer SLE in the open ledger so that
1878 // sfAdditionalBooks is present but empty (size 0). This is the
1879 // malformed state that fixCleanup3_1_3 is designed to catch.
1880 auto const offerKey = keylet::offer(bob.id(), SeqProxy::rawSequence(bobOfferSeq));
1881 env.app().getOpenLedger().modify([&offerKey](OpenView& view, beast::Journal) {
1882 auto const sle = view.read(offerKey);
1883 if (!sle)
1884 return false;
1885 auto const replacement = std::make_shared<SLE>(*sle, sle->key());
1886 replacement->setFieldArray(sfAdditionalBooks, STArray{});
1887 view.rawReplace(replacement);
1888 return true;
1889 });
1890
1891 if (fixEnabled)
1892 {
1893 // post-fixCleanup3_1_3: offerInDomain rejects the malformed
1894 // offer (size == 0), so no valid domain offer is found.
1895 env(pay(alice, carol, usd(10)),
1896 Path(~usd),
1897 Sendmax(XRP(10)),
1898 Domain(domainID),
1900 }
1901 else
1902 {
1903 // pre-fixCleanup3_1_3: offerInDomain only checks for a missing
1904 // sfAdditionalBooks field; size == 0 passes through, so the
1905 // malformed offer is crossed and the payment succeeds.
1906 env(pay(alice, carol, usd(10)), Path(~usd), Sendmax(XRP(10)), Domain(domainID));
1907 }
1908 }
1909
1910 void
1912 {
1913 bool const fixEnabled = features[fixCleanup3_2_0];
1914 testcase << "Hybrid offer crossing quality"
1915 << (fixEnabled ? " (fixCleanup3_2_0)" : " (pre-fix)");
1916
1917 // Partially-crossed hybrid offer should have consistent quality
1918 // across both book directories.
1919 //
1920 // Steps:
1921 // - Bob places a hybrid offer.
1922 // - Alice places an opposing hybrid offer that partially crosses.
1923 //
1924 // Verify:
1925 // - Domain-book key quality == its sfExchangeRate.
1926 // - Post-fix: open-book key quality == domain-book key quality.
1927 // - Pre-fix: open-book key quality != domain-book key quality
1928 // (key used post-crossing rate, sfExchangeRate used pre-crossing).
1929
1930 Env env(*this, features);
1931 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
1932 PermissionedDEX(env);
1933
1934 // Bob places a hybrid offer: TakerPays = XRP(100), TakerGets = USD(40)
1935 auto const bobOfferSeq{env.seq(bob)};
1936 env(offer(bob, XRP(100), usd(40)), Txflags(tfHybrid), Domain(domainID));
1937 env.close();
1938 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
1939
1940 // Alice places a hybrid offer in the opposite direction that
1941 // partially crosses Bob's offer.
1942 // Alice: TakerPays = USD(100), TakerGets = XRP(300) (rate = 3 XRP/USD)
1943 // Bob's offer is at a better rate (2.5 XRP/USD) so crossing occurs.
1944 auto const aliceOfferSeq{env.seq(alice)};
1945 env(offer(alice, usd(100), XRP(300)), Txflags(tfHybrid), Domain(domainID));
1946 env.close();
1947
1948 // After crossing, Alice's remaining offer should be placed.
1949 auto const sle = env.le(keylet::offer(alice.id(), SeqProxy::rawSequence(aliceOfferSeq)));
1950 BEAST_EXPECT(sle);
1951 BEAST_EXPECT(sle->isFieldPresent(sfAdditionalBooks));
1952 BEAST_EXPECT(sle->getFieldArray(sfAdditionalBooks).size() == 1);
1953
1954 auto const domainDirKey = sle->getFieldH256(sfBookDirectory);
1955 auto const openDirKey =
1956 sle->getFieldArray(sfAdditionalBooks)[0].getFieldH256(sfBookDirectory);
1957
1958 auto const domainQuality = getQuality(domainDirKey);
1959 auto const openQuality = getQuality(openDirKey);
1960
1961 // Read the directory SLEs and check sfExchangeRate vs key quality.
1962 auto const domainDirSle = env.le(Keylet(ltDIR_NODE, domainDirKey));
1963 auto const openDirSle = env.le(Keylet(ltDIR_NODE, openDirKey));
1964 BEAST_EXPECT(domainDirSle);
1965 BEAST_EXPECT(openDirSle);
1966
1967 auto const domainExRate = domainDirSle->getFieldU64(sfExchangeRate);
1968 auto const openExRate = openDirSle->getFieldU64(sfExchangeRate);
1969 auto const preCrossingQuality = std::uint64_t{5623825668291712342ULL};
1970 auto const postCrossingQuality = std::uint64_t{5623825668291712341ULL};
1971
1972 // Domain directory: sfExchangeRate should always match key quality
1973 // (both use the pre-crossing rate). Correct behavior.
1974 BEAST_EXPECT(domainQuality == preCrossingQuality);
1975 BEAST_EXPECT(domainExRate == preCrossingQuality);
1976 BEAST_EXPECT(domainExRate == domainQuality);
1977
1978 if (fixEnabled)
1979 {
1980 // Correct behavior: both directory keys use the pre-crossing rate.
1981 BEAST_EXPECT(openQuality == preCrossingQuality);
1982 BEAST_EXPECT(domainQuality == openQuality);
1983
1984 // sfExchangeRate matches key quality on both directories.
1985 BEAST_EXPECT(openExRate == preCrossingQuality);
1986 BEAST_EXPECT(openExRate == openQuality);
1987 }
1988 else
1989 {
1990 // Wrong legacy behavior: the open-book directory key uses the
1991 // post-crossing rate instead of the domain-book rate.
1992 BEAST_EXPECT(openQuality == postCrossingQuality);
1993 BEAST_EXPECT(domainQuality != openQuality);
1994
1995 // The open-book sfExchangeRate still uses the pre-crossing rate,
1996 // so it no longer matches the actual quality encoded in the
1997 // open-book directory key.
1998 BEAST_EXPECT(openExRate == preCrossingQuality);
1999 BEAST_EXPECT(openExRate != openQuality);
2000 BEAST_EXPECT(openExRate == domainQuality);
2001 }
2002 }
2003
2004 void
2006 {
2007 testcase("LedgerStateFix BookExchangeRate");
2008
2009 // Use the pre-fix path to create a hybrid offer with a mismatched
2010 // sfExchangeRate, then apply LedgerStateFix to correct it.
2011 //
2012 // Steps:
2013 // - Create a partially-crossed hybrid offer (pre-fixCleanup3_2_0)
2014 // so the open-book directory has wrong sfExchangeRate.
2015 // - Re-enable fixCleanup3_2_0 and submit a LedgerStateFix to
2016 // repair the open-book directory's sfExchangeRate.
2017 //
2018 // Verify:
2019 // - Before fix: sfExchangeRate != getQuality(key).
2020 // - After fix: sfExchangeRate == getQuality(key).
2021
2022 {
2023 // Amendment gate: BookExchangeRate fixes require fixCleanup3_2_0.
2024 Env env(*this, features - fixCleanup3_2_0);
2025 Account const carol{"carol"};
2026
2027 env.fund(XRP(1000), carol);
2028 env.close();
2029
2031 }
2032
2033 {
2034 // Preflight check: BookExchangeRate fixes only accept their
2035 // required fix-specific field.
2036 Env env(*this, features);
2037 Account const carol{"carol"};
2038
2039 env.fund(XRP(1000), carol);
2040 env.close();
2041
2042 // BookExchangeRate fixes require sfBookDirectory.
2043 auto missingBookDirectory = ledger_state_fix::bookExchangeRate(carol, UInt256{1});
2044 missingBookDirectory.removeMember(sfBookDirectory.jsonName);
2045 env(missingBookDirectory, Ter(temINVALID));
2046
2047 // BookExchangeRate fixes reject fields that belong to other
2048 // LedgerStateFix types.
2049 auto extraOwner = ledger_state_fix::bookExchangeRate(carol, UInt256{1});
2050 extraOwner[sfOwner.jsonName] = carol.human();
2051 env(extraOwner, Ter(temINVALID));
2052 }
2053
2054 {
2055 Env env(*this, features);
2056 auto const setup = PermissionedDEX(env);
2057 auto const fixFee = drops(env.current()->fees().increment);
2058
2059 {
2060 // Preclaim check: the target directory must exist.
2061 env(ledger_state_fix::bookExchangeRate(setup.carol, UInt256{1}),
2062 Fee(fixFee),
2064 }
2065
2066 {
2067 // Preclaim check: the target directory must be a book root
2068 // page. Owner directories are ltDIR_NODE entries, but they do
2069 // not carry sfExchangeRate.
2070 auto const ownerDir = keylet::ownerDir(setup.bob.id());
2071 auto const ownerDirSle = env.le(ownerDir);
2072 BEAST_EXPECT(ownerDirSle);
2073 BEAST_EXPECT(!ownerDirSle->isFieldPresent(sfExchangeRate));
2074
2075 env(ledger_state_fix::bookExchangeRate(setup.carol, ownerDir.key),
2076 Fee(fixFee),
2078 }
2079
2080 {
2081 // Preclaim check: a correct sfExchangeRate leaves nothing to
2082 // repair.
2083 auto const bobOfferSeq{env.seq(setup.bob)};
2084 env(offer(setup.bob, XRP(100), setup.usd(40)));
2085 env.close();
2086
2087 auto const sle =
2088 env.le(keylet::offer(setup.bob.id(), SeqProxy::rawSequence(bobOfferSeq)));
2089 BEAST_EXPECT(sle);
2090
2091 auto const dirKey = sle->getFieldH256(sfBookDirectory);
2092 {
2093 auto const dirSle = env.le(Keylet(ltDIR_NODE, dirKey));
2094 BEAST_EXPECT(dirSle);
2095 auto const exchangeRate = dirSle->getFieldU64(sfExchangeRate);
2096 auto const quality = getQuality(dirKey);
2097 BEAST_EXPECT(exchangeRate == quality);
2098 }
2099
2100 env(ledger_state_fix::bookExchangeRate(setup.carol, dirKey),
2101 Fee(fixFee),
2103 }
2104 }
2105
2106 {
2107 // Repair path: start without fixCleanup3_2_0 to produce the
2108 // mismatch, then enable the amendment and fix it.
2109 Env env(*this, features - fixCleanup3_2_0);
2110 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
2111 PermissionedDEX(env);
2112
2113 // Bob places a hybrid offer.
2114 env(offer(bob, XRP(100), usd(40)), Txflags(tfHybrid), Domain(domainID));
2115 env.close();
2116
2117 // Alice partially crosses Bob.
2118 auto const aliceOfferSeq{env.seq(alice)};
2119 env(offer(alice, usd(100), XRP(300)), Txflags(tfHybrid), Domain(domainID));
2120 env.close();
2121
2122 auto const sle =
2123 env.le(keylet::offer(alice.id(), SeqProxy::rawSequence(aliceOfferSeq)));
2124 BEAST_EXPECT(sle);
2125
2126 auto const openDirKey =
2127 sle->getFieldArray(sfAdditionalBooks)[0].getFieldH256(sfBookDirectory);
2128
2129 auto const preCrossingQuality = std::uint64_t{5623825668291712342ULL};
2130 auto const postCrossingQuality = std::uint64_t{5623825668291712341ULL};
2131
2132 // Confirm mismatch exists.
2133 {
2134 auto const dirSle = env.le(Keylet(ltDIR_NODE, openDirKey));
2135 BEAST_EXPECT(dirSle);
2136 auto const exchangeRate = dirSle->getFieldU64(sfExchangeRate);
2137 auto const quality = getQuality(openDirKey);
2138 BEAST_EXPECT(exchangeRate == preCrossingQuality);
2139 BEAST_EXPECT(quality == postCrossingQuality);
2140 BEAST_EXPECT(exchangeRate != quality);
2141 }
2142
2143 // Enable fixCleanup3_2_0 and apply the LedgerStateFix.
2144 env.enableFeature(fixCleanup3_2_0);
2145 env.close();
2146
2147 auto const fixFee = drops(env.current()->fees().increment);
2148 env(ledger_state_fix::bookExchangeRate(carol, openDirKey), Fee(fixFee));
2149 env.close();
2150
2151 // Confirm sfExchangeRate now matches the key quality.
2152 {
2153 auto const dirSle = env.le(Keylet(ltDIR_NODE, openDirKey));
2154 BEAST_EXPECT(dirSle);
2155 auto const exchangeRate = dirSle->getFieldU64(sfExchangeRate);
2156 auto const quality = getQuality(openDirKey);
2157 BEAST_EXPECT(exchangeRate == postCrossingQuality);
2158 BEAST_EXPECT(quality == postCrossingQuality);
2159 BEAST_EXPECT(exchangeRate == quality);
2160 }
2161
2162 // Submitting again should fail — nothing to fix.
2163 env(ledger_state_fix::bookExchangeRate(carol, openDirKey),
2164 Fee(fixFee),
2166 }
2167 }
2168
2169 void
2171 {
2172 bool const fixEnabled = features[fixCleanup3_2_0];
2173
2174 testcase << "Cancel regular offer via domain OfferCreate"
2175 << (fixEnabled ? " (fixCleanup3_2_0 enabled)" : " (fixCleanup3_2_0 disabled)");
2176
2177 // An OfferCreate with sfDomainID and sfOfferSequence pointing to
2178 // the user's own non-domain offer should atomically cancel the
2179 // regular offer and place the new domain offer.
2180 //
2181 // Pre-fixCleanup3_2_0: ValidPermissionedDEX flagged the deleted
2182 // regular offer, so the transaction failed with tecINVARIANT_FAILED.
2183 // Post-fixCleanup3_2_0: the invariant ignores deletions and the
2184 // transaction succeeds.
2185
2186 Env env(*this, features);
2187 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
2188 PermissionedDEX(env);
2189
2190 auto const regularSeq = env.seq(bob);
2191 env(offer(bob, XRP(10), usd(10)));
2192 env.close();
2193 BEAST_EXPECT(checkOffer(env, bob, regularSeq, XRP(10), usd(10), 0, false));
2194
2195 auto const domainSeq = env.seq(bob);
2196 if (fixEnabled)
2197 {
2198 env(offer(bob, XRP(20), usd(20)),
2199 Domain(domainID),
2200 Json(jss::OfferSequence, regularSeq));
2201 env.close();
2202 BEAST_EXPECT(!offerExists(env, bob, regularSeq));
2203 BEAST_EXPECT(checkOffer(env, bob, domainSeq, XRP(20), usd(20), 0, true));
2204 }
2205 else
2206 {
2207 env(offer(bob, XRP(20), usd(20)),
2208 Domain(domainID),
2209 Json(jss::OfferSequence, regularSeq),
2211 env.close();
2212 BEAST_EXPECT(offerExists(env, bob, regularSeq));
2213 BEAST_EXPECT(!offerExists(env, bob, domainSeq));
2214 }
2215 }
2216
2217 void
2219 {
2220 bool const fixEnabled = features[fixCleanup3_4_0];
2221
2222 testcase << "Domain offer indexed in the wrong domain book"
2223 << (fixEnabled ? " (fixCleanup3_4_0 enabled)" : " (fixCleanup3_4_0 disabled)");
2224
2225 // Bob (a member of domains A and B) places an offer in domain A's
2226 // book, which we then corrupt to claim domain B while it stays in
2227 // domain A's book. A payment routed through domain A meets this offer.
2228 //
2229 // - With fixCleanup3_4_0: OfferStream sees the offer's domain (B)
2230 // mismatch the book (A) and errors out -> tecPATH_PARTIAL.
2231 // - Without it: OfferStream only checks the offer's own domain (B,
2232 // which Bob is in), so it is used; the invariant then catches the
2233 // mismatch -> tecINVARIANT_FAILED.
2234 //
2235 // Either way the payment fails and the offer is left untouched.
2236
2237 Env env(*this, features);
2238 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
2239 PermissionedDEX(env);
2240
2241 // A second domain that Bob also belongs to.
2242 Account const bobAcct = bob;
2243 auto const domainID2 =
2244 setupDomain(env, {bobAcct}, Account("permdex-domainOwner2"), "permdex-cred2");
2245
2246 // Bob places a domain offer in domain A's book.
2247 auto const bobOfferSeq{env.seq(bob)};
2248 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
2249 env.close();
2250 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), 0, true));
2251
2252 // Corrupt the offer: point its sfDomainID at domain B while it stays
2253 // indexed in domain A's book directory.
2254 auto const offerKey = keylet::offer(bob.id(), SeqProxy::rawSequence(bobOfferSeq));
2255 env.app().getOpenLedger().modify([&offerKey, &domainID2](OpenView& view, beast::Journal) {
2256 auto const sle = view.read(offerKey);
2257 if (!sle)
2258 return false;
2259 auto const replacement = std::make_shared<SLE>(*sle, sle->key());
2260 replacement->setFieldH256(sfDomainID, domainID2);
2261 view.rawReplace(replacement);
2262 return true;
2263 });
2264
2265 if (fixEnabled)
2266 {
2267 // With the fix: OfferStream rejects the mismatched offer.
2268 env(pay(alice, carol, usd(10)),
2269 Path(~usd),
2270 Sendmax(XRP(10)),
2271 Domain(domainID),
2273 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
2274 }
2275 else
2276 {
2277 // Without the fix: the offer is used, then the invariant
2278 // rejects the whole transaction.
2279 env(pay(alice, carol, usd(10)),
2280 Path(~usd),
2281 Sendmax(XRP(10)),
2282 Domain(domainID),
2284 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
2285 }
2286 }
2287
2288 void
2290 {
2291 bool const fixEnabled = features[fixCleanup3_4_0];
2292
2293 testcase << "Offer without a domain indexed in a domain book"
2294 << (fixEnabled ? " (fixCleanup3_4_0 enabled)" : " (fixCleanup3_4_0 disabled)");
2295
2296 // Same corruption as testDomainOfferInWrongBook, except the offer
2297 // loses sfDomainID entirely instead of pointing at another domain
2298 // while it stays indexed in domain A's book.
2299 //
2300 // - With fixCleanup3_4_0: OfferStream sees an offer that claims no
2301 // domain in a domain book and errors out -> tecPATH_PARTIAL.
2302 // - Without it: neither the domain mismatch check nor the domain
2303 // membership check fires (both are gated on sfDomainID being
2304 // present), and the invariant does not catch it either because the
2305 // offer is fully consumed and deleted. The payment succeeds using an
2306 // offer that was never credential checked.
2307
2308 Env env(*this, features);
2309 auto const& [gw, domainOwner, alice, bob, carol, usd, domainID, credType] =
2310 PermissionedDEX(env);
2311
2312 // Bob places a domain offer in domain A's book.
2313 auto const bobOfferSeq{env.seq(bob)};
2314 env(offer(bob, XRP(10), usd(10)), Domain(domainID));
2315 env.close();
2316 BEAST_EXPECT(checkOffer(env, bob, bobOfferSeq, XRP(10), usd(10), 0, true));
2317
2318 // Corrupt the offer: drop sfDomainID while it stays indexed in domain
2319 // A's book directory.
2320 auto const offerKey = keylet::offer(bob.id(), SeqProxy::rawSequence(bobOfferSeq));
2321 env.app().getOpenLedger().modify([&offerKey](OpenView& view, beast::Journal) {
2322 auto const sle = view.read(offerKey);
2323 if (!sle)
2324 return false;
2325 auto const replacement = std::make_shared<SLE>(*sle, sle->key());
2326 replacement->makeFieldAbsent(sfDomainID);
2327 view.rawReplace(replacement);
2328 return true;
2329 });
2330
2331 auto const carolBefore = env.balance(carol, usd);
2332
2333 if (fixEnabled)
2334 {
2335 // With the fix: OfferStream rejects the domainless offer.
2336 env(pay(alice, carol, usd(10)),
2337 Path(~usd),
2338 Sendmax(XRP(10)),
2339 Domain(domainID),
2341 BEAST_EXPECT(offerExists(env, bob, bobOfferSeq));
2342 BEAST_EXPECT(env.balance(carol, usd) - carolBefore == usd(0));
2343 }
2344 else
2345 {
2346 // Without the fix: the offer is silently usable in the domain
2347 // book, and the payment goes through.
2348 env(pay(alice, carol, usd(10)), Path(~usd), Sendmax(XRP(10)), Domain(domainID));
2349 BEAST_EXPECT(!offerExists(env, bob, bobOfferSeq));
2350 BEAST_EXPECT(env.balance(carol, usd) - carolBefore == usd(10));
2351 }
2352 }
2353
2354 void
2356 {
2357 bool const fixEnabled = features[fixCleanup3_4_0];
2358
2359 testcase << "Replace domain offer via OfferCreate"
2360 << (fixEnabled ? " (fixCleanup3_4_0 enabled)" : " (fixCleanup3_4_0 disabled)");
2361
2362 Env env(*this, features);
2363 auto const& [gw, domainOwner, alice, bob, carol, usd, domainA, credType] =
2364 PermissionedDEX(env);
2365
2366 Account const domainOwnerB("permdex-domainOwnerB");
2367 auto const domainB =
2368 setupDomain(env, {alice, bob, carol, gw}, domainOwnerB, "permdex-other-domain");
2369 BEAST_EXPECT(domainA != domainB);
2370
2371 auto const oldSeq = env.seq(alice);
2372 env(offer(alice, usd(100), XRP(1)), Domain(domainA));
2373 env.close();
2374
2375 BEAST_EXPECT(checkOffer(env, alice, oldSeq, usd(100), XRP(1), 0, true));
2376 auto const oldOffer = env.le(keylet::offer(alice.id(), SeqProxy::rawSequence(oldSeq)));
2377 if (!BEAST_EXPECT(oldOffer))
2378 return;
2379 BEAST_EXPECT(oldOffer->getFieldH256(sfDomainID) == domainA);
2380
2381 auto const newSeq = env.seq(alice);
2382 // The invariant should reject mixing active Permissioned DEX domains,
2383 // not a domain that is only touched because its offer is being deleted.
2384 if (fixEnabled)
2385 {
2386 env(offer(alice, usd(100), XRP(2)), Domain(domainB), Json(jss::OfferSequence, oldSeq));
2387 env.close();
2388
2389 BEAST_EXPECT(!offerExists(env, alice, oldSeq));
2390 BEAST_EXPECT(checkOffer(env, alice, newSeq, usd(100), XRP(2), 0, true));
2391 auto const newOffer = env.le(keylet::offer(alice.id(), SeqProxy::rawSequence(newSeq)));
2392 if (!BEAST_EXPECT(newOffer))
2393 return;
2394 BEAST_EXPECT(newOffer->getFieldH256(sfDomainID) == domainB);
2395 }
2396 else
2397 {
2398 env(offer(alice, usd(100), XRP(2)),
2399 Domain(domainB),
2400 Json(jss::OfferSequence, oldSeq),
2402 env.close();
2403
2404 BEAST_EXPECT(checkOffer(env, alice, oldSeq, usd(100), XRP(1), 0, true));
2405 BEAST_EXPECT(!offerExists(env, alice, newSeq));
2406 }
2407 }
2408
2409public:
2410 void
2411 run() override
2412 {
2414
2415 // Test domain offer (w/o hybrid)
2416 testOfferCreate(all);
2417 testOfferCreate(all - fixCleanup3_2_0);
2418 testOfferCreate(all - fixCleanup3_4_0);
2419 testPayment(all);
2420 testPayment(all - fixCleanup3_2_0);
2421 testBookStep(all);
2422 testRippling(all);
2425 testAmmNotUsed(all);
2427 testAmmQualityNotLeaked(all - fixCleanup3_3_0);
2428 testAutoBridge(all);
2430 testExpiredCredentialCleanup(all - fixCleanup3_4_0);
2431
2432 // Test hybrid offers
2434 testHybridBookStep(all);
2435 testHybridInvalidOffer(all - fixCleanup3_3_0);
2440 testHybridMalformedOffer(all - fixCleanup3_1_3);
2442 testHybridOfferCrossingQuality(all - fixCleanup3_2_0);
2444
2445 // Cancelling a regular offer in a domain OfferCreate is allowed
2446 // only after fixCleanup3_2_0.
2448 testCancelRegularOfferWithDomainCreate(all - fixCleanup3_2_0);
2449
2450 // A domain offer indexed in the wrong domain book is caught only
2451 // after fixCleanup3_4_0. (Not an existing bug, but defensive testing)
2453 testDomainOfferInWrongBook(all - fixCleanup3_4_0);
2455 testDomainBookOfferMissingDomain(all - fixCleanup3_4_0);
2456
2458 testReplaceDomainOfferWithOtherDomainOffer(all - fixCleanup3_4_0);
2459 }
2460};
2461
2463
2464} // namespace xrpl::test
T any_of(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
A testsuite class.
Definition suite.h:52
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Value removeMember(char const *key)
Remove and return the named member.
Specifies an order book.
Definition Book.h:28
A currency issued by an account.
Definition Issue.h:18
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
void rawReplace(SLE::Ref sle) override
Unconditionally replace a state item.
Definition OpenView.cpp:246
SLE::const_pointer read(Keylet const &k) const override
Return the state item associated with a key.
Definition OpenView.cpp:170
static constexpr SeqProxy rawSequence(std::uint32_t v)
Factory function to return a sequence-based SeqProxy.
Definition SeqProxy.h:62
void testRippling(FeatureBitset features)
static std::optional< UInt256 > getDefaultOfferDirKey(Env const &env, Account const &account, std::uint32_t offerSeq)
static bool checkDirectorySize(Env const &env, UInt256 directory, std::uint32_t dirSize)
void testOfferCreate(FeatureBitset features)
void testHybridOfferDirectories(FeatureBitset features)
static UInt256 getBookDirKey(Book const &book, STAmount const &takerPays, STAmount const &takerGets)
void testExpiredCredentialCleanup(FeatureBitset features)
void testHybridOfferCreate(FeatureBitset features)
void testCancelRegularOfferWithDomainCreate(FeatureBitset features)
void testHybridOpenBookAfterCredentialExpiry(FeatureBitset features)
void testHybridOfferCrossingQuality(FeatureBitset features)
void testAutoBridge(FeatureBitset features)
void testOfferTokenIssuerInDomain(FeatureBitset features)
void run() override
Runs the suite.
void testPayment(FeatureBitset features)
void testHybridBookStep(FeatureBitset features)
void testAmmNotUsed(FeatureBitset features)
void testBookStep(FeatureBitset features)
void testDomainOfferInWrongBook(FeatureBitset features)
static bool offerExists(Env const &env, Account const &account, std::uint32_t offerSeq)
void testAmmQualityNotLeaked(FeatureBitset features)
void testReplaceDomainOfferWithOtherDomainOffer(FeatureBitset features)
static bool checkOffer(Env const &env, Account const &account, std::uint32_t offerSeq, STAmount const &takerPays, STAmount const &takerGets, uint32_t const flags=0, bool const domainOffer=false)
void testDomainBookOfferMissingDomain(FeatureBitset features)
void testBookExchangeRateFix(FeatureBitset features)
void testHybridMalformedOffer(FeatureBitset features)
void testHybridInvalidOffer(FeatureBitset features)
void testRemoveUnfundedOffer(FeatureBitset features)
Convenience class to test AMM functionality.
Immutable cryptographic account descriptor.
Definition jtx/Account.h:21
std::string const & human() const
Returns the human readable public key.
A balance matches.
Definition balance.h:24
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
SLE::const_pointer le(Account const &account) const
Return an account root.
Definition Env.cpp:311
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
PrettyAmount balance(Account const &account) const
Returns the XRP balance on an account.
Definition Env.cpp:201
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition Env.cpp:354
void enableFeature(UInt256 const feature)
Definition Env.cpp:709
void require(Args const &... args)
Check a set of requirements.
Definition Env.h:766
std::shared_ptr< OpenView const > current() const
Returns the current ledger.
Definition Env.h:377
Set the fee on a JTx.
Definition fee.h:20
Converts to IOU Issue or STAmount.
Inject raw JSON.
Definition jtx_json.h:16
Add a path.
Definition paths.h:47
Set Paths, SendMax on a JTx.
Definition paths.h:23
Sets the SendMax on a JTx.
Definition sendmax.h:16
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition ter.h:18
Set the flags on a JTx.
Definition txflags.h:14
T emplace_back(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 ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition Indexes.cpp:415
Keylet page(UInt256 const &root, std::uint64_t const index=0) noexcept
A page in a directory.
Definition Indexes.cpp:421
Keylet credential(AccountID const &subject, AccountID const &issuer, Slice const &credType) noexcept
Definition Indexes.cpp:559
json::Value deleteCred(jtx::Account const &acc, jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
Definition creds.cpp:40
json::Value accept(jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
Definition creds.cpp:29
json::Value create(jtx::Account const &subject, jtx::Account const &issuer, std::string_view credType)
Definition creds.cpp:16
Directory operations.
Definition directory.h:19
json::Value bookExchangeRate(jtx::Account const &acct, UInt256 const &bookDir)
Repair sfExchangeRate on a book directory's first page.
std::map< UInt256, json::Value > getObjects(Account const &account, Env &env, bool withType)
std::vector< Credential > Credentials
json::Value setTx(AccountID const &account, Credentials const &credentials, std::optional< UInt256 > domain)
json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition pay.cpp:14
json::Value offerCancel(Account const &account, std::uint32_t offerSeq)
Cancel an offer.
Definition offer.cpp:31
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
std::uint32_t ownerCount(Env const &env, Account const &account)
FeatureBitset testableAmendments()
Definition Env.h:92
json::Value offer(Account const &account, STAmount const &takerPays, STAmount const &takerGets, std::uint32_t flags)
Create an offer.
Definition offer.cpp:14
json::Value trust(Account const &account, STAmount const &amount, std::uint32_t flags)
Modify a trust line.
Definition trust.cpp:18
UInt256 setupDomain(jtx::Env &env, std::vector< jtx::Account > const &accounts, jtx::Account const &domainOwner, std::string const &credType)
PrettyAmount drops(Integer i)
Returns an XRP PrettyAmount, which is trivially convertible to STAmount.
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BaseUInt< 256 > Domain
Domain is a 256-bit hash representing a specific domain.
Definition UintTypes.h:59
std::uint64_t getQuality(UInt256 const &uBase)
Definition Indexes.cpp:207
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228
BaseUInt< 256 > UInt256
Definition base_uint.h:581
std::uint64_t getRate(STAmount const &offerOut, STAmount const &offerIn)
@ temINVALID
Definition TER.h:98
@ temINVALID_FLAG
Definition TER.h:99
@ temMALFORMED
Definition TER.h:75
@ temDISABLED
Definition TER.h:102
TERSubset< CanCvtToTER > TER
Definition TER.h:657
@ tecPATH_PARTIAL
Definition TER.h:293
@ tecPATH_DRY
Definition TER.h:305
@ tecOBJECT_NOT_FOUND
Definition TER.h:337
@ tecINVARIANT_FAILED
Definition TER.h:324
@ tecEXPIRED
Definition TER.h:325
@ tecNO_PERMISSION
Definition TER.h:316
T reserve(T... args)
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
UInt256 key
Definition Keylet.h:21
T value_or(T... args)