Skip to content

Commit 144bf94

Browse files
committed
add test to simulate program upgrade
1 parent 7aa7f94 commit 144bf94

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

program/rust/src/tests/test_upd_price.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use {
2929
program_error::ProgramError,
3030
pubkey::Pubkey,
3131
},
32+
solana_sdk::account_info::AccountInfo,
3233
std::mem::size_of,
3334
};
3435

@@ -312,6 +313,75 @@ fn test_upd_price() {
312313
assert_eq!(price_data.prev_conf_, 2);
313314
assert_eq!(price_data.prev_timestamp_, 4);
314315
}
316+
317+
// add new test for multiple publishers and ensure that agg price is not updated multiple times when program upgrade happens in the same slot after the first update
318+
let mut funding_setup_two = AccountSetup::new_funding();
319+
let funding_account_two = funding_setup_two.as_account_info();
320+
321+
add_publisher(&mut price_account, funding_account_two.key, 1);
322+
323+
populate_instruction(&mut instruction_data, 10, 1, 10);
324+
update_clock_slot(&mut clock_account, 10);
325+
326+
assert!(process_instruction(
327+
&program_id,
328+
&[
329+
funding_account.clone(),
330+
price_account.clone(),
331+
clock_account.clone()
332+
],
333+
&instruction_data
334+
)
335+
.is_ok());
336+
337+
{
338+
let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
339+
assert_eq!(price_data.comp_[0].latest_.price_, 10);
340+
assert_eq!(price_data.comp_[0].latest_.conf_, 1);
341+
assert_eq!(price_data.comp_[0].latest_.pub_slot_, 10);
342+
assert_eq!(price_data.comp_[0].latest_.status_, PC_STATUS_TRADING);
343+
assert_eq!(price_data.valid_slot_, 8);
344+
assert_eq!(price_data.agg_.pub_slot_, 10);
345+
assert_eq!(price_data.agg_.price_, 10);
346+
assert_eq!(price_data.agg_.status_, PC_STATUS_TRADING);
347+
assert_eq!(price_data.prev_slot_, 8);
348+
assert_eq!(price_data.prev_price_, -100);
349+
assert_eq!(price_data.prev_conf_, 1);
350+
assert_eq!(price_data.prev_timestamp_, 4);
351+
}
352+
353+
// reset twap_.denom_ to 0 to simulate program upgrade in the same slot and make sure agg_.price_ is not updated again
354+
{
355+
let mut price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
356+
price_data.prev_twap_.denom_ = 0;
357+
}
358+
populate_instruction(&mut instruction_data, 20, 1, 10);
359+
assert!(process_instruction(
360+
&program_id,
361+
&[
362+
funding_account_two.clone(),
363+
price_account.clone(),
364+
clock_account.clone()
365+
],
366+
&instruction_data
367+
)
368+
.is_ok());
369+
370+
{
371+
let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
372+
assert_eq!(price_data.comp_[1].latest_.price_, 20);
373+
assert_eq!(price_data.comp_[1].latest_.conf_, 1);
374+
assert_eq!(price_data.comp_[1].latest_.pub_slot_, 10);
375+
assert_eq!(price_data.comp_[1].latest_.status_, PC_STATUS_TRADING);
376+
assert_eq!(price_data.valid_slot_, 8);
377+
assert_eq!(price_data.agg_.pub_slot_, 10);
378+
assert_eq!(price_data.agg_.price_, 10);
379+
assert_eq!(price_data.agg_.status_, PC_STATUS_TRADING);
380+
assert_eq!(price_data.prev_slot_, 8);
381+
assert_eq!(price_data.prev_price_, -100);
382+
assert_eq!(price_data.prev_conf_, 1);
383+
assert_eq!(price_data.prev_timestamp_, 4);
384+
}
315385
}
316386

317387
// Create an upd_price instruction with the provided parameters
@@ -324,3 +394,9 @@ fn populate_instruction(instruction_data: &mut [u8], price: i64, conf: u64, pub_
324394
cmd.publishing_slot = pub_slot;
325395
cmd.unused_ = 0;
326396
}
397+
398+
fn add_publisher(price_account: &mut AccountInfo, publisher_key: &Pubkey, index: usize) {
399+
let mut price_data = load_checked::<PriceAccount>(price_account, PC_VERSION).unwrap();
400+
price_data.num_ = (index + 1) as u32;
401+
price_data.comp_[index].pub_ = *publisher_key;
402+
}

0 commit comments

Comments
 (0)