-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathOffer_test.cpp
5479 lines (4514 loc) · 198 KB
/
Offer_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012-2017 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <test/jtx.h>
#include <test/jtx/PathSet.h>
#include <test/jtx/WSClient.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/Quality.h>
#include <xrpl/protocol/jss.h>
namespace ripple {
namespace test {
class OfferBaseUtil_test : public beast::unit_test::suite
{
XRPAmount
reserve(jtx::Env& env, std::uint32_t count)
{
return env.current()->fees().accountReserve(count);
}
std::uint32_t
lastClose(jtx::Env& env)
{
return env.current()->info().parentCloseTime.time_since_epoch().count();
}
static auto
ledgerEntryOffer(
jtx::Env& env,
jtx::Account const& acct,
std::uint32_t offer_seq)
{
Json::Value jvParams;
jvParams[jss::offer][jss::account] = acct.human();
jvParams[jss::offer][jss::seq] = offer_seq;
return env.rpc(
"json", "ledger_entry", to_string(jvParams))[jss::result];
}
static auto
getBookOffers(
jtx::Env& env,
Issue const& taker_pays,
Issue const& taker_gets)
{
Json::Value jvbp;
jvbp[jss::ledger_index] = "current";
jvbp[jss::taker_pays][jss::currency] = to_string(taker_pays.currency);
jvbp[jss::taker_pays][jss::issuer] = to_string(taker_pays.account);
jvbp[jss::taker_gets][jss::currency] = to_string(taker_gets.currency);
jvbp[jss::taker_gets][jss::issuer] = to_string(taker_gets.account);
return env.rpc("json", "book_offers", to_string(jvbp))[jss::result];
}
public:
void
testRmFundedOffer(FeatureBitset features)
{
testcase("Incorrect Removal of Funded Offers");
// We need at least two paths. One at good quality and one at bad
// quality. The bad quality path needs two offer books in a row.
// Each offer book should have two offers at the same quality, the
// offers should be completely consumed, and the payment should
// should require both offers to be satisfied. The first offer must
// be "taker gets" XRP. Old, broken would remove the first
// "taker gets" xrp offer, even though the offer is still funded and
// not used for the payment.
using namespace jtx;
Env env{*this, features};
auto const gw = Account{"gateway"};
auto const USD = gw["USD"];
auto const BTC = gw["BTC"];
Account const alice{"alice"};
Account const bob{"bob"};
Account const carol{"carol"};
env.fund(XRP(10000), alice, bob, carol, gw);
env.trust(USD(1000), alice, bob, carol);
env.trust(BTC(1000), alice, bob, carol);
env(pay(gw, alice, BTC(1000)));
env(pay(gw, carol, USD(1000)));
env(pay(gw, carol, BTC(1000)));
// Must be two offers at the same quality
// "taker gets" must be XRP
// (Different amounts so I can distinguish the offers)
env(offer(carol, BTC(49), XRP(49)));
env(offer(carol, BTC(51), XRP(51)));
// Offers for the poor quality path
// Must be two offers at the same quality
env(offer(carol, XRP(50), USD(50)));
env(offer(carol, XRP(50), USD(50)));
// Offers for the good quality path
env(offer(carol, BTC(1), USD(100)));
PathSet paths(Path(XRP, USD), Path(USD));
env(pay(alice, bob, USD(100)),
json(paths.json()),
sendmax(BTC(1000)),
txflags(tfPartialPayment));
env.require(balance(bob, USD(100)));
BEAST_EXPECT(
!isOffer(env, carol, BTC(1), USD(100)) &&
isOffer(env, carol, BTC(49), XRP(49)));
}
void
testCanceledOffer(FeatureBitset features)
{
testcase("Removing Canceled Offers");
using namespace jtx;
Env env{*this, features};
auto const gw = Account{"gateway"};
auto const alice = Account{"alice"};
auto const USD = gw["USD"];
env.fund(XRP(10000), alice, gw);
env.close();
env.trust(USD(100), alice);
env.close();
env(pay(gw, alice, USD(50)));
env.close();
auto const offer1Seq = env.seq(alice);
env(offer(alice, XRP(500), USD(100)), require(offers(alice, 1)));
env.close();
BEAST_EXPECT(isOffer(env, alice, XRP(500), USD(100)));
// cancel the offer above and replace it with a new offer
auto const offer2Seq = env.seq(alice);
env(offer(alice, XRP(300), USD(100)),
json(jss::OfferSequence, offer1Seq),
require(offers(alice, 1)));
env.close();
BEAST_EXPECT(
isOffer(env, alice, XRP(300), USD(100)) &&
!isOffer(env, alice, XRP(500), USD(100)));
// Test canceling non-existent offer.
// auto const offer3Seq = env.seq (alice);
env(offer(alice, XRP(400), USD(200)),
json(jss::OfferSequence, offer1Seq),
require(offers(alice, 2)));
env.close();
BEAST_EXPECT(
isOffer(env, alice, XRP(300), USD(100)) &&
isOffer(env, alice, XRP(400), USD(200)));
// Test cancellation now with OfferCancel tx
auto const offer4Seq = env.seq(alice);
env(offer(alice, XRP(222), USD(111)), require(offers(alice, 3)));
env.close();
BEAST_EXPECT(isOffer(env, alice, XRP(222), USD(111)));
env(offer_cancel(alice, offer4Seq));
env.close();
BEAST_EXPECT(env.seq(alice) == offer4Seq + 2);
BEAST_EXPECT(!isOffer(env, alice, XRP(222), USD(111)));
// Create an offer that both fails with a tecEXPIRED code and removes
// an offer. Show that the attempt to remove the offer fails.
env.require(offers(alice, 2));
// featureDepositPreauths changes the return code on an expired Offer.
// Adapt to that.
bool const featPreauth{features[featureDepositPreauth]};
env(offer(alice, XRP(5), USD(2)),
json(sfExpiration.fieldName, lastClose(env)),
json(jss::OfferSequence, offer2Seq),
ter(featPreauth ? TER{tecEXPIRED} : TER{tesSUCCESS}));
env.close();
env.require(offers(alice, 2));
BEAST_EXPECT(isOffer(env, alice, XRP(300), USD(100))); // offer2
BEAST_EXPECT(!isOffer(env, alice, XRP(5), USD(2))); // expired
}
void
testTinyPayment(FeatureBitset features)
{
testcase("Tiny payments");
// Regression test for tiny payments
using namespace jtx;
using namespace std::chrono_literals;
auto const alice = Account{"alice"};
auto const bob = Account{"bob"};
auto const carol = Account{"carol"};
auto const gw = Account{"gw"};
auto const USD = gw["USD"];
auto const EUR = gw["EUR"];
Env env{*this, features};
env.fund(XRP(10000), alice, bob, carol, gw);
env.trust(USD(1000), alice, bob, carol);
env.trust(EUR(1000), alice, bob, carol);
env(pay(gw, alice, USD(100)));
env(pay(gw, carol, EUR(100)));
// Create more offers than the loop max count in DeliverNodeReverse
// Note: the DeliverNodeReverse code has been removed; however since
// this is a regression test the original test is being left as-is for
// now.
for (int i = 0; i < 101; ++i)
env(offer(carol, USD(1), EUR(2)));
env(pay(alice, bob, EUR(epsilon)), path(~EUR), sendmax(USD(100)));
}
void
testXRPTinyPayment(FeatureBitset features)
{
testcase("XRP Tiny payments");
// Regression test for tiny xrp payments
// In some cases, when the payment code calculates
// the amount of xrp needed as input to an xrp->iou offer
// it would incorrectly round the amount to zero (even when
// round-up was set to true).
// The bug would cause funded offers to be incorrectly removed
// because the code thought they were unfunded.
// The conditions to trigger the bug are:
// 1) When we calculate the amount of input xrp needed for an offer
// from xrp->iou, the amount is less than 1 drop (after rounding
// up the float representation).
// 2) There is another offer in the same book with a quality
// sufficiently bad that when calculating the input amount
// needed the amount is not set to zero.
using namespace jtx;
using namespace std::chrono_literals;
auto const alice = Account{"alice"};
auto const bob = Account{"bob"};
auto const carol = Account{"carol"};
auto const dan = Account{"dan"};
auto const erin = Account{"erin"};
auto const gw = Account{"gw"};
auto const USD = gw["USD"];
Env env{*this, features};
env.fund(XRP(10000), alice, bob, carol, dan, erin, gw);
env.close();
env.trust(USD(1000), alice, bob, carol, dan, erin);
env.close();
env(pay(gw, carol, USD(0.99999)));
env(pay(gw, dan, USD(1)));
env(pay(gw, erin, USD(1)));
env.close();
// Carol doesn't quite have enough funds for this offer
// The amount left after this offer is taken will cause
// STAmount to incorrectly round to zero when the next offer
// (at a good quality) is considered. (when the now removed
// stAmountCalcSwitchover2 patch was inactive)
env(offer(carol, drops(1), USD(0.99999)));
// Offer at a quality poor enough so when the input xrp is
// calculated in the reverse pass, the amount is not zero.
env(offer(dan, XRP(100), USD(1)));
env.close();
// This is the funded offer that will be incorrectly removed.
// It is considered after the offer from carol, which leaves a
// tiny amount left to pay. When calculating the amount of xrp
// needed for this offer, it will incorrectly compute zero in both
// the forward and reverse passes (when the now removed
// stAmountCalcSwitchover2 was inactive.)
env(offer(erin, drops(2), USD(1)));
env(pay(alice, bob, USD(1)),
path(~USD),
sendmax(XRP(102)),
txflags(tfNoRippleDirect | tfPartialPayment));
env.require(offers(carol, 0), offers(dan, 1));
// offer was correctly consumed. There is still some
// liquidity left on that offer.
env.require(balance(erin, USD(0.99999)), offers(erin, 1));
}
void
testRmSmallIncreasedQOffersXRP(FeatureBitset features)
{
testcase("Rm small increased q offers XRP");
// Carol places an offer, but cannot fully fund the offer. When her
// funding is taken into account, the offer's quality drops below its
// initial quality and has an input amount of 1 drop. This is removed as
// an offer that may block offer books.
using namespace jtx;
using namespace std::chrono_literals;
auto const alice = Account{"alice"};
auto const bob = Account{"bob"};
auto const carol = Account{"carol"};
auto const gw = Account{"gw"};
auto const USD = gw["USD"];
// Test offer crossing
for (auto crossBothOffers : {false, true})
{
Env env{*this, features};
env.fund(XRP(10000), alice, bob, carol, gw);
env.close();
env.trust(USD(1000), alice, bob, carol);
// underfund carol's offer
auto initialCarolUSD = USD(0.499);
env(pay(gw, carol, initialCarolUSD));
env(pay(gw, bob, USD(100)));
env.close();
// This offer is underfunded
env(offer(carol, drops(1), USD(1)));
env.close();
// offer at a lower quality
env(offer(bob, drops(2), USD(1), tfPassive));
env.close();
env.require(offers(bob, 1), offers(carol, 1));
// alice places an offer that crosses carol's; depending on
// "crossBothOffers" it may cross bob's as well
auto aliceTakerGets = crossBothOffers ? drops(2) : drops(1);
env(offer(alice, USD(1), aliceTakerGets));
env.close();
if (features[fixRmSmallIncreasedQOffers])
{
env.require(
offers(carol, 0),
balance(
carol,
initialCarolUSD)); // offer is removed but not taken
if (crossBothOffers)
{
env.require(
offers(alice, 0),
balance(alice, USD(1))); // alice's offer is crossed
}
else
{
env.require(
offers(alice, 1),
balance(
alice, USD(0))); // alice's offer is not crossed
}
}
else
{
env.require(
offers(alice, 1),
offers(bob, 1),
offers(carol, 1),
balance(alice, USD(0)),
balance(
carol,
initialCarolUSD)); // offer is not crossed at all
}
}
// Test payments
for (auto partialPayment : {false, true})
{
Env env{*this, features};
env.fund(XRP(10000), alice, bob, carol, gw);
env.close();
env.trust(USD(1000), alice, bob, carol);
env.close();
auto const initialCarolUSD = USD(0.999);
env(pay(gw, carol, initialCarolUSD));
env.close();
env(pay(gw, bob, USD(100)));
env.close();
env(offer(carol, drops(1), USD(1)));
env.close();
env(offer(bob, drops(2), USD(2), tfPassive));
env.close();
env.require(offers(bob, 1), offers(carol, 1));
std::uint32_t const flags = partialPayment
? (tfNoRippleDirect | tfPartialPayment)
: tfNoRippleDirect;
TER const expectedTer =
partialPayment ? TER{tesSUCCESS} : TER{tecPATH_PARTIAL};
env(pay(alice, bob, USD(5)),
path(~USD),
sendmax(XRP(1)),
txflags(flags),
ter(expectedTer));
env.close();
if (features[fixRmSmallIncreasedQOffers])
{
if (expectedTer == tesSUCCESS)
{
env.require(offers(carol, 0));
env.require(balance(
carol,
initialCarolUSD)); // offer is removed but not taken
}
else
{
// TODO: Offers are not removed when payments fail
// If that is addressed, the test should show that carol's
// offer is removed but not taken, as in the other branch of
// this if statement
}
}
else
{
if (partialPayment)
{
env.require(offers(carol, 0));
env.require(
balance(carol, USD(0))); // offer is removed and taken
}
else
{
// offer is not removed or taken
BEAST_EXPECT(isOffer(env, carol, drops(1), USD(1)));
}
}
}
}
void
testRmSmallIncreasedQOffersIOU(FeatureBitset features)
{
testcase("Rm small increased q offers IOU");
// Carol places an offer, but cannot fully fund the offer. When her
// funding is taken into account, the offer's quality drops below its
// initial quality and has an input amount of 1 drop. This is removed as
// an offer that may block offer books.
using namespace jtx;
using namespace std::chrono_literals;
auto const alice = Account{"alice"};
auto const bob = Account{"bob"};
auto const carol = Account{"carol"};
auto const gw = Account{"gw"};
auto const USD = gw["USD"];
auto const EUR = gw["EUR"];
auto tinyAmount = [&](IOU const& iou) -> PrettyAmount {
STAmount amt(
iou.issue(),
/*mantissa*/ 1,
/*exponent*/ -81);
return PrettyAmount(amt, iou.account.name());
};
// Test offer crossing
for (auto crossBothOffers : {false, true})
{
Env env{*this, features};
env.fund(XRP(10000), alice, bob, carol, gw);
env.close();
env.trust(USD(1000), alice, bob, carol);
env.trust(EUR(1000), alice, bob, carol);
// underfund carol's offer
auto initialCarolUSD = tinyAmount(USD);
env(pay(gw, carol, initialCarolUSD));
env(pay(gw, bob, USD(100)));
env(pay(gw, alice, EUR(100)));
env.close();
// This offer is underfunded
env(offer(carol, EUR(1), USD(10)));
env.close();
// offer at a lower quality
env(offer(bob, EUR(1), USD(5), tfPassive));
env.close();
env.require(offers(bob, 1), offers(carol, 1));
// alice places an offer that crosses carol's; depending on
// "crossBothOffers" it may cross bob's as well
// Whatever
auto aliceTakerGets = crossBothOffers ? EUR(0.2) : EUR(0.1);
env(offer(alice, USD(1), aliceTakerGets));
env.close();
if (features[fixRmSmallIncreasedQOffers])
{
env.require(
offers(carol, 0),
balance(
carol,
initialCarolUSD)); // offer is removed but not taken
if (crossBothOffers)
{
env.require(
offers(alice, 0),
balance(alice, USD(1))); // alice's offer is crossed
}
else
{
env.require(
offers(alice, 1),
balance(
alice, USD(0))); // alice's offer is not crossed
}
}
else
{
env.require(
offers(alice, 1),
offers(bob, 1),
offers(carol, 1),
balance(alice, USD(0)),
balance(
carol,
initialCarolUSD)); // offer is not crossed at all
}
}
// Test payments
for (auto partialPayment : {false, true})
{
Env env{*this, features};
env.fund(XRP(10000), alice, bob, carol, gw);
env.close();
env.trust(USD(1000), alice, bob, carol);
env.trust(EUR(1000), alice, bob, carol);
env.close();
// underfund carol's offer
auto const initialCarolUSD = tinyAmount(USD);
env(pay(gw, carol, initialCarolUSD));
env(pay(gw, bob, USD(100)));
env(pay(gw, alice, EUR(100)));
env.close();
// This offer is underfunded
env(offer(carol, EUR(1), USD(2)));
env.close();
env(offer(bob, EUR(2), USD(4), tfPassive));
env.close();
env.require(offers(bob, 1), offers(carol, 1));
std::uint32_t const flags = partialPayment
? (tfNoRippleDirect | tfPartialPayment)
: tfNoRippleDirect;
TER const expectedTer =
partialPayment ? TER{tesSUCCESS} : TER{tecPATH_PARTIAL};
env(pay(alice, bob, USD(5)),
path(~USD),
sendmax(EUR(10)),
txflags(flags),
ter(expectedTer));
env.close();
if (features[fixRmSmallIncreasedQOffers])
{
if (expectedTer == tesSUCCESS)
{
env.require(offers(carol, 0));
env.require(balance(
carol,
initialCarolUSD)); // offer is removed but not taken
}
else
{
// TODO: Offers are not removed when payments fail
// If that is addressed, the test should show that carol's
// offer is removed but not taken, as in the other branch of
// this if statement
}
}
else
{
if (partialPayment)
{
env.require(offers(carol, 0));
env.require(
balance(carol, USD(0))); // offer is removed and taken
}
else
{
// offer is not removed or taken
BEAST_EXPECT(isOffer(env, carol, EUR(1), USD(2)));
}
}
}
}
void
testEnforceNoRipple(FeatureBitset features)
{
testcase("Enforce No Ripple");
using namespace jtx;
auto const gw = Account{"gateway"};
auto const USD = gw["USD"];
auto const BTC = gw["BTC"];
auto const EUR = gw["EUR"];
Account const alice{"alice"};
Account const bob{"bob"};
Account const carol{"carol"};
Account const dan{"dan"};
{
// No ripple with an implied account step after an offer
Env env{*this, features};
auto const gw1 = Account{"gw1"};
auto const USD1 = gw1["USD"];
auto const gw2 = Account{"gw2"};
auto const USD2 = gw2["USD"];
env.fund(XRP(10000), alice, noripple(bob), carol, dan, gw1, gw2);
env.trust(USD1(1000), alice, carol, dan);
env(trust(bob, USD1(1000), tfSetNoRipple));
env.trust(USD2(1000), alice, carol, dan);
env(trust(bob, USD2(1000), tfSetNoRipple));
env(pay(gw1, dan, USD1(50)));
env(pay(gw1, bob, USD1(50)));
env(pay(gw2, bob, USD2(50)));
env(offer(dan, XRP(50), USD1(50)));
env(pay(alice, carol, USD2(50)),
path(~USD1, bob),
sendmax(XRP(50)),
txflags(tfNoRippleDirect),
ter(tecPATH_DRY));
}
{
// Make sure payment works with default flags
Env env{*this, features};
auto const gw1 = Account{"gw1"};
auto const USD1 = gw1["USD"];
auto const gw2 = Account{"gw2"};
auto const USD2 = gw2["USD"];
env.fund(XRP(10000), alice, bob, carol, dan, gw1, gw2);
env.trust(USD1(1000), alice, bob, carol, dan);
env.trust(USD2(1000), alice, bob, carol, dan);
env(pay(gw1, dan, USD1(50)));
env(pay(gw1, bob, USD1(50)));
env(pay(gw2, bob, USD2(50)));
env(offer(dan, XRP(50), USD1(50)));
env(pay(alice, carol, USD2(50)),
path(~USD1, bob),
sendmax(XRP(50)),
txflags(tfNoRippleDirect));
env.require(balance(alice, xrpMinusFee(env, 10000 - 50)));
env.require(balance(bob, USD1(100)));
env.require(balance(bob, USD2(0)));
env.require(balance(carol, USD2(50)));
}
}
void
testInsufficientReserve(FeatureBitset features)
{
testcase("Insufficient Reserve");
// If an account places an offer and its balance
// *before* the transaction began isn't high enough
// to meet the reserve *after* the transaction runs,
// then no offer should go on the books but if the
// offer partially or fully crossed the tx succeeds.
using namespace jtx;
auto const gw = Account{"gateway"};
auto const alice = Account{"alice"};
auto const bob = Account{"bob"};
auto const carol = Account{"carol"};
auto const USD = gw["USD"];
auto const usdOffer = USD(1000);
auto const xrpOffer = XRP(1000);
// No crossing:
{
Env env{*this, features};
env.fund(XRP(1000000), gw);
auto const f = env.current()->fees().base;
auto const r = reserve(env, 0);
env.fund(r + f, alice);
env(trust(alice, usdOffer), ter(tesSUCCESS));
env(pay(gw, alice, usdOffer), ter(tesSUCCESS));
env(offer(alice, xrpOffer, usdOffer), ter(tecINSUF_RESERVE_OFFER));
env.require(balance(alice, r - f), owners(alice, 1));
}
// Partial cross:
{
Env env{*this, features};
env.fund(XRP(1000000), gw);
auto const f = env.current()->fees().base;
auto const r = reserve(env, 0);
auto const usdOffer2 = USD(500);
auto const xrpOffer2 = XRP(500);
env.fund(r + f + xrpOffer, bob);
env(offer(bob, usdOffer2, xrpOffer2), ter(tesSUCCESS));
env.fund(r + f, alice);
env(trust(alice, usdOffer), ter(tesSUCCESS));
env(pay(gw, alice, usdOffer), ter(tesSUCCESS));
env(offer(alice, xrpOffer, usdOffer), ter(tesSUCCESS));
env.require(
balance(alice, r - f + xrpOffer2),
balance(alice, usdOffer2),
owners(alice, 1),
balance(bob, r + xrpOffer2),
balance(bob, usdOffer2),
owners(bob, 1));
}
// Account has enough reserve as is, but not enough
// if an offer were added. Attempt to sell IOUs to
// buy XRP. If it fully crosses, we succeed.
{
Env env{*this, features};
env.fund(XRP(1000000), gw);
auto const f = env.current()->fees().base;
auto const r = reserve(env, 0);
auto const usdOffer2 = USD(500);
auto const xrpOffer2 = XRP(500);
env.fund(r + f + xrpOffer, bob, carol);
env(offer(bob, usdOffer2, xrpOffer2), ter(tesSUCCESS));
env(offer(carol, usdOffer, xrpOffer), ter(tesSUCCESS));
env.fund(r + f, alice);
env(trust(alice, usdOffer), ter(tesSUCCESS));
env(pay(gw, alice, usdOffer), ter(tesSUCCESS));
env(offer(alice, xrpOffer, usdOffer), ter(tesSUCCESS));
env.require(
balance(alice, r - f + xrpOffer),
balance(alice, USD(0)),
owners(alice, 1),
balance(bob, r + xrpOffer2),
balance(bob, usdOffer2),
owners(bob, 1),
balance(carol, r + xrpOffer2),
balance(carol, usdOffer2),
owners(carol, 2));
}
}
// Helper function that returns the Offers on an account.
static std::vector<std::shared_ptr<SLE const>>
offersOnAccount(jtx::Env& env, jtx::Account account)
{
std::vector<std::shared_ptr<SLE const>> result;
forEachItem(
*env.current(),
account,
[&result](std::shared_ptr<SLE const> const& sle) {
if (sle->getType() == ltOFFER)
result.push_back(sle);
});
return result;
}
void
testFillModes(FeatureBitset features)
{
testcase("Fill Modes");
using namespace jtx;
auto const startBalance = XRP(1000000);
auto const gw = Account{"gateway"};
auto const alice = Account{"alice"};
auto const bob = Account{"bob"};
auto const USD = gw["USD"];
// Fill or Kill - unless we fully cross, just charge a fee and don't
// place the offer on the books. But also clean up expired offers
// that are discovered along the way.
//
// fix1578 changes the return code. Verify expected behavior
// without and with fix1578.
for (auto const& tweakedFeatures :
{features - fix1578, features | fix1578})
{
Env env{*this, tweakedFeatures};
auto const f = env.current()->fees().base;
env.fund(startBalance, gw, alice, bob);
// bob creates an offer that expires before the next ledger close.
env(offer(bob, USD(500), XRP(500)),
json(sfExpiration.fieldName, lastClose(env) + 1),
ter(tesSUCCESS));
// The offer expires (it's not removed yet).
env.close();
env.require(owners(bob, 1), offers(bob, 1));
// bob creates the offer that will be crossed.
env(offer(bob, USD(500), XRP(500)), ter(tesSUCCESS));
env.close();
env.require(owners(bob, 2), offers(bob, 2));
env(trust(alice, USD(1000)), ter(tesSUCCESS));
env(pay(gw, alice, USD(1000)), ter(tesSUCCESS));
// Order that can't be filled but will remove bob's expired offer:
{
TER const killedCode{
tweakedFeatures[fix1578] ? TER{tecKILLED}
: TER{tesSUCCESS}};
env(offer(alice, XRP(1000), USD(1000)),
txflags(tfFillOrKill),
ter(killedCode));
}
env.require(
balance(alice, startBalance - (f * 2)),
balance(alice, USD(1000)),
owners(alice, 1),
offers(alice, 0),
balance(bob, startBalance - (f * 2)),
balance(bob, USD(none)),
owners(bob, 1),
offers(bob, 1));
// Order that can be filled
env(offer(alice, XRP(500), USD(500)),
txflags(tfFillOrKill),
ter(tesSUCCESS));
env.require(
balance(alice, startBalance - (f * 3) + XRP(500)),
balance(alice, USD(500)),
owners(alice, 1),
offers(alice, 0),
balance(bob, startBalance - (f * 2) - XRP(500)),
balance(bob, USD(500)),
owners(bob, 1),
offers(bob, 0));
}
// Immediate or Cancel - cross as much as possible
// and add nothing on the books:
{
Env env{*this, features};
auto const f = env.current()->fees().base;
env.fund(startBalance, gw, alice, bob);
env(trust(alice, USD(1000)), ter(tesSUCCESS));
env(pay(gw, alice, USD(1000)), ter(tesSUCCESS));
// No cross:
{
TER const expectedCode = features[featureImmediateOfferKilled]
? static_cast<TER>(tecKILLED)
: static_cast<TER>(tesSUCCESS);
env(offer(alice, XRP(1000), USD(1000)),
txflags(tfImmediateOrCancel),
ter(expectedCode));
}
env.require(
balance(alice, startBalance - f - f),
balance(alice, USD(1000)),
owners(alice, 1),
offers(alice, 0));
// Partially cross:
env(offer(bob, USD(50), XRP(50)), ter(tesSUCCESS));
env(offer(alice, XRP(1000), USD(1000)),
txflags(tfImmediateOrCancel),
ter(tesSUCCESS));
env.require(
balance(alice, startBalance - f - f - f + XRP(50)),
balance(alice, USD(950)),
owners(alice, 1),
offers(alice, 0),
balance(bob, startBalance - f - XRP(50)),
balance(bob, USD(50)),
owners(bob, 1),
offers(bob, 0));
// Fully cross:
env(offer(bob, USD(50), XRP(50)), ter(tesSUCCESS));
env(offer(alice, XRP(50), USD(50)),
txflags(tfImmediateOrCancel),
ter(tesSUCCESS));
env.require(
balance(alice, startBalance - f - f - f - f + XRP(100)),
balance(alice, USD(900)),
owners(alice, 1),
offers(alice, 0),
balance(bob, startBalance - f - f - XRP(100)),
balance(bob, USD(100)),
owners(bob, 1),
offers(bob, 0));
}
// tfPassive -- place the offer without crossing it.
{
Env env(*this, features);
env.fund(startBalance, gw, alice, bob);
env.close();
env(trust(bob, USD(1000)));
env.close();
env(pay(gw, bob, USD(1000)));
env.close();
env(offer(alice, USD(1000), XRP(2000)));
env.close();
auto const aliceOffers = offersOnAccount(env, alice);
BEAST_EXPECT(aliceOffers.size() == 1);
for (auto offerPtr : aliceOffers)
{
auto const& offer = *offerPtr;
BEAST_EXPECT(offer[sfTakerGets] == XRP(2000));
BEAST_EXPECT(offer[sfTakerPays] == USD(1000));
}
// bob creates a passive offer that could cross alice's.
// bob's offer should stay in the ledger.
env(offer(bob, XRP(2000), USD(1000), tfPassive));
env.close();
env.require(offers(alice, 1));
auto const bobOffers = offersOnAccount(env, bob);
BEAST_EXPECT(bobOffers.size() == 1);
for (auto offerPtr : bobOffers)
{
auto const& offer = *offerPtr;