forked from Liquifact/Liquifact-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperties.rs
More file actions
2791 lines (2450 loc) · 91.8 KB
/
Copy pathproperties.rs
File metadata and controls
2791 lines (2450 loc) · 91.8 KB
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
use super::*;
use proptest::prelude::*;
use std::collections::BTreeSet;
proptest! {
#[test]
fn prop_funded_amount_non_decreasing(
amount1 in 1i128..50_000_000_000i128,
amount2 in 1i128..50_000_000_000i128,
) {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor1 = Address::generate(&env);
let investor2 = Address::generate(&env);
let client = deploy(&env);
let target = 200_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "INVTST"),
&sme,
&target,
&800i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>);
let before = client.get_escrow().funded_amount;
client.fund(&investor1, &amount1);
let after1 = client.get_escrow().funded_amount;
prop_assert!(after1 >= before, "funded_amount must be non-decreasing");
if client.get_escrow().status == 0 {
client.fund(&investor2, &amount2);
let after2 = client.get_escrow().funded_amount;
prop_assert!(after2 >= after1, "funded_amount must be non-decreasing on successive funds");
}
}
#[test]
fn prop_status_only_increases(
amount in 1i128..100_000_000_000i128,
target in 1i128..100_000_000_000i128,
) {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let client = deploy(&env);
let escrow = client.init(
&admin,
&soroban_sdk::String::from_str(&env, "INVSTA"),
&sme,
&target,
&800i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>);
prop_assert_eq!(escrow.status, 0);
let after_fund = client.fund(&investor, &amount);
prop_assert!(after_fund.status >= escrow.status, "status must not decrease");
prop_assert!(after_fund.status <= 3, "status must be in valid range");
if amount >= target {
prop_assert_eq!(after_fund.status, 1);
let after_settle = client.settle();
prop_assert_eq!(after_settle.status, 2);
} else {
prop_assert_eq!(after_fund.status, 0);
}
}
}
/// Generate a positive i128 amount bounded by `max`.
fn gen_positive_amount(max: i128) -> impl Strategy<Value = i128> {
// NatSpec style: guarantees amount > 0 for escrow entrypoints.
1i128..=max
}
/// Generate an investment call sequence.
#[derive(Clone, Debug)]
struct FundingStep {
investor_ix: usize,
amount: i128,
/// When true, use `fund_with_commitment`; otherwise use `fund`.
use_commitment: bool,
/// commitment lock applied when `use_commitment` is true.
lock_secs: u64,
}
/// Property tests for funding accounting invariants (issue #325).
proptest! {
#[test]
fn prop_funding_accounting_invariants_issue_325(
// Investors participating in the sequence (addresses may repeat across steps).
investor_count in 2usize..=6,
// Sequence length.
seq_len in 1usize..=12,
// Escrow target and per-call max.
funding_target in 50_000i128..=200_000i128,
max_each in 1i128..=50_000i128,
// Optional caps toggles.
caps_present in any::<bool>(),
// caps values when enabled
per_inv_cap in 1i128..=100_000i128,
uniq_cap in 1u32..=6u32,
// sequence components
investor_ixs in proptest::collection::vec(0usize..=5, 1usize..=12),
amounts in proptest::collection::vec(1i128..=50_000i128, 1usize..=12),
use_commitments in proptest::collection::vec(any::<bool>(), 1usize..=12),
lock_secs in proptest::collection::vec(0u64..=200u64, 1usize..=12),
) {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let client = deploy(&env);
let (token, treasury) = free_addresses(&env);
let max_per_investor = if caps_present { Some(per_inv_cap.min(funding_target)) } else { None };
let max_unique_investors: Option<u32> = if caps_present { Some(uniq_cap.min(6)) } else { None };
// Optional tiered yield is not required for these invariants; keep it off.
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "I325"),
&sme,
&funding_target,
&800i64,
&0u64,
&token,
&None,
&treasury,
&None,
&None,
&max_unique_investors,
&max_per_investor,
&None,
&None,
&None,
&None,
&None::<i64>);
let investors: Vec<Address> = (0..investor_count)
.map(|_| Address::generate(&env))
.collect();
let seq_len = seq_len
.min(investor_ixs.len())
.min(amounts.len())
.min(use_commitments.len())
.min(lock_secs.len());
// Expected model.
let mut expected_contribs: Vec<i128> = vec![0i128; investor_count];
let mut expected_funded: i128 = 0;
let mut distinct_funders: BTreeSet<Address> = BTreeSet::new();
// Track when the funded status should flip (first step where funded >= target).
let mut expected_flip_at: Option<usize> = None;
let mut actual_transitions_to_funded = 0u32;
let mut prev_status = client.get_escrow().status;
for step in 0..seq_len {
if client.get_escrow().status != 0 {
break;
}
let ix = investor_ixs[step] % investor_count;
let inv = investors[ix].clone();
let mut amt = amounts[step].min(max_each);
if amt <= 0 {
amt = 1;
}
// Filter out sequences that would violate caps by construction.
if let Some(cap) = max_per_investor {
if expected_contribs[ix] + amt > cap {
// Skip this generated step by ending the sequence.
break;
}
}
if expected_contribs[ix] == 0 {
if let Some(uc) = max_unique_investors {
if distinct_funders.len() as u32 >= uc {
break;
}
}
}
let use_commitment = use_commitments[step];
if use_commitment && expected_contribs[ix] > 0 {
break;
}
let lock = lock_secs[step];
let before_funded = client.get_escrow().funded_amount;
let before_status = client.get_escrow().status;
let after = if use_commitment {
// For first-deposit commitment invariants, lock can be 0.
client.fund_with_commitment(&inv, &amt, &lock)
} else {
client.fund(&inv, &amt)
};
// Update expected.
expected_contribs[ix] += amt;
expected_funded = expected_funded
.checked_add(amt)
.expect("expected_funded overflow");
if expected_contribs[ix] > 0 {
distinct_funders.insert(inv.clone());
}
// Invariant: conservation.
prop_assert_eq!(after.funded_amount, expected_funded);
prop_assert_eq!(client.get_escrow().funded_amount, expected_funded);
// Invariant: unique funder count.
prop_assert_eq!(
client.get_unique_funder_count(),
distinct_funders.len() as u32
);
// Invariant: caps never exceeded.
if let Some(cap) = max_per_investor {
prop_assert!(expected_contribs[ix] <= cap);
}
if let Some(uc) = max_unique_investors {
prop_assert!(distinct_funders.len() as u32 <= uc);
}
// Invariant: status flip correctness.
let should_be_funded = expected_funded >= funding_target;
let status_now = after.status;
prop_assert!(status_now >= before_status, "status monotonicity");
match expected_flip_at {
None => {
if should_be_funded {
expected_flip_at = Some(step);
prop_assert_eq!(status_now, 1);
actual_transitions_to_funded += 1;
} else {
prop_assert_eq!(status_now, 0);
}
}
Some(_) => {
if should_be_funded {
prop_assert_eq!(status_now, 1);
}
}
}
// status monotonicity and funded_amount monotonicity are already implied by conservation,
// but keep a local check.
prop_assert!(after.funded_amount >= before_funded);
// If we’ve funded, verify snapshot exists and is immutable.
if status_now == 1 {
let snap = client
.get_funding_close_snapshot()
.expect("FundingCloseSnapshot must exist when funded");
prop_assert_eq!(snap.total_principal, expected_funded);
prop_assert_eq!(snap.funding_target, funding_target);
let snap2 = client
.get_funding_close_snapshot()
.expect("FundingCloseSnapshot must still exist");
prop_assert_eq!(snap, snap2);
break;
}
prev_status = after.status;
}
// If we ever reached funded state, it must have happened exactly once.
if client.get_escrow().status == 1 {
prop_assert_eq!(actual_transitions_to_funded, 1);
}
}
}
// Issue #145: Status state machine property tests
// Valid transitions: 0->1 (fund reaches target), 1->2 (settle), 1->3 (withdraw)
// Forbidden: 1->0, 2->0, 3->0, 2->1, 3->1, 2->2, 3->3, 2->3, 3->2
#[test]
fn prop_status_transitions_open_to_funded_only() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let client = deploy(&env);
let target: i128 = 100_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "ST0"),
&sme,
&target,
&800i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
let initial = client.get_escrow();
assert_eq!(initial.status, 0, "status must start at 0");
let after = client.fund(&investor, &target);
assert_eq!(after.status, 1, "funded: status must be 1");
assert!(
after.status <= 1,
"status must not exceed 1 before settle/withdraw"
);
}
#[test]
fn prop_status_settle_transition() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let client = deploy(&env);
let target: i128 = 100_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "ST1"),
&sme,
&target,
&800i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
client.fund(&investor, &target);
let before_settle = client.get_escrow();
assert_eq!(before_settle.status, 1, "status before settle must be 1");
let after_settle = client.settle();
assert_eq!(after_settle.status, 2, "settle must transition to 2");
}
#[test]
fn prop_status_withdraw_transition() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let client = deploy(&env);
let token = install_stellar_asset_token(&env);
let target: i128 = 100_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "STW1"),
&sme,
&target,
&800i64,
&0u64,
&token.id,
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
token.stellar.mint(&investor, &target);
client.fund(&investor, &target);
let before_withdraw = client.get_escrow();
assert_eq!(
before_withdraw.status, 1,
"status before withdraw must be 1"
);
let after_withdraw = client.withdraw();
assert_eq!(after_withdraw.status, 3, "withdraw must transition to 3");
}
// Issue #145: Forbidden regression tests
#[test]
fn prop_no_regression_from_funded_status() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let client = deploy(&env);
let target: i128 = 100_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "NREG1"),
&sme,
&target,
&800i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
client.fund(&investor, &target);
let funded = client.get_escrow();
assert_eq!(funded.status, 1, "must be funded");
let settled = client.settle();
assert!(settled.status >= 1, "status must not decrease after settle");
assert_ne!(settled.status, 0, "status must never regress to 0");
assert_ne!(settled.status, 1, "after settle status must not be 1");
}
#[test]
fn prop_no_regression_after_withdraw() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let client = deploy(&env);
let token = install_stellar_asset_token(&env);
let target: i128 = 100_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "NREG2"),
&sme,
&target,
&800i64,
&0u64,
&token.id,
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
token.stellar.mint(&investor, &target);
client.fund(&investor, &target);
let withdrawn = client.withdraw();
assert_eq!(withdrawn.status, 3, "withdraw must set status to 3");
assert!(withdrawn.status >= 1, "status must not decrease below 1");
}
// Issue #145: Terminal state isolation
#[test]
fn prop_settled_is_terminal_for_settle() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let client = deploy(&env);
let target: i128 = 100_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "TERM1"),
&sme,
&target,
&800i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
client.fund(&investor, &target);
client.settle();
let settled = client.get_escrow();
assert_eq!(settled.status, 2, "must be settled");
}
#[test]
fn prop_withdrawn_is_terminal_for_withdraw() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let client = deploy(&env);
let token = install_stellar_asset_token(&env);
let target: i128 = 100_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "TERM2"),
&sme,
&target,
&800i64,
&0u64,
&token.id,
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
token.stellar.mint(&investor, &target);
client.fund(&investor, &target);
client.withdraw();
let withdrawn = client.get_escrow();
assert_eq!(withdrawn.status, 3, "must be withdrawn");
}
#[test]
fn prop_status_invariant_all_states_valid_range() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let client = deploy(&env);
let target: i128 = 200_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "INV1"),
&sme,
&target,
&800i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
assert!(client.get_escrow().status == 0);
let partial_amount = target / 2;
client.fund(&investor, &partial_amount);
let after_partial = client.get_escrow();
assert!(
after_partial.status <= 1,
"partial funding: status must be 0 or 1"
);
}
// Issue #144: funded_amount monotonicity tests
#[test]
fn prop_funded_amount_sum_of_contributions() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let client = deploy(&env);
let target: i128 = 300_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "MONO1"),
&sme,
&target,
&800i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
let inv1 = Address::generate(&env);
let inv2 = Address::generate(&env);
let inv3 = Address::generate(&env);
let amt1: i128 = 50_000_000_000i128;
let amt2: i128 = 100_000_000_000i128;
let amt3: i128 = 50_000_000_000i128;
let after1 = client.fund(&inv1, &amt1);
assert_eq!(after1.funded_amount, amt1, "first contribution");
let after2 = client.fund(&inv2, &amt2);
assert_eq!(after2.funded_amount, amt1 + amt2, "sum of contributions");
let after3 = client.fund(&inv3, &amt3);
assert_eq!(
after3.funded_amount,
amt1 + amt2 + amt3,
"total contributions"
);
}
#[test]
fn prop_funded_amount_respects_funding_target() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let investor = Address::generate(&env);
let client = deploy(&env);
let target: i128 = 100_000_000_000i128;
let excess: i128 = 50_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "MONO2"),
&sme,
&target,
&800i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
let fund_amount = target + excess;
let after = client.fund(&investor, &fund_amount);
assert_eq!(
after.funded_amount, fund_amount,
"funded_amount records exact amount"
);
assert!(after.funded_amount > target, "overfunding recorded");
}
#[test]
fn prop_funded_amount_non_decreasing_across_multiple_funders() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let inv1 = Address::generate(&env);
let inv2 = Address::generate(&env);
let inv3 = Address::generate(&env);
let client = deploy(&env);
let target: i128 = 300_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "MONO3"),
&sme,
&target,
&800i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
let amt1: i128 = 50_000_000_000i128;
let amt2: i128 = 100_000_000_000i128;
let amt3: i128 = 50_000_000_000i128;
let before1 = client.get_escrow().funded_amount;
let after1 = client.fund(&inv1, &amt1);
assert!(after1.funded_amount >= before1, "first fund non-decreasing");
let before2 = after1.funded_amount;
let after2 = client.fund(&inv2, &amt2);
assert!(
after2.funded_amount >= before2,
"second fund non-decreasing"
);
let before3 = after2.funded_amount;
let after3 = client.fund(&inv3, &amt3);
assert!(after3.funded_amount >= before3, "third fund non-decreasing");
assert_eq!(
after3.funded_amount,
before1 + amt1 + amt2 + amt3,
"total equals sum"
);
}
#[test]
fn prop_funded_amount_equals_contribution_sum_for_funded_escrow() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let client = deploy(&env);
let target: i128 = 300_000_000_000i128;
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "MONO4"),
&sme,
&target,
&800i64,
&0u64,
&Address::generate(&env),
&None,
&Address::generate(&env),
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
let amounts: [i128; 3] = [50_000_000_000i128, 100_000_000_000i128, 50_000_000_000i128];
let mut total_contributed: i128 = 0;
for amount in amounts {
let before = client.get_escrow().funded_amount;
let after = client.fund(&Address::generate(&env), &amount);
total_contributed += amount;
assert_eq!(
after.funded_amount, total_contributed,
"funded_amount equals running sum"
);
assert!(
after.funded_amount >= before,
"funded_amount never decreases"
);
}
let final_funded = client.get_escrow().funded_amount;
assert_eq!(
final_funded, total_contributed,
"final funded_amount equals total contributions"
);
}
#[derive(Clone, Copy)]
struct SplitMix64 {
state: u64,
}
impl SplitMix64 {
fn new(seed: u64) -> Self {
Self { state: seed }
}
fn next_u64(&mut self) -> u64 {
self.state = self.state.wrapping_add(0x9E3779B97F4A7C15);
let mut z = self.state;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D049BB133111EB);
z ^ (z >> 31)
}
fn gen_usize(&mut self, upper: usize) -> usize {
if upper == 0 {
return 0;
}
(self.next_u64() % (upper as u64)) as usize
}
fn gen_i128_inclusive(&mut self, lo: i128, hi: i128) -> i128 {
assert!(lo <= hi, "invalid range");
let span: u128 = (hi - lo) as u128 + 1;
let draw: u128 = (self.next_u64() as u128) % span;
lo + (draw as i128)
}
}
fn shuffle_in_place<T>(rng: &mut SplitMix64, items: &mut [T]) {
// Fisher-Yates in-place shuffle.
for i in (1..items.len()).rev() {
let j = rng.gen_usize(i + 1);
items.swap(i, j);
}
}
fn read_fuzz_seed_u64() -> u64 {
// Repro: set `ESCROW_FUZZ_SEED` (decimal or hex like `0xdeadbeef`) and re-run this test.
const DEFAULT: u64 = 0xE5D7_F00D_1760_0001;
let Ok(raw) = std::env::var("ESCROW_FUZZ_SEED") else {
return DEFAULT;
};
let raw = raw.trim();
if let Some(hex) = raw.strip_prefix("0x") {
u64::from_str_radix(hex, 16).unwrap_or(DEFAULT)
} else {
raw.parse::<u64>().unwrap_or(DEFAULT)
}
}
#[test]
fn fuzz_multi_investor_fund_ordering_snapshot_once_only() {
// Keep runtime predictable in CI; allow local override when investigating.
let cases: usize = std::env::var("ESCROW_FUZZ_CASES")
.ok()
.and_then(|v| v.parse::<usize>().ok())
.unwrap_or(64);
let base_seed = read_fuzz_seed_u64();
for case_idx in 0..cases {
let case_seed = base_seed ^ (case_idx as u64).wrapping_mul(0x9E3779B97F4A7C15u64);
let mut rng = SplitMix64::new(case_seed);
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let sme = Address::generate(&env);
let client = deploy(&env);
let (token, treasury) = free_addresses(&env);
client.init(
&admin,
&soroban_sdk::String::from_str(&env, "FUZZSNAP"),
&sme,
&TARGET,
&800i64,
&0u64,
&token,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
// Randomize investor count/order and positive amounts. Keep the sequence small so
// runtime stays within budget and shrinking isn't required to debug failures.
let investor_count: usize = 2 + rng.gen_usize(10); // 2..=11
let investors: Vec<Address> = (0..investor_count)
.map(|_| Address::generate(&env))
.collect();
let max_each = (TARGET / 2).max(1);
let mut amounts: Vec<i128> = (0..investor_count)
.map(|_| rng.gen_i128_inclusive(1, max_each))
.collect();
// Guarantee we cross the target at least once (and often overfund a bit).
let sum: i128 = amounts.iter().sum();
if sum < TARGET {
let top_up_idx = rng.gen_usize(investor_count);
let needed = TARGET - sum;
let extra = rng.gen_i128_inclusive(0, (TARGET / 4).max(1));
amounts[top_up_idx] = amounts[top_up_idx]
.checked_add(needed + extra)
.expect("amount top-up overflow");
}
let mut order: Vec<usize> = (0..investor_count).collect();
shuffle_in_place(&mut rng, &mut order);
// Find the first call that crosses the funding target so we can assert that:
// - status flips to funded exactly once
// - FundingCloseSnapshot is written exactly once and never changes thereafter
let mut cumulative = 0i128;
let mut close_pos = None;
for (pos, &idx) in order.iter().enumerate() {
cumulative = cumulative
.checked_add(amounts[idx])
.expect("cumulative overflow");
if cumulative >= TARGET {
close_pos = Some(pos);
break;
}
}
let close_pos = close_pos.expect("expected funding to reach target");
assert_eq!(