Skip to content

Commit f0b7d91

Browse files
committed
Refactor price aggregation test
1 parent a189fbb commit f0b7d91

File tree

1 file changed

+72
-78
lines changed

1 file changed

+72
-78
lines changed

program/rust/src/tests/test_upd_aggregate.rs

Lines changed: 72 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,14 @@ fn test_upd_aggregate() {
5959
corp_act_status_: 0,
6060
};
6161

62-
let p5: PriceInfo = PriceInfo {
62+
let mut p5: PriceInfo = PriceInfo {
6363
price_: 500,
6464
conf_: 50,
6565
status_: PC_STATUS_TRADING,
66-
pub_slot_: 1050,
66+
pub_slot_: 1024,
6767
corp_act_status_: 0,
6868
};
6969

70-
let p6: PriceInfo = PriceInfo {
71-
price_: 600,
72-
conf_: 60,
73-
status_: PC_STATUS_TRADING,
74-
pub_slot_: 1074,
75-
corp_act_status_: 0,
76-
};
7770

7871
let mut instruction_data = [0u8; size_of::<UpdPriceArgs>()];
7972
populate_instruction(&mut instruction_data, 42, 2, 1);
@@ -287,18 +280,52 @@ fn test_upd_aggregate() {
287280
assert_eq!(price_data.prev_timestamp_, 5);
288281
}
289282

290-
// check max_latency_ = 0 defaults to PC_MAX_SEND_LATENCY
283+
// // check max_latency_ = 0 defaults to PC_MAX_SEND_LATENCY
284+
// {
285+
// let mut price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
286+
// price_data.num_ = 1;
287+
// price_data.agg_.pub_slot_ = 1050;
288+
// price_data.comp_[0].latest_ = p5;
289+
// }
290+
291+
// unsafe {
292+
// assert!(c_upd_aggregate(
293+
// price_account.try_borrow_mut_data().unwrap().as_mut_ptr(),
294+
// 1051,
295+
// 13,
296+
// ));
297+
// }
298+
299+
// {
300+
// let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
301+
302+
// assert_eq!(price_data.max_latency_, 0);
303+
// assert_eq!(price_data.agg_.price_, 500);
304+
// assert_eq!(price_data.agg_.conf_, 50);
305+
// assert_eq!(price_data.twap_.val_, 177);
306+
// assert_eq!(price_data.twac_.val_, 34);
307+
// assert_eq!(price_data.num_qt_, 1);
308+
// assert_eq!(price_data.timestamp_, 13);
309+
// assert_eq!(price_data.prev_slot_, 1025);
310+
// assert_eq!(price_data.prev_price_, 245);
311+
// assert_eq!(price_data.prev_conf_, 85);
312+
// assert_eq!(price_data.prev_timestamp_, 5);
313+
// }
314+
315+
// ensure the update occurs within the PC_MAX_SEND_LATENCY limit of 25 slots, allowing the aggregated price to reflect both p4 and p5 contributions
291316
{
292317
let mut price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
293-
price_data.num_ = 1;
294-
price_data.agg_.pub_slot_ = 1050;
295-
price_data.comp_[0].latest_ = p5;
318+
price_data.num_ = 2;
319+
price_data.last_slot_ = 1000;
320+
price_data.agg_.pub_slot_ = 1000;
321+
price_data.comp_[0].latest_ = p4;
322+
price_data.comp_[1].latest_ = p5;
296323
}
297324

298325
unsafe {
299326
assert!(c_upd_aggregate(
300327
price_account.try_borrow_mut_data().unwrap().as_mut_ptr(),
301-
1051,
328+
1025,
302329
13,
303330
));
304331
}
@@ -307,32 +334,23 @@ fn test_upd_aggregate() {
307334
let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
308335

309336
assert_eq!(price_data.max_latency_, 0);
310-
assert_eq!(price_data.agg_.price_, 500);
311-
assert_eq!(price_data.agg_.conf_, 50);
312-
assert_eq!(price_data.twap_.val_, 177);
313-
assert_eq!(price_data.twac_.val_, 34);
314-
assert_eq!(price_data.num_qt_, 1);
337+
assert_eq!(price_data.agg_.price_, 445);
338+
assert_eq!(price_data.agg_.conf_, 55);
339+
assert_eq!(price_data.twap_.val_, 168);
340+
assert_eq!(price_data.twac_.val_, 35);
341+
assert_eq!(price_data.num_qt_, 2);
315342
assert_eq!(price_data.timestamp_, 13);
316343
assert_eq!(price_data.prev_slot_, 1025);
317344
assert_eq!(price_data.prev_price_, 245);
318345
assert_eq!(price_data.prev_conf_, 85);
319346
assert_eq!(price_data.prev_timestamp_, 5);
320347
}
321348

322-
// ensure the update occurs within the PC_MAX_SEND_LATENCY limit of 25 slots, allowing the aggregated price to reflect both p5 and p6 contributions
323-
{
324-
let mut price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
325-
price_data.num_ = 2;
326-
price_data.last_slot_ = 1025;
327-
price_data.agg_.pub_slot_ = 1050;
328-
price_data.comp_[0].latest_ = p5;
329-
price_data.comp_[1].latest_ = p6;
330-
}
331-
349+
// verify behavior when publishing halts for 1 slot, causing the slot difference from p5 to exceed the PC_MAX_SEND_LATENCY threshold of 25.
332350
unsafe {
333351
assert!(c_upd_aggregate(
334352
price_account.try_borrow_mut_data().unwrap().as_mut_ptr(),
335-
1075,
353+
1026,
336354
14,
337355
));
338356
}
@@ -341,59 +359,35 @@ fn test_upd_aggregate() {
341359
let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
342360

343361
assert_eq!(price_data.max_latency_, 0);
344-
assert_eq!(price_data.agg_.price_, 545);
345-
assert_eq!(price_data.agg_.conf_, 55);
346-
assert_eq!(price_data.twap_.val_, 212);
362+
assert_eq!(price_data.agg_.price_, 500);
363+
assert_eq!(price_data.agg_.conf_, 50);
364+
assert_eq!(price_data.twap_.val_, 203);
347365
assert_eq!(price_data.twac_.val_, 36);
348-
assert_eq!(price_data.num_qt_, 2);
349-
assert_eq!(price_data.timestamp_, 14);
350-
assert_eq!(price_data.prev_slot_, 1050);
351-
assert_eq!(price_data.prev_price_, 500);
352-
assert_eq!(price_data.prev_conf_, 50);
353-
assert_eq!(price_data.prev_timestamp_, 13);
354-
}
355-
356-
// verify behavior when publishing halts for 1 slot, causing the slot difference from p5 to exceed the PC_MAX_SEND_LATENCY threshold of 25.
357-
unsafe {
358-
assert!(c_upd_aggregate(
359-
price_account.try_borrow_mut_data().unwrap().as_mut_ptr(),
360-
1076,
361-
15,
362-
));
363-
}
364-
365-
{
366-
let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
367-
368-
assert_eq!(price_data.max_latency_, 0);
369-
assert_eq!(price_data.agg_.price_, 600);
370-
assert_eq!(price_data.agg_.conf_, 60);
371-
assert_eq!(price_data.twap_.val_, 244);
372-
assert_eq!(price_data.twac_.val_, 38);
373366
assert_eq!(price_data.num_qt_, 1);
374-
assert_eq!(price_data.timestamp_, 15);
375-
assert_eq!(price_data.prev_slot_, 1075);
376-
assert_eq!(price_data.prev_price_, 545);
367+
assert_eq!(price_data.timestamp_, 14);
368+
assert_eq!(price_data.prev_slot_, 1025);
369+
assert_eq!(price_data.prev_price_, 445);
377370
assert_eq!(price_data.prev_conf_, 55);
378-
assert_eq!(price_data.prev_timestamp_, 14);
371+
assert_eq!(price_data.prev_timestamp_, 13);
379372
}
380373

381-
// verify behavior when max_latency_ is set to 5, this should result in PC_STATUS_UNKNOWN status
374+
// verify behavior when max_latency_ is set to 5, and all components pub_slot_ gap is more than 5, this should result in PC_STATUS_UNKNOWN status
382375
{
383376
let mut price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
384377
price_data.max_latency_ = 5;
385378
price_data.num_ = 2;
386-
price_data.last_slot_ = 1025;
387-
price_data.agg_.pub_slot_ = 1050;
388-
price_data.comp_[0].latest_ = p5;
389-
price_data.comp_[1].latest_ = p6;
379+
price_data.last_slot_ = 1000;
380+
price_data.agg_.pub_slot_ = 1000;
381+
p5.pub_slot_ = 1004;
382+
price_data.comp_[0].latest_ = p4;
383+
price_data.comp_[1].latest_ = p5;
390384
}
391385

392386
unsafe {
393387
assert!(!c_upd_aggregate(
394388
price_account.try_borrow_mut_data().unwrap().as_mut_ptr(),
395-
1080,
396-
16,
389+
1010,
390+
15,
397391
));
398392
}
399393

@@ -402,16 +396,16 @@ fn test_upd_aggregate() {
402396

403397
assert_eq!(price_data.max_latency_, 5);
404398
assert_eq!(price_data.agg_.status_, PC_STATUS_UNKNOWN);
405-
assert_eq!(price_data.agg_.price_, 600);
406-
assert_eq!(price_data.agg_.conf_, 60);
407-
assert_eq!(price_data.twap_.val_, 244);
408-
assert_eq!(price_data.twac_.val_, 38);
399+
assert_eq!(price_data.agg_.price_, 500);
400+
assert_eq!(price_data.agg_.conf_, 50);
401+
assert_eq!(price_data.twap_.val_, 203);
402+
assert_eq!(price_data.twac_.val_, 36);
409403
assert_eq!(price_data.num_qt_, 0);
410-
assert_eq!(price_data.timestamp_, 16);
411-
assert_eq!(price_data.prev_slot_, 1050);
412-
assert_eq!(price_data.prev_price_, 600);
413-
assert_eq!(price_data.prev_conf_, 60);
414-
assert_eq!(price_data.prev_timestamp_, 15);
404+
assert_eq!(price_data.timestamp_, 15);
405+
assert_eq!(price_data.prev_slot_, 1000);
406+
assert_eq!(price_data.prev_price_, 500);
407+
assert_eq!(price_data.prev_conf_, 50);
408+
assert_eq!(price_data.prev_timestamp_, 14);
415409
}
416410
}
417411

0 commit comments

Comments
 (0)