-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Escrow_test.cpp
1680 lines (1439 loc) · 61.2 KB
/
Escrow_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, 2013 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 <xrpld/app/tx/applySteps.h>
#include <xrpld/ledger/Dir.h>
#include <xrpl/protocol/Feature.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/TxFlags.h>
#include <xrpl/protocol/jss.h>
#include <algorithm>
#include <iterator>
namespace ripple {
namespace test {
struct Escrow_test : public beast::unit_test::suite
{
// A PreimageSha256 fulfillments and its associated condition.
std::array<std::uint8_t, 4> const fb1 = {{0xA0, 0x02, 0x80, 0x00}};
std::array<std::uint8_t, 39> const cb1 = {
{0xA0, 0x25, 0x80, 0x20, 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC,
0x1C, 0x14, 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, 0xA4, 0x95,
0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55, 0x81, 0x01, 0x00}};
// Another PreimageSha256 fulfillments and its associated condition.
std::array<std::uint8_t, 7> const fb2 = {
{0xA0, 0x05, 0x80, 0x03, 0x61, 0x61, 0x61}};
std::array<std::uint8_t, 39> const cb2 = {
{0xA0, 0x25, 0x80, 0x20, 0x98, 0x34, 0x87, 0x6D, 0xCF, 0xB0,
0x5C, 0xB1, 0x67, 0xA5, 0xC2, 0x49, 0x53, 0xEB, 0xA5, 0x8C,
0x4A, 0xC8, 0x9B, 0x1A, 0xDF, 0x57, 0xF2, 0x8F, 0x2F, 0x9D,
0x09, 0xAF, 0x10, 0x7E, 0xE8, 0xF0, 0x81, 0x01, 0x03}};
// Another PreimageSha256 fulfillment and its associated condition.
std::array<std::uint8_t, 8> const fb3 = {
{0xA0, 0x06, 0x80, 0x04, 0x6E, 0x69, 0x6B, 0x62}};
std::array<std::uint8_t, 39> const cb3 = {
{0xA0, 0x25, 0x80, 0x20, 0x6E, 0x4C, 0x71, 0x45, 0x30, 0xC0,
0xA4, 0x26, 0x8B, 0x3F, 0xA6, 0x3B, 0x1B, 0x60, 0x6F, 0x2D,
0x26, 0x4A, 0x2D, 0x85, 0x7B, 0xE8, 0xA0, 0x9C, 0x1D, 0xFD,
0x57, 0x0D, 0x15, 0x85, 0x8B, 0xD4, 0x81, 0x01, 0x04}};
void
testEnablement()
{
testcase("Enablement");
using namespace jtx;
using namespace std::chrono;
Env env(*this);
env.fund(XRP(5000), "alice", "bob");
env(escrow("alice", "bob", XRP(1000)), finish_time(env.now() + 1s));
env.close();
auto const seq1 = env.seq("alice");
env(escrow("alice", "bob", XRP(1000)),
condition(cb1),
finish_time(env.now() + 1s),
fee(1500));
env.close();
env(finish("bob", "alice", seq1),
condition(cb1),
fulfillment(fb1),
fee(1500));
auto const seq2 = env.seq("alice");
env(escrow("alice", "bob", XRP(1000)),
condition(cb2),
finish_time(env.now() + 1s),
cancel_time(env.now() + 2s),
fee(1500));
env.close();
env(cancel("bob", "alice", seq2), fee(1500));
}
void
testTiming()
{
using namespace jtx;
using namespace std::chrono;
{
testcase("Timing: Finish Only");
Env env(*this);
env.fund(XRP(5000), "alice", "bob");
env.close();
// We create an escrow that can be finished in the future
auto const ts = env.now() + 97s;
auto const seq = env.seq("alice");
env(escrow("alice", "bob", XRP(1000)), finish_time(ts));
// Advance the ledger, verifying that the finish won't complete
// prematurely.
for (; env.now() < ts; env.close())
env(finish("bob", "alice", seq),
fee(1500),
ter(tecNO_PERMISSION));
env(finish("bob", "alice", seq), fee(1500));
}
{
testcase("Timing: Cancel Only");
Env env(*this);
env.fund(XRP(5000), "alice", "bob");
env.close();
// We create an escrow that can be cancelled in the future
auto const ts = env.now() + 117s;
auto const seq = env.seq("alice");
env(escrow("alice", "bob", XRP(1000)),
condition(cb1),
cancel_time(ts));
// Advance the ledger, verifying that the cancel won't complete
// prematurely.
for (; env.now() < ts; env.close())
env(cancel("bob", "alice", seq),
fee(1500),
ter(tecNO_PERMISSION));
// Verify that a finish won't work anymore.
env(finish("bob", "alice", seq),
condition(cb1),
fulfillment(fb1),
fee(1500),
ter(tecNO_PERMISSION));
// Verify that the cancel will succeed
env(cancel("bob", "alice", seq), fee(1500));
}
{
testcase("Timing: Finish and Cancel -> Finish");
Env env(*this);
env.fund(XRP(5000), "alice", "bob");
env.close();
// We create an escrow that can be cancelled in the future
auto const fts = env.now() + 117s;
auto const cts = env.now() + 192s;
auto const seq = env.seq("alice");
env(escrow("alice", "bob", XRP(1000)),
finish_time(fts),
cancel_time(cts));
// Advance the ledger, verifying that the finish and cancel won't
// complete prematurely.
for (; env.now() < fts; env.close())
{
env(finish("bob", "alice", seq),
fee(1500),
ter(tecNO_PERMISSION));
env(cancel("bob", "alice", seq),
fee(1500),
ter(tecNO_PERMISSION));
}
// Verify that a cancel still won't work
env(cancel("bob", "alice", seq), fee(1500), ter(tecNO_PERMISSION));
// And verify that a finish will
env(finish("bob", "alice", seq), fee(1500));
}
{
testcase("Timing: Finish and Cancel -> Cancel");
Env env(*this);
env.fund(XRP(5000), "alice", "bob");
env.close();
// We create an escrow that can be cancelled in the future
auto const fts = env.now() + 109s;
auto const cts = env.now() + 184s;
auto const seq = env.seq("alice");
env(escrow("alice", "bob", XRP(1000)),
finish_time(fts),
cancel_time(cts));
// Advance the ledger, verifying that the finish and cancel won't
// complete prematurely.
for (; env.now() < fts; env.close())
{
env(finish("bob", "alice", seq),
fee(1500),
ter(tecNO_PERMISSION));
env(cancel("bob", "alice", seq),
fee(1500),
ter(tecNO_PERMISSION));
}
// Continue advancing, verifying that the cancel won't complete
// prematurely. At this point a finish would succeed.
for (; env.now() < cts; env.close())
env(cancel("bob", "alice", seq),
fee(1500),
ter(tecNO_PERMISSION));
// Verify that finish will no longer work, since we are past the
// cancel activation time.
env(finish("bob", "alice", seq), fee(1500), ter(tecNO_PERMISSION));
// And verify that a cancel will succeed.
env(cancel("bob", "alice", seq), fee(1500));
}
}
void
testTags()
{
testcase("Tags");
using namespace jtx;
using namespace std::chrono;
Env env(*this);
auto const alice = Account("alice");
auto const bob = Account("bob");
env.fund(XRP(5000), alice, bob);
// Check to make sure that we correctly detect if tags are really
// required:
env(fset(bob, asfRequireDest));
env(escrow(alice, bob, XRP(1000)),
finish_time(env.now() + 1s),
ter(tecDST_TAG_NEEDED));
// set source and dest tags
auto const seq = env.seq(alice);
env(escrow(alice, bob, XRP(1000)),
finish_time(env.now() + 1s),
stag(1),
dtag(2));
auto const sle = env.le(keylet::escrow(alice.id(), seq));
BEAST_EXPECT(sle);
BEAST_EXPECT((*sle)[sfSourceTag] == 1);
BEAST_EXPECT((*sle)[sfDestinationTag] == 2);
}
void
testDisallowXRP()
{
testcase("Disallow XRP");
using namespace jtx;
using namespace std::chrono;
{
// Respect the "asfDisallowXRP" account flag:
Env env(*this, supported_amendments() - featureDepositAuth);
env.fund(XRP(5000), "bob", "george");
env(fset("george", asfDisallowXRP));
env(escrow("bob", "george", XRP(10)),
finish_time(env.now() + 1s),
ter(tecNO_TARGET));
}
{
// Ignore the "asfDisallowXRP" account flag, which we should
// have been doing before.
Env env(*this);
env.fund(XRP(5000), "bob", "george");
env(fset("george", asfDisallowXRP));
env(escrow("bob", "george", XRP(10)), finish_time(env.now() + 1s));
}
}
void
test1571()
{
using namespace jtx;
using namespace std::chrono;
{
testcase("Implied Finish Time (without fix1571)");
Env env(*this, supported_amendments() - fix1571);
env.fund(XRP(5000), "alice", "bob", "carol");
env.close();
// Creating an escrow without a finish time and finishing it
// is allowed without fix1571:
auto const seq1 = env.seq("alice");
env(escrow("alice", "bob", XRP(100)),
cancel_time(env.now() + 1s),
fee(1500));
env.close();
env(finish("carol", "alice", seq1), fee(1500));
BEAST_EXPECT(env.balance("bob") == XRP(5100));
env.close();
// Creating an escrow without a finish time and a condition is
// also allowed without fix1571:
auto const seq2 = env.seq("alice");
env(escrow("alice", "bob", XRP(100)),
cancel_time(env.now() + 1s),
condition(cb1),
fee(1500));
env.close();
env(finish("carol", "alice", seq2),
condition(cb1),
fulfillment(fb1),
fee(1500));
BEAST_EXPECT(env.balance("bob") == XRP(5200));
}
{
testcase("Implied Finish Time (with fix1571)");
Env env(*this);
env.fund(XRP(5000), "alice", "bob", "carol");
env.close();
// Creating an escrow with only a cancel time is not allowed:
env(escrow("alice", "bob", XRP(100)),
cancel_time(env.now() + 90s),
fee(1500),
ter(temMALFORMED));
// Creating an escrow with only a cancel time and a condition is
// allowed:
auto const seq = env.seq("alice");
env(escrow("alice", "bob", XRP(100)),
cancel_time(env.now() + 90s),
condition(cb1),
fee(1500));
env.close();
env(finish("carol", "alice", seq),
condition(cb1),
fulfillment(fb1),
fee(1500));
BEAST_EXPECT(env.balance("bob") == XRP(5100));
}
}
void
testFails()
{
testcase("Failure Cases");
using namespace jtx;
using namespace std::chrono;
Env env(*this);
env.fund(XRP(5000), "alice", "bob");
env.close();
// Finish time is in the past
env(escrow("alice", "bob", XRP(1000)),
finish_time(env.now() - 5s),
ter(tecNO_PERMISSION));
// Cancel time is in the past
env(escrow("alice", "bob", XRP(1000)),
condition(cb1),
cancel_time(env.now() - 5s),
ter(tecNO_PERMISSION));
// no destination account
env(escrow("alice", "carol", XRP(1000)),
finish_time(env.now() + 1s),
ter(tecNO_DST));
env.fund(XRP(5000), "carol");
// Using non-XRP:
env(escrow("alice", "carol", Account("alice")["USD"](500)),
finish_time(env.now() + 1s),
ter(temBAD_AMOUNT));
// Sending zero or no XRP:
env(escrow("alice", "carol", XRP(0)),
finish_time(env.now() + 1s),
ter(temBAD_AMOUNT));
env(escrow("alice", "carol", XRP(-1000)),
finish_time(env.now() + 1s),
ter(temBAD_AMOUNT));
// Fail if neither CancelAfter nor FinishAfter are specified:
env(escrow("alice", "carol", XRP(1)), ter(temBAD_EXPIRATION));
// Fail if neither a FinishTime nor a condition are attached:
env(escrow("alice", "carol", XRP(1)),
cancel_time(env.now() + 1s),
ter(temMALFORMED));
// Fail if FinishAfter has already passed:
env(escrow("alice", "carol", XRP(1)),
finish_time(env.now() - 1s),
ter(tecNO_PERMISSION));
// If both CancelAfter and FinishAfter are set, then CancelAfter must
// be strictly later than FinishAfter.
env(escrow("alice", "carol", XRP(1)),
condition(cb1),
finish_time(env.now() + 10s),
cancel_time(env.now() + 10s),
ter(temBAD_EXPIRATION));
env(escrow("alice", "carol", XRP(1)),
condition(cb1),
finish_time(env.now() + 10s),
cancel_time(env.now() + 5s),
ter(temBAD_EXPIRATION));
// Carol now requires the use of a destination tag
env(fset("carol", asfRequireDest));
// missing destination tag
env(escrow("alice", "carol", XRP(1)),
condition(cb1),
cancel_time(env.now() + 1s),
ter(tecDST_TAG_NEEDED));
// Success!
env(escrow("alice", "carol", XRP(1)),
condition(cb1),
cancel_time(env.now() + 1s),
dtag(1));
{ // Fail if the sender wants to send more than he has:
auto const accountReserve = drops(env.current()->fees().reserve);
auto const accountIncrement =
drops(env.current()->fees().increment);
env.fund(accountReserve + accountIncrement + XRP(50), "daniel");
env(escrow("daniel", "bob", XRP(51)),
finish_time(env.now() + 1s),
ter(tecUNFUNDED));
env.fund(accountReserve + accountIncrement + XRP(50), "evan");
env(escrow("evan", "bob", XRP(50)),
finish_time(env.now() + 1s),
ter(tecUNFUNDED));
env.fund(accountReserve, "frank");
env(escrow("frank", "bob", XRP(1)),
finish_time(env.now() + 1s),
ter(tecINSUFFICIENT_RESERVE));
}
{ // Specify incorrect sequence number
env.fund(XRP(5000), "hannah");
auto const seq = env.seq("hannah");
env(escrow("hannah", "hannah", XRP(10)),
finish_time(env.now() + 1s),
fee(1500));
env.close();
env(finish("hannah", "hannah", seq + 7),
fee(1500),
ter(tecNO_TARGET));
}
{ // Try to specify a condition for a non-conditional payment
env.fund(XRP(5000), "ivan");
auto const seq = env.seq("ivan");
env(escrow("ivan", "ivan", XRP(10)), finish_time(env.now() + 1s));
env.close();
env(finish("ivan", "ivan", seq),
condition(cb1),
fulfillment(fb1),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
}
}
void
testLockup()
{
testcase("Lockup");
using namespace jtx;
using namespace std::chrono;
{
// Unconditional
Env env(*this);
env.fund(XRP(5000), "alice", "bob");
auto const seq = env.seq("alice");
env(escrow("alice", "alice", XRP(1000)),
finish_time(env.now() + 5s));
env.require(balance("alice", XRP(4000) - drops(10)));
// Not enough time has elapsed for a finish and canceling isn't
// possible.
env(cancel("bob", "alice", seq), ter(tecNO_PERMISSION));
env(finish("bob", "alice", seq), ter(tecNO_PERMISSION));
env.close();
// Cancel continues to not be possible
env(cancel("bob", "alice", seq), ter(tecNO_PERMISSION));
// Finish should succeed. Verify funds.
env(finish("bob", "alice", seq));
env.require(balance("alice", XRP(5000) - drops(10)));
}
{
// Unconditionally pay from Alice to Bob. Zelda (neither source nor
// destination) signs all cancels and finishes. This shows that
// Escrow will make a payment to Bob with no intervention from Bob.
Env env(*this);
env.fund(XRP(5000), "alice", "bob", "zelda");
auto const seq = env.seq("alice");
env(escrow("alice", "bob", XRP(1000)), finish_time(env.now() + 5s));
env.require(balance("alice", XRP(4000) - drops(10)));
// Not enough time has elapsed for a finish and canceling isn't
// possible.
env(cancel("zelda", "alice", seq), ter(tecNO_PERMISSION));
env(finish("zelda", "alice", seq), ter(tecNO_PERMISSION));
env.close();
// Cancel continues to not be possible
env(cancel("zelda", "alice", seq), ter(tecNO_PERMISSION));
// Finish should succeed. Verify funds.
env(finish("zelda", "alice", seq));
env.close();
env.require(balance("alice", XRP(4000) - drops(10)));
env.require(balance("bob", XRP(6000)));
env.require(balance("zelda", XRP(5000) - drops(40)));
}
{
// Bob sets DepositAuth so only Bob can finish the escrow.
Env env(*this);
env.fund(XRP(5000), "alice", "bob", "zelda");
env(fset("bob", asfDepositAuth));
env.close();
auto const seq = env.seq("alice");
env(escrow("alice", "bob", XRP(1000)), finish_time(env.now() + 5s));
env.require(balance("alice", XRP(4000) - drops(10)));
// Not enough time has elapsed for a finish and canceling isn't
// possible.
env(cancel("zelda", "alice", seq), ter(tecNO_PERMISSION));
env(cancel("alice", "alice", seq), ter(tecNO_PERMISSION));
env(cancel("bob", "alice", seq), ter(tecNO_PERMISSION));
env(finish("zelda", "alice", seq), ter(tecNO_PERMISSION));
env(finish("alice", "alice", seq), ter(tecNO_PERMISSION));
env(finish("bob", "alice", seq), ter(tecNO_PERMISSION));
env.close();
// Cancel continues to not be possible. Finish will only succeed for
// Bob, because of DepositAuth.
env(cancel("zelda", "alice", seq), ter(tecNO_PERMISSION));
env(cancel("alice", "alice", seq), ter(tecNO_PERMISSION));
env(cancel("bob", "alice", seq), ter(tecNO_PERMISSION));
env(finish("zelda", "alice", seq), ter(tecNO_PERMISSION));
env(finish("alice", "alice", seq), ter(tecNO_PERMISSION));
env(finish("bob", "alice", seq));
env.close();
auto const baseFee = env.current()->fees().base;
env.require(balance("alice", XRP(4000) - (baseFee * 5)));
env.require(balance("bob", XRP(6000) - (baseFee * 5)));
env.require(balance("zelda", XRP(5000) - (baseFee * 4)));
}
{
// Bob sets DepositAuth but preauthorizes Zelda, so Zelda can
// finish the escrow.
Env env(*this);
env.fund(XRP(5000), "alice", "bob", "zelda");
env(fset("bob", asfDepositAuth));
env.close();
env(deposit::auth("bob", "zelda"));
env.close();
auto const seq = env.seq("alice");
env(escrow("alice", "bob", XRP(1000)), finish_time(env.now() + 5s));
env.require(balance("alice", XRP(4000) - drops(10)));
env.close();
// DepositPreauth allows Finish to succeed for either Zelda or
// Bob. But Finish won't succeed for Alice since she is not
// preauthorized.
env(finish("alice", "alice", seq), ter(tecNO_PERMISSION));
env(finish("zelda", "alice", seq));
env.close();
auto const baseFee = env.current()->fees().base;
env.require(balance("alice", XRP(4000) - (baseFee * 2)));
env.require(balance("bob", XRP(6000) - (baseFee * 2)));
env.require(balance("zelda", XRP(5000) - (baseFee * 1)));
}
{
// Conditional
Env env(*this);
env.fund(XRP(5000), "alice", "bob");
auto const seq = env.seq("alice");
env(escrow("alice", "alice", XRP(1000)),
condition(cb2),
finish_time(env.now() + 5s));
env.require(balance("alice", XRP(4000) - drops(10)));
// Not enough time has elapsed for a finish and canceling isn't
// possible.
env(cancel("alice", "alice", seq), ter(tecNO_PERMISSION));
env(cancel("bob", "alice", seq), ter(tecNO_PERMISSION));
env(finish("alice", "alice", seq), ter(tecNO_PERMISSION));
env(finish("alice", "alice", seq),
condition(cb2),
fulfillment(fb2),
fee(1500),
ter(tecNO_PERMISSION));
env(finish("bob", "alice", seq), ter(tecNO_PERMISSION));
env(finish("bob", "alice", seq),
condition(cb2),
fulfillment(fb2),
fee(1500),
ter(tecNO_PERMISSION));
env.close();
// Cancel continues to not be possible. Finish is possible but
// requires the fulfillment associated with the escrow.
env(cancel("alice", "alice", seq), ter(tecNO_PERMISSION));
env(cancel("bob", "alice", seq), ter(tecNO_PERMISSION));
env(finish("bob", "alice", seq), ter(tecCRYPTOCONDITION_ERROR));
env(finish("alice", "alice", seq), ter(tecCRYPTOCONDITION_ERROR));
env.close();
env(finish("bob", "alice", seq),
condition(cb2),
fulfillment(fb2),
fee(1500));
}
{
// Self-escrowed conditional with DepositAuth.
Env env(*this);
env.fund(XRP(5000), "alice", "bob");
auto const seq = env.seq("alice");
env(escrow("alice", "alice", XRP(1000)),
condition(cb3),
finish_time(env.now() + 5s));
env.require(balance("alice", XRP(4000) - drops(10)));
env.close();
// Finish is now possible but requires the cryptocondition.
env(finish("bob", "alice", seq), ter(tecCRYPTOCONDITION_ERROR));
env(finish("alice", "alice", seq), ter(tecCRYPTOCONDITION_ERROR));
// Enable deposit authorization. After this only Alice can finish
// the escrow.
env(fset("alice", asfDepositAuth));
env.close();
env(finish("alice", "alice", seq),
condition(cb2),
fulfillment(fb2),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
env(finish("bob", "alice", seq),
condition(cb3),
fulfillment(fb3),
fee(1500),
ter(tecNO_PERMISSION));
env(finish("alice", "alice", seq),
condition(cb3),
fulfillment(fb3),
fee(1500));
}
{
// Self-escrowed conditional with DepositAuth and DepositPreauth.
Env env(*this);
env.fund(XRP(5000), "alice", "bob", "zelda");
auto const seq = env.seq("alice");
env(escrow("alice", "alice", XRP(1000)),
condition(cb3),
finish_time(env.now() + 5s));
env.require(balance("alice", XRP(4000) - drops(10)));
env.close();
// Alice preauthorizes Zelda for deposit, even though Alice has not
// set the lsfDepositAuth flag (yet).
env(deposit::auth("alice", "zelda"));
env.close();
// Finish is now possible but requires the cryptocondition.
env(finish("alice", "alice", seq), ter(tecCRYPTOCONDITION_ERROR));
env(finish("bob", "alice", seq), ter(tecCRYPTOCONDITION_ERROR));
env(finish("zelda", "alice", seq), ter(tecCRYPTOCONDITION_ERROR));
// Alice enables deposit authorization. After this only Alice or
// Zelda (because Zelda is preauthorized) can finish the escrow.
env(fset("alice", asfDepositAuth));
env.close();
env(finish("alice", "alice", seq),
condition(cb2),
fulfillment(fb2),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
env(finish("bob", "alice", seq),
condition(cb3),
fulfillment(fb3),
fee(1500),
ter(tecNO_PERMISSION));
env(finish("zelda", "alice", seq),
condition(cb3),
fulfillment(fb3),
fee(1500));
}
}
void
testEscrowConditions()
{
testcase("Escrow with CryptoConditions");
using namespace jtx;
using namespace std::chrono;
{ // Test cryptoconditions
Env env(*this);
env.fund(XRP(5000), "alice", "bob", "carol");
auto const seq = env.seq("alice");
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 0);
env(escrow("alice", "carol", XRP(1000)),
condition(cb1),
cancel_time(env.now() + 1s));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
env.require(balance("alice", XRP(4000) - drops(10)));
env.require(balance("carol", XRP(5000)));
env(cancel("bob", "alice", seq), ter(tecNO_PERMISSION));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
// Attempt to finish without a fulfillment
env(finish("bob", "alice", seq), ter(tecCRYPTOCONDITION_ERROR));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
// Attempt to finish with a condition instead of a fulfillment
env(finish("bob", "alice", seq),
condition(cb1),
fulfillment(cb1),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
env(finish("bob", "alice", seq),
condition(cb1),
fulfillment(cb2),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
env(finish("bob", "alice", seq),
condition(cb1),
fulfillment(cb3),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
// Attempt to finish with an incorrect condition and various
// combinations of correct and incorrect fulfillments.
env(finish("bob", "alice", seq),
condition(cb2),
fulfillment(fb1),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
env(finish("bob", "alice", seq),
condition(cb2),
fulfillment(fb2),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
env(finish("bob", "alice", seq),
condition(cb2),
fulfillment(fb3),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
// Attempt to finish with the correct condition & fulfillment
env(finish("bob", "alice", seq),
condition(cb1),
fulfillment(fb1),
fee(1500));
// SLE removed on finish
BEAST_EXPECT(!env.le(keylet::escrow(Account("alice").id(), seq)));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 0);
env.require(balance("carol", XRP(6000)));
env(cancel("bob", "alice", seq), ter(tecNO_TARGET));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 0);
env(cancel("bob", "carol", 1), ter(tecNO_TARGET));
}
{ // Test cancel when condition is present
Env env(*this);
env.fund(XRP(5000), "alice", "bob", "carol");
auto const seq = env.seq("alice");
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 0);
env(escrow("alice", "carol", XRP(1000)),
condition(cb2),
cancel_time(env.now() + 1s));
env.close();
env.require(balance("alice", XRP(4000) - drops(10)));
// balance restored on cancel
env(cancel("bob", "alice", seq));
env.require(balance("alice", XRP(5000) - drops(10)));
// SLE removed on cancel
BEAST_EXPECT(!env.le(keylet::escrow(Account("alice").id(), seq)));
}
{
Env env(*this);
env.fund(XRP(5000), "alice", "bob", "carol");
env.close();
auto const seq = env.seq("alice");
env(escrow("alice", "carol", XRP(1000)),
condition(cb3),
cancel_time(env.now() + 1s));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
// cancel fails before expiration
env(cancel("bob", "alice", seq), ter(tecNO_PERMISSION));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
env.close();
// finish fails after expiration
env(finish("bob", "alice", seq),
condition(cb3),
fulfillment(fb3),
fee(1500),
ter(tecNO_PERMISSION));
BEAST_EXPECT((*env.le("alice"))[sfOwnerCount] == 1);
env.require(balance("carol", XRP(5000)));
}
{ // Test long & short conditions during creation
Env env(*this);
env.fund(XRP(5000), "alice", "bob", "carol");
std::vector<std::uint8_t> v;
v.resize(cb1.size() + 2, 0x78);
std::memcpy(v.data() + 1, cb1.data(), cb1.size());
auto const p = v.data();
auto const s = v.size();
auto const ts = env.now() + 1s;
// All these are expected to fail, because the
// condition we pass in is malformed in some way
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{p, s}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{p, s - 1}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{p, s - 2}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{p + 1, s - 1}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{p + 1, s - 3}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{p + 2, s - 2}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{p + 2, s - 3}),
cancel_time(ts),
ter(temMALFORMED));
auto const seq = env.seq("alice");
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{p + 1, s - 2}),
cancel_time(ts),
fee(100));
env(finish("bob", "alice", seq),
condition(cb1),
fulfillment(fb1),
fee(1500));
env.require(balance("alice", XRP(4000) - drops(100)));
env.require(balance("bob", XRP(5000) - drops(1500)));
env.require(balance("carol", XRP(6000)));
}
{ // Test long and short conditions & fulfillments during finish
Env env(*this);
env.fund(XRP(5000), "alice", "bob", "carol");
std::vector<std::uint8_t> cv;
cv.resize(cb2.size() + 2, 0x78);
std::memcpy(cv.data() + 1, cb2.data(), cb2.size());
auto const cp = cv.data();
auto const cs = cv.size();
std::vector<std::uint8_t> fv;
fv.resize(fb2.size() + 2, 0x13);
std::memcpy(fv.data() + 1, fb2.data(), fb2.size());
auto const fp = fv.data();
auto const fs = fv.size();
auto const ts = env.now() + 1s;
// All these are expected to fail, because the
// condition we pass in is malformed in some way
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{cp, cs}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{cp, cs - 1}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{cp, cs - 2}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{cp + 1, cs - 1}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{cp + 1, cs - 3}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{cp + 2, cs - 2}),
cancel_time(ts),
ter(temMALFORMED));
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{cp + 2, cs - 3}),
cancel_time(ts),
ter(temMALFORMED));
auto const seq = env.seq("alice");
env(escrow("alice", "carol", XRP(1000)),
condition(Slice{cp + 1, cs - 2}),
cancel_time(ts),
fee(100));
// Now, try to fulfill using the same sequence of
// malformed conditions.
env(finish("bob", "alice", seq),
condition(Slice{cp, cs}),
fulfillment(Slice{fp, fs}),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
env(finish("bob", "alice", seq),
condition(Slice{cp, cs - 1}),
fulfillment(Slice{fp, fs}),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
env(finish("bob", "alice", seq),
condition(Slice{cp, cs - 2}),
fulfillment(Slice{fp, fs}),
fee(1500),
ter(tecCRYPTOCONDITION_ERROR));
env(finish("bob", "alice", seq),
condition(Slice{cp + 1, cs - 1}),
fulfillment(Slice{fp, fs}),
fee(1500),