forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_batch.rs
More file actions
570 lines (460 loc) · 17.5 KB
/
Copy pathtest_batch.rs
File metadata and controls
570 lines (460 loc) · 17.5 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
//! Tests for [`StreamingContract::create_streams_batch`] — Issue #165
//!
//! Covers:
//! - Happy path: batch creates all streams and returns correct IDs
//! - Partial validation failure: any invalid stream rejects the whole batch
//! - Max batch size enforcement (> 20 streams → BatchSizeExceeded)
//! - Empty batch → BatchEmpty
//! - Sender/recipient index updated for every stream
//! - Events emitted per stream
#![cfg(test)]
extern crate std;
use super::*;
use soroban_sdk::{
testutils::{Address as _, Ledger},
token::{Client as TokenClient, StellarAssetClient},
Address, Env, Vec,
};
// ─── Test helpers ─────────────────────────────────────────────────────────────
struct TestEnv {
env: Env,
contract_id: Address,
token_id: Address,
sender: Address,
recipient: Address,
}
impl TestEnv {
fn setup() -> Self {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register(StreamingContract, ());
let sender = Address::generate(&env);
let recipient = Address::generate(&env);
let admin = Address::generate(&env);
let token_admin = Address::generate(&env);
let token_id = env
.register_stellar_asset_contract_v2(token_admin.clone())
.address();
let asset_client = StellarAssetClient::new(&env, &token_id);
// Mint 10M tokens so we have room for large batch tests
asset_client.mint(&sender, &10_000_000_0000000);
let client = StreamingContractClient::new(&env, &contract_id);
client.initialize(&admin);
TestEnv {
env,
contract_id,
token_id,
sender,
recipient,
}
}
fn client(&self) -> StreamingContractClient<'_> {
StreamingContractClient::new(&self.env, &self.contract_id)
}
fn token(&self) -> TokenClient<'_> {
TokenClient::new(&self.env, &self.token_id)
}
fn set_time(&self, timestamp: u64) {
self.env.ledger().with_mut(|l| l.timestamp = timestamp);
}
/// Build a valid [`CreateStreamInput`] pointing at `recipient`.
fn make_input(&self, recipient: &Address, now: u64) -> CreateStreamInput {
CreateStreamInput {
recipient: recipient.clone(),
token: self.token_id.clone(),
total_amount: 1_000_0000000, // 1000 tokens
start_time: now,
end_time: now + 1000,
cliff_time: now,
cliff_amount: 0,
}
}
/// Approve `amount` tokens to the contract from the sender.
fn approve(&self, amount: i128) {
self.token().approve(
&self.sender,
&self.contract_id,
&amount,
&(self.env.ledger().sequence() + 500),
);
}
}
// ─── Happy path ───────────────────────────────────────────────────────────────
#[test]
fn test_batch_create_happy_path() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let r1 = Address::generate(&t.env);
let r2 = Address::generate(&t.env);
let r3 = Address::generate(&t.env);
let per_stream = 1_000_0000000i128;
let total_approval = per_stream * 3;
t.approve(total_approval);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(t.make_input(&r1, now));
inputs.push_back(t.make_input(&r2, now));
inputs.push_back(t.make_input(&r3, now));
let client = t.client();
let ids = client.create_streams_batch(&t.sender, &inputs);
// Three IDs returned in order
assert_eq!(ids.len(), 3);
assert_eq!(ids.get(0).unwrap(), 1u64);
assert_eq!(ids.get(1).unwrap(), 2u64);
assert_eq!(ids.get(2).unwrap(), 3u64);
// Each stream has the correct state
for (i, recipient) in [&r1, &r2, &r3].iter().enumerate() {
let id = ids.get(i as u32).unwrap();
let stream = client.get_stream(&id);
assert_eq!(stream.sender, t.sender);
assert_eq!(&stream.recipient, *recipient);
assert_eq!(stream.deposited_amount, per_stream);
assert_eq!(stream.withdrawn_amount, 0);
assert!(!stream.cancelled);
}
// Contract holds all funds
assert_eq!(t.token().balance(&t.contract_id), total_approval);
// Sender balance reduced by total
assert_eq!(
t.token().balance(&t.sender),
10_000_000_0000000 - total_approval
);
}
#[test]
fn test_batch_create_returns_ids_in_order() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
// Pre-create one stream so IDs don't start at 1
let single_amount = 1_000_0000000i128;
t.approve(single_amount);
let client = t.client();
let params = CreateStreamParams {
recipient: t.recipient.clone(),
token: t.token_id.clone(),
total_amount: single_amount,
start_time: now,
end_time: now + 1000,
cliff_time: now,
cliff_amount: 0,
};
client.create_stream(&t.sender, ¶ms);
// Batch of 2 → IDs should be 2 and 3
let r1 = Address::generate(&t.env);
let r2 = Address::generate(&t.env);
t.approve(single_amount * 2);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(t.make_input(&r1, now));
inputs.push_back(t.make_input(&r2, now));
let ids = client.create_streams_batch(&t.sender, &inputs);
assert_eq!(ids.len(), 2);
assert_eq!(ids.get(0).unwrap(), 2u64);
assert_eq!(ids.get(1).unwrap(), 3u64);
}
// ─── Index management ─────────────────────────────────────────────────────────
#[test]
fn test_batch_sender_index_updated() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let per_stream = 1_000_0000000i128;
t.approve(per_stream * 5);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
for _ in 0..5 {
let r = Address::generate(&t.env);
inputs.push_back(t.make_input(&r, now));
}
let client = t.client();
let ids = client.create_streams_batch(&t.sender, &inputs);
let sent = client.get_sent_streams(&t.sender, &0, &100);
assert_eq!(sent.len(), 5);
for i in 0..5u32 {
let id = ids.get(i).unwrap();
assert!(sent.contains(id));
}
}
#[test]
fn test_batch_recipient_indexes_updated() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
// Two streams to the same recipient and one to a different recipient
let r_shared = Address::generate(&t.env);
let r_other = Address::generate(&t.env);
let per_stream = 1_000_0000000i128;
t.approve(per_stream * 3);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(t.make_input(&r_shared, now));
inputs.push_back(t.make_input(&r_shared, now));
inputs.push_back(t.make_input(&r_other, now));
let client = t.client();
let ids = client.create_streams_batch(&t.sender, &inputs);
// r_shared should have 2 streams
let shared_received = client.get_received_streams(&r_shared, &0, &100);
assert_eq!(shared_received.len(), 2);
assert!(shared_received.contains(ids.get(0).unwrap()));
assert!(shared_received.contains(ids.get(1).unwrap()));
// r_other should have 1 stream
let other_received = client.get_received_streams(&r_other, &0, &100);
assert_eq!(other_received.len(), 1);
assert!(other_received.contains(ids.get(2).unwrap()));
}
// ─── Validation failures ──────────────────────────────────────────────────────
#[test]
fn test_batch_partial_failure_rejected_atomically() {
// Stream 0 is valid, stream 1 has zero amount — entire batch must fail
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let r1 = Address::generate(&t.env);
let r2 = Address::generate(&t.env);
let per_stream = 1_000_0000000i128;
t.approve(per_stream * 2);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(t.make_input(&r1, now)); // valid
// Invalid: zero amount
inputs.push_back(CreateStreamInput {
recipient: r2.clone(),
token: t.token_id.clone(),
total_amount: 0,
start_time: now,
end_time: now + 1000,
cliff_time: now,
cliff_amount: 0,
});
let client = t.client();
let result = client.try_create_streams_batch(&t.sender, &inputs);
assert_eq!(result, Err(Ok(StreamError::InvalidAmount)));
// No streams should have been created
let sent = client.get_sent_streams(&t.sender, &0, &100);
assert_eq!(sent.len(), 0);
// Contract should hold no funds
assert_eq!(t.token().balance(&t.contract_id), 0);
}
#[test]
fn test_batch_accepts_past_start_and_cliff_times() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let r1 = Address::generate(&t.env);
let per_stream = 1_000_0000000i128;
t.approve(per_stream);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(CreateStreamInput {
recipient: r1,
token: t.token_id.clone(),
total_amount: per_stream,
start_time: now - 1000,
end_time: now + 1000,
cliff_time: now - 500,
cliff_amount: 0,
});
let client = t.client();
let ids = client.create_streams_batch(&t.sender, &inputs);
assert_eq!(ids.len(), 1);
let stream = client.get_stream(&ids.get(0).unwrap());
assert_eq!(stream.start_time, now - 1000);
assert_eq!(stream.cliff_time, now - 500);
assert_eq!(stream.end_time, now + 1000);
}
#[test]
fn test_batch_invalid_time_range_fails() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let r1 = Address::generate(&t.env);
let per_stream = 1_000_0000000i128;
t.approve(per_stream);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(CreateStreamInput {
recipient: r1,
token: t.token_id.clone(),
total_amount: per_stream,
start_time: now + 1000,
end_time: now + 500, // end before start
cliff_time: now + 500,
cliff_amount: 0,
});
let result = t.client().try_create_streams_batch(&t.sender, &inputs);
assert_eq!(result, Err(Ok(StreamError::InvalidTimeRange)));
}
#[test]
fn test_batch_invalid_cliff_fails() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let r1 = Address::generate(&t.env);
let per_stream = 1_000_0000000i128;
t.approve(per_stream);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(CreateStreamInput {
recipient: r1,
token: t.token_id.clone(),
total_amount: per_stream,
start_time: now,
end_time: now + 1000,
cliff_time: now + 2000, // cliff after end
cliff_amount: 0,
});
let result = t.client().try_create_streams_batch(&t.sender, &inputs);
assert_eq!(result, Err(Ok(StreamError::InvalidCliff)));
}
#[test]
fn test_batch_self_stream_fails() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let per_stream = 1_000_0000000i128;
t.approve(per_stream);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(CreateStreamInput {
recipient: t.sender.clone(), // self-stream
token: t.token_id.clone(),
total_amount: per_stream,
start_time: now,
end_time: now + 1000,
cliff_time: now,
cliff_amount: 0,
});
let result = t.client().try_create_streams_batch(&t.sender, &inputs);
assert_eq!(result, Err(Ok(StreamError::SelfStream)));
}
// ─── Batch size limits ────────────────────────────────────────────────────────
#[test]
fn test_batch_max_size_exactly_20_succeeds() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let per_stream = 1_000_0000000i128;
t.approve(per_stream * 20);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
for _ in 0..20 {
let r = Address::generate(&t.env);
inputs.push_back(t.make_input(&r, now));
}
let client = t.client();
let ids = client.create_streams_batch(&t.sender, &inputs);
assert_eq!(ids.len(), 20);
}
#[test]
fn test_batch_over_max_size_fails() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let per_stream = 1_000_0000000i128;
t.approve(per_stream * 21);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
for _ in 0..21 {
let r = Address::generate(&t.env);
inputs.push_back(t.make_input(&r, now));
}
let result = t.client().try_create_streams_batch(&t.sender, &inputs);
assert_eq!(result, Err(Ok(StreamError::BatchSizeExceeded)));
}
#[test]
fn test_batch_empty_fails() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
let result = t.client().try_create_streams_batch(&t.sender, &inputs);
assert_eq!(result, Err(Ok(StreamError::BatchEmpty)));
}
// ─── Single-stream batch ──────────────────────────────────────────────────────
#[test]
fn test_batch_single_stream_works() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let r = Address::generate(&t.env);
let amount = 1_000_0000000i128;
t.approve(amount);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(t.make_input(&r, now));
let client = t.client();
let ids = client.create_streams_batch(&t.sender, &inputs);
assert_eq!(ids.len(), 1);
let stream = client.get_stream(&ids.get(0).unwrap());
assert_eq!(stream.deposited_amount, amount);
}
// ─── Cliff support in batch ───────────────────────────────────────────────────
#[test]
fn test_batch_supports_cliff() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let r = Address::generate(&t.env);
let amount = 1_000_0000000i128;
let cliff_amount = 100_0000000i128;
t.approve(amount);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(CreateStreamInput {
recipient: r.clone(),
token: t.token_id.clone(),
total_amount: amount,
start_time: now,
end_time: now + 1000,
cliff_time: now + 200,
cliff_amount,
});
let client = t.client();
let ids = client.create_streams_batch(&t.sender, &inputs);
let id = ids.get(0).unwrap();
let stream = client.get_stream(&id);
assert_eq!(stream.cliff_amount, cliff_amount);
assert_eq!(stream.cliff_time, now + 200);
// Before cliff — nothing withdrawable
t.set_time(now + 100);
assert_eq!(client.get_withdrawable(&id), 0);
// After cliff — cliff amount available
t.set_time(now + 200);
assert!(client.get_withdrawable(&id) >= cliff_amount);
}
// ─── Batch streams are individually withdrawable ──────────────────────────────
#[test]
fn test_batch_streams_are_independently_withdrawable() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let r1 = Address::generate(&t.env);
let r2 = Address::generate(&t.env);
let per_stream = 1_000_0000000i128;
t.approve(per_stream * 2);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(t.make_input(&r1, now));
inputs.push_back(t.make_input(&r2, now));
let client = t.client();
let ids = client.create_streams_batch(&t.sender, &inputs);
// Advance halfway
t.set_time(now + 500);
let w1 = client.get_withdrawable(&ids.get(0).unwrap());
let w2 = client.get_withdrawable(&ids.get(1).unwrap());
assert!(w1 > 0);
assert!(w2 > 0);
// Withdraw from stream 1 only
client.withdraw(&ids.get(0).unwrap(), &w1);
assert_eq!(t.token().balance(&r1), w1);
assert_eq!(t.token().balance(&r2), 0); // stream 2 untouched
}
// ─── Batch streams are independently cancellable ──────────────────────────────
#[test]
fn test_batch_streams_are_independently_cancellable() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let r1 = Address::generate(&t.env);
let r2 = Address::generate(&t.env);
let per_stream = 1_000_0000000i128;
t.approve(per_stream * 2);
let mut inputs: Vec<CreateStreamInput> = Vec::new(&t.env);
inputs.push_back(t.make_input(&r1, now));
inputs.push_back(t.make_input(&r2, now));
let client = t.client();
let ids = client.create_streams_batch(&t.sender, &inputs);
// Cancel stream 1 only
client.cancel(&ids.get(0).unwrap());
let s1 = client.get_stream(&ids.get(0).unwrap());
let s2 = client.get_stream(&ids.get(1).unwrap());
assert!(s1.cancelled);
assert!(!s2.cancelled);
}