|
| 1 | +use { |
| 2 | + crate::{ |
| 3 | + accounts::{ |
| 4 | + PermissionAccount, |
| 5 | + PriceAccount, |
| 6 | + PythAccount, |
| 7 | + }, |
| 8 | + c_oracle_header::PC_VERSION, |
| 9 | + deserialize::{ |
| 10 | + load_checked, |
| 11 | + load_mut, |
| 12 | + }, |
| 13 | + instruction::{ |
| 14 | + OracleCommand, |
| 15 | + SetMaxLatencyArgs, |
| 16 | + }, |
| 17 | + processor::set_max_latency, |
| 18 | + tests::test_utils::AccountSetup, |
| 19 | + }, |
| 20 | + solana_program::{ |
| 21 | + account_info::AccountInfo, |
| 22 | + program_error::ProgramError, |
| 23 | + pubkey::Pubkey, |
| 24 | + }, |
| 25 | + std::mem::size_of, |
| 26 | +}; |
| 27 | + |
| 28 | +#[test] |
| 29 | +fn test_set_max_latency() { |
| 30 | + let mut instruction_data = [0u8; size_of::<SetMaxLatencyArgs>()]; |
| 31 | + |
| 32 | + let program_id = Pubkey::new_unique(); |
| 33 | + |
| 34 | + let mut funding_setup = AccountSetup::new_funding(); |
| 35 | + let funding_account = funding_setup.as_account_info(); |
| 36 | + |
| 37 | + let mut price_setup = AccountSetup::new::<PriceAccount>(&program_id); |
| 38 | + let price_account = price_setup.as_account_info(); |
| 39 | + PriceAccount::initialize(&price_account, PC_VERSION).unwrap(); |
| 40 | + |
| 41 | + let mut permissions_setup = AccountSetup::new_permission(&program_id); |
| 42 | + let permissions_account = permissions_setup.as_account_info(); |
| 43 | + |
| 44 | + { |
| 45 | + let mut permissions_account_data = |
| 46 | + PermissionAccount::initialize(&permissions_account, PC_VERSION).unwrap(); |
| 47 | + permissions_account_data.master_authority = *funding_account.key; |
| 48 | + permissions_account_data.data_curation_authority = *funding_account.key; |
| 49 | + permissions_account_data.security_authority = *funding_account.key; |
| 50 | + } |
| 51 | + |
| 52 | + assert_eq!(get_max_latency(&price_account), Ok(0)); |
| 53 | + |
| 54 | + populate_instruction(&mut instruction_data, 10); |
| 55 | + assert!(set_max_latency( |
| 56 | + &program_id, |
| 57 | + &[ |
| 58 | + funding_account.clone(), |
| 59 | + price_account.clone(), |
| 60 | + permissions_account.clone() |
| 61 | + ], |
| 62 | + &instruction_data |
| 63 | + ) |
| 64 | + .is_ok()); |
| 65 | + assert_eq!(get_max_latency(&price_account), Ok(10)); |
| 66 | + |
| 67 | + populate_instruction(&mut instruction_data, 5); |
| 68 | + assert!(set_max_latency( |
| 69 | + &program_id, |
| 70 | + &[ |
| 71 | + funding_account.clone(), |
| 72 | + price_account.clone(), |
| 73 | + permissions_account.clone() |
| 74 | + ], |
| 75 | + &instruction_data |
| 76 | + ) |
| 77 | + .is_ok()); |
| 78 | + assert_eq!(get_max_latency(&price_account), Ok(5)); |
| 79 | +} |
| 80 | + |
| 81 | +// Populate the instruction data with SetMaxLatencyArgs |
| 82 | +fn populate_instruction(instruction_data: &mut [u8], max_latency: u8) { |
| 83 | + let mut hdr = load_mut::<SetMaxLatencyArgs>(instruction_data).unwrap(); |
| 84 | + hdr.header = OracleCommand::SetMaxLatency.into(); |
| 85 | + hdr.max_latency = max_latency; |
| 86 | +} |
| 87 | + |
| 88 | +// Helper function to get the max latency from a PriceAccount |
| 89 | +fn get_max_latency(account: &AccountInfo) -> Result<u8, ProgramError> { |
| 90 | + Ok(load_checked::<PriceAccount>(account, PC_VERSION)?.max_latency_) |
| 91 | +} |
0 commit comments