forked from Cylo-Traders/Agrocylo-PIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproptest_invariants.rs
More file actions
300 lines (245 loc) · 10.9 KB
/
Copy pathproptest_invariants.rs
File metadata and controls
300 lines (245 loc) · 10.9 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
// Property-based invariant tests for the production escrow contract.
//
// Run: cargo test -p production_escrow proptest
// Tune: PROPTEST_CASES=2000 cargo test -p production_escrow proptest
// Seed: PROPTEST_SEED=0xDEADBEEF cargo test ... (reproduce a specific failure)
#![cfg(test)]
extern crate std;
use proptest::prelude::*;
use soroban_sdk::{testutils::Address as _, token::StellarAssetClient, Address, Env, Symbol};
use super::{DisputeResolution, ProductionEscrowContract};
// ── helpers ──────────────────────────────────────────────────────────────────
/// Divides `total` into `proportions.len()` slots, each > 0, summing exactly
/// to `total`. Any integer remainder from division goes to the last slot.
fn distribute(proportions: &[u32], total: i64) -> std::vec::Vec<i64> {
let prop_sum: u64 = proportions.iter().map(|&p| p as u64).sum();
let mut amounts: std::vec::Vec<i64> = proportions
.iter()
.map(|&p| (total as u64 * p as u64 / prop_sum) as i64)
.collect();
let assigned: i64 = amounts.iter().sum();
*amounts.last_mut().unwrap() += total - assigned;
// Drop any slot that rounded down to zero (only when total < slot count).
amounts.into_iter().filter(|&x| x > 0).collect()
}
fn create_token<'a>(env: &'a Env, admin: &Address) -> (Address, StellarAssetClient<'a>) {
let token_id = env.register_stellar_asset_contract_v2(admin.clone());
let sac = StellarAssetClient::new(env, &token_id.address());
(token_id.address(), sac)
}
// ── Scenario 1: Failed campaign — sum(refunds) ≤ refundable ──────────────────
//
// Invariants verified:
// • sum of all claimed pro-rata shares ≤ campaign.refundable
// • every contribution slot is zeroed after a successful claim (no double-spend)
proptest! {
#![proptest_config(ProptestConfig::with_cases(200))]
#[test]
fn prop_failed_campaign_refunds_bounded(
target in 2i64..=100_000i64,
proportions in prop::collection::vec(1u32..=1000u32, 1..=5_usize),
) {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register_contract(None, ProductionEscrowContract);
let client = super::ProductionEscrowContractClient::new(&env, &contract_id);
let admin = Address::generate(&env);
let farmer = Address::generate(&env);
let (token_addr, sac) = create_token(&env, &admin);
// Pre-fund the contract balance so receive_contribution's solvency check passes.
sac.mint(&contract_id, &(target as i128));
client.initialize(&admin);
client.create_campaign(
&1u64,
&farmer,
&(target as i128),
&token_addr,
&1_000_000u64,
&Symbol::new(&env, "harvest"),
);
let amounts = distribute(&proportions, target);
prop_assume!(!amounts.is_empty());
let investors: std::vec::Vec<Address> =
amounts.iter().map(|_| Address::generate(&env)).collect();
for (inv, &amt) in investors.iter().zip(amounts.iter()) {
client.receive_contribution(&1u64, inv, &(amt as i128));
}
client.complete_funding(&1u64, &(target as i128));
// Mark failed — all held funds (= total_funded here) become refundable.
client.mark_failed(&1u64);
let campaign = client.get_campaign(&1u64);
let refundable = campaign.refundable;
// Simulate what claim_refund computes, then call it.
let mut total_claimed: i128 = 0;
for inv in &investors {
let contributed = client.get_contribution(&1u64, inv);
if contributed > 0 {
let share = contributed * refundable / (target as i128);
if share > 0 {
client.claim_refund(&1u64, inv);
total_claimed += share;
}
}
}
// sum(claimed) ≤ refundable (integer truncation may leave dust in contract).
prop_assert!(
total_claimed <= refundable,
"total_claimed={} > refundable={}", total_claimed, refundable
);
// Every slot that claimed must now be zeroed (no double-spend).
for inv in &investors {
prop_assert_eq!(
client.get_contribution(&1u64, inv),
0i128,
"contribution slot not zeroed after claim_refund"
);
}
}
}
// ── Scenario 2: Settled campaign — sum(returns) ≤ returnable ─────────────────
//
// Invariants verified:
// • campaign.returnable equals (held − farmer_payout) after settlement
// • sum of all claimed pro-rata returns ≤ campaign.returnable
// • every contribution slot is zeroed after a successful claim
proptest! {
#![proptest_config(ProptestConfig::with_cases(200))]
#[test]
fn prop_settled_campaign_returns_bounded(
target in 2i64..=100_000i64,
proportions in prop::collection::vec(1u32..=1000u32, 1..=5_usize),
payout_pct in 0u32..=100u32,
) {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register_contract(None, ProductionEscrowContract);
let client = super::ProductionEscrowContractClient::new(&env, &contract_id);
let admin = Address::generate(&env);
let farmer = Address::generate(&env);
let (token_addr, sac) = create_token(&env, &admin);
sac.mint(&contract_id, &(target as i128));
client.initialize(&admin);
client.create_campaign(
&1u64,
&farmer,
&(target as i128),
&token_addr,
&1_000_000u64,
&Symbol::new(&env, "harvest"),
);
let amounts = distribute(&proportions, target);
prop_assume!(!amounts.is_empty());
let investors: std::vec::Vec<Address> =
amounts.iter().map(|_| Address::generate(&env)).collect();
for (inv, &amt) in investors.iter().zip(amounts.iter()) {
client.receive_contribution(&1u64, inv, &(amt as i128));
}
client.complete_funding(&1u64, &(target as i128));
// Farmer reports harvest, admin settles.
client.report_harvest(&1u64, &farmer, &Symbol::new(&env, "good"));
let held: i128 = target as i128; // released=refundable=returnable=0
let farmer_payout: i128 = (held as u64 * payout_pct as u64 / 100) as i128;
let expected_returnable = held - farmer_payout;
client.settle_campaign(&1u64, &farmer, &farmer_payout);
let campaign = client.get_campaign(&1u64);
let returnable = campaign.returnable;
prop_assert_eq!(
returnable,
expected_returnable,
"returnable mismatch: got {} expected {}", returnable, expected_returnable
);
if returnable == 0 {
return Ok(());
}
let mut total_claimed: i128 = 0;
for inv in &investors {
let contributed = client.get_contribution(&1u64, inv);
if contributed > 0 {
let share = contributed * returnable / (target as i128);
if share > 0 {
client.claim_return(&1u64, inv);
total_claimed += share;
}
}
}
prop_assert!(
total_claimed <= returnable,
"total_claimed={} > returnable={}", total_claimed, returnable
);
for inv in &investors {
prop_assert_eq!(
client.get_contribution(&1u64, inv),
0i128,
"contribution slot not zeroed after claim_return"
);
}
}
}
// ── Scenario 3: Partial settlement — payout + refundable == held (exact) ─────
//
// Invariants verified:
// • campaign.released == payout_amount after PartialSettlement
// • campaign.refundable == held − payout_amount (no rounding loss)
// • released + refundable == held (exact conservation, no dust)
// • escrow_held == 0 (all funds accounted for)
proptest! {
#![proptest_config(ProptestConfig::with_cases(200))]
#[test]
fn prop_partial_settlement_conservation(
target in 3i64..=100_000i64,
payout_frac_num in 1i64..=98i64, // payout = frac/100 * held
) {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register_contract(None, ProductionEscrowContract);
let client = super::ProductionEscrowContractClient::new(&env, &contract_id);
let admin = Address::generate(&env);
let farmer = Address::generate(&env);
let investor = Address::generate(&env);
let (token_addr, sac) = create_token(&env, &admin);
sac.mint(&contract_id, &(target as i128));
client.initialize(&admin);
client.create_campaign(
&1u64,
&farmer,
&(target as i128),
&token_addr,
&1_000_000u64,
&Symbol::new(&env, "harvest"),
);
// Single investor funds the whole campaign.
client.receive_contribution(&1u64, &investor, &(target as i128));
client.complete_funding(&1u64, &(target as i128));
// Investor opens a dispute while the campaign is Funded.
client.open_dispute(&1u64, &investor, &Symbol::new(&env, "quality"));
let held: i128 = target as i128; // released=refundable=returnable=0
let payout_amount: i128 = (held * payout_frac_num / 100).max(1).min(held - 1);
let expected_refundable = held - payout_amount;
client.resolve_dispute(&1u64, &DisputeResolution::PartialSettlement, &payout_amount);
let campaign = client.get_campaign(&1u64);
// Exact conservation: payout booked as released, remainder as refundable.
prop_assert_eq!(
campaign.released, payout_amount,
"released ({}) != payout_amount ({})", campaign.released, payout_amount
);
prop_assert_eq!(
campaign.refundable, expected_refundable,
"refundable ({}) != expected ({})", campaign.refundable, expected_refundable
);
prop_assert_eq!(
campaign.released + campaign.refundable,
held,
"released + refundable ({}) != held ({})",
campaign.released + campaign.refundable,
held
);
// escrow_held = total_funded − released − refundable − returnable must be 0.
let escrow_held =
campaign.total_funded - campaign.released - campaign.refundable - campaign.returnable;
prop_assert_eq!(
escrow_held,
0i128,
"escrow_held not zero after full dispute resolution: {}", escrow_held
);
}
}