|
| 1 | +use { |
| 2 | + crate::{ |
| 3 | + accounts::{ |
| 4 | + PermissionAccount, |
| 5 | + PriceAccount, |
| 6 | + PriceAccountFlags, |
| 7 | + PythAccount, |
| 8 | + }, |
| 9 | + c_oracle_header::{ |
| 10 | + PC_STATUS_TRADING, |
| 11 | + PC_STATUS_UNKNOWN, |
| 12 | + PC_VERSION, |
| 13 | + }, |
| 14 | + deserialize::{ |
| 15 | + load_checked, |
| 16 | + load_mut, |
| 17 | + }, |
| 18 | + instruction::{ |
| 19 | + AddPublisherArgs, |
| 20 | + OracleCommand, |
| 21 | + UpdPriceArgs, |
| 22 | + }, |
| 23 | + processor::{ |
| 24 | + process_instruction, |
| 25 | + ALLOW_ZERO_CI, |
| 26 | + FORBID_ZERO_CI, |
| 27 | + }, |
| 28 | + tests::test_utils::{ |
| 29 | + update_clock_slot, |
| 30 | + AccountSetup, |
| 31 | + }, |
| 32 | + }, |
| 33 | + bytemuck::bytes_of, |
| 34 | + solana_program::pubkey::Pubkey, |
| 35 | + std::mem::size_of, |
| 36 | +}; |
| 37 | + |
| 38 | +struct Accounts { |
| 39 | + program_id: Pubkey, |
| 40 | + publisher_account: AccountSetup, |
| 41 | + funding_account: AccountSetup, |
| 42 | + price_account: AccountSetup, |
| 43 | + permissions_account: AccountSetup, |
| 44 | + clock_account: AccountSetup, |
| 45 | +} |
| 46 | + |
| 47 | +impl Accounts { |
| 48 | + fn new() -> Self { |
| 49 | + let program_id = Pubkey::new_unique(); |
| 50 | + let publisher_account = AccountSetup::new_funding(); |
| 51 | + let clock_account = AccountSetup::new_clock(); |
| 52 | + let mut funding_account = AccountSetup::new_funding(); |
| 53 | + let mut permissions_account = AccountSetup::new_permission(&program_id); |
| 54 | + let mut price_account = AccountSetup::new::<PriceAccount>(&program_id); |
| 55 | + |
| 56 | + PriceAccount::initialize(&price_account.as_account_info(), PC_VERSION).unwrap(); |
| 57 | + |
| 58 | + { |
| 59 | + let permissions_account_info = permissions_account.as_account_info(); |
| 60 | + let mut permissions_account_data = |
| 61 | + PermissionAccount::initialize(&permissions_account_info, PC_VERSION).unwrap(); |
| 62 | + permissions_account_data.master_authority = *funding_account.as_account_info().key; |
| 63 | + permissions_account_data.data_curation_authority = |
| 64 | + *funding_account.as_account_info().key; |
| 65 | + permissions_account_data.security_authority = *funding_account.as_account_info().key; |
| 66 | + } |
| 67 | + |
| 68 | + Self { |
| 69 | + program_id, |
| 70 | + publisher_account, |
| 71 | + funding_account, |
| 72 | + price_account, |
| 73 | + permissions_account, |
| 74 | + clock_account, |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +fn add_publisher(accounts: &mut Accounts, publisher: Option<Pubkey>) { |
| 80 | + let args = AddPublisherArgs { |
| 81 | + header: OracleCommand::AddPublisher.into(), |
| 82 | + publisher: publisher.unwrap_or(*accounts.publisher_account.as_account_info().key), |
| 83 | + }; |
| 84 | + |
| 85 | + assert!(process_instruction( |
| 86 | + &accounts.program_id, |
| 87 | + &[ |
| 88 | + accounts.funding_account.as_account_info(), |
| 89 | + accounts.price_account.as_account_info(), |
| 90 | + accounts.permissions_account.as_account_info(), |
| 91 | + ], |
| 92 | + bytes_of::<AddPublisherArgs>(&args) |
| 93 | + ) |
| 94 | + .is_ok()); |
| 95 | +} |
| 96 | + |
| 97 | +fn update_price(accounts: &mut Accounts, price: i64, conf: u64, slot: u64) { |
| 98 | + let instruction_data = &mut [0u8; size_of::<UpdPriceArgs>()]; |
| 99 | + let mut cmd = load_mut::<UpdPriceArgs>(instruction_data).unwrap(); |
| 100 | + cmd.header = OracleCommand::UpdPrice.into(); |
| 101 | + cmd.status = PC_STATUS_TRADING; |
| 102 | + cmd.price = price; |
| 103 | + cmd.confidence = conf; |
| 104 | + cmd.publishing_slot = slot; |
| 105 | + cmd.unused_ = 0; |
| 106 | + |
| 107 | + let mut clock = accounts.clock_account.as_account_info(); |
| 108 | + clock.is_signer = false; |
| 109 | + clock.is_writable = false; |
| 110 | + |
| 111 | + process_instruction( |
| 112 | + &accounts.program_id, |
| 113 | + &[ |
| 114 | + accounts.publisher_account.as_account_info(), |
| 115 | + accounts.price_account.as_account_info(), |
| 116 | + clock, |
| 117 | + ], |
| 118 | + instruction_data, |
| 119 | + ) |
| 120 | + .unwrap(); |
| 121 | +} |
| 122 | + |
| 123 | +#[test] |
| 124 | +fn test_aggregate_v2_toggle() { |
| 125 | + let accounts = &mut Accounts::new(); |
| 126 | + |
| 127 | + // Add an initial Publisher to test with. |
| 128 | + add_publisher(accounts, None); |
| 129 | + |
| 130 | + // Update the price, no aggregation will happen on the first slot. |
| 131 | + { |
| 132 | + update_clock_slot(&mut accounts.clock_account.as_account_info(), 1); |
| 133 | + update_price(accounts, 42, 2, 1); |
| 134 | + let info = accounts.price_account.as_account_info(); |
| 135 | + let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap(); |
| 136 | + assert_eq!(price_data.last_slot_, 0); |
| 137 | + assert!(!price_data.flags.contains(PriceAccountFlags::ACCUMULATOR_V2)); |
| 138 | + } |
| 139 | + |
| 140 | + // Update again, component is now TRADING so aggregation should trigger. |
| 141 | + { |
| 142 | + update_clock_slot(&mut accounts.clock_account.as_account_info(), 2); |
| 143 | + update_price(accounts, 43, 3, 2); |
| 144 | + let info = accounts.price_account.as_account_info(); |
| 145 | + let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap(); |
| 146 | + assert_eq!(price_data.agg_.status_, PC_STATUS_TRADING); |
| 147 | + assert_eq!(price_data.last_slot_, 2); |
| 148 | + assert_eq!(price_data.agg_.price_, 42); |
| 149 | + assert_eq!(price_data.agg_.conf_, 2); |
| 150 | + } |
| 151 | + |
| 152 | + // Update again, but with confidence set to 0, it should not aggregate *in the next slot*. |
| 153 | + { |
| 154 | + update_clock_slot(&mut accounts.clock_account.as_account_info(), 3); |
| 155 | + update_price(accounts, 44, 0, 3); |
| 156 | + let info = accounts.price_account.as_account_info(); |
| 157 | + let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap(); |
| 158 | + assert_eq!(price_data.agg_.status_, PC_STATUS_TRADING); |
| 159 | + assert_eq!(price_data.last_slot_, 3); |
| 160 | + assert_eq!(price_data.agg_.price_, 43); |
| 161 | + assert_eq!(price_data.agg_.conf_, 3); |
| 162 | + } |
| 163 | + |
| 164 | + // Update again, to trigger aggregation. We should see status go to unknown |
| 165 | + { |
| 166 | + update_clock_slot(&mut accounts.clock_account.as_account_info(), 4); |
| 167 | + update_price(accounts, 45, 0, 4); |
| 168 | + let info = accounts.price_account.as_account_info(); |
| 169 | + let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap(); |
| 170 | + println!("Price Data: {:?}", price_data.agg_); |
| 171 | + assert_eq!(price_data.agg_.status_, PC_STATUS_UNKNOWN); |
| 172 | + assert_eq!(price_data.last_slot_, 3); |
| 173 | + } |
| 174 | + |
| 175 | + // Enable allow zero confidence bit |
| 176 | + add_publisher(accounts, Some(ALLOW_ZERO_CI.into())); |
| 177 | + |
| 178 | + // Update again, with allow zero confidence bit set, aggregation should support |
| 179 | + // zero confidence values. Note that we don't need to do this twice, because the |
| 180 | + // price with ci zero was already stored in the previous slot. |
| 181 | + { |
| 182 | + update_clock_slot(&mut accounts.clock_account.as_account_info(), 5); |
| 183 | + update_price(accounts, 46, 0, 5); |
| 184 | + let info = accounts.price_account.as_account_info(); |
| 185 | + let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap(); |
| 186 | + assert!(price_data.flags.contains(PriceAccountFlags::ALLOW_ZERO_CI)); |
| 187 | + assert_eq!(price_data.agg_.status_, PC_STATUS_TRADING); |
| 188 | + assert_eq!(price_data.last_slot_, 5); |
| 189 | + assert_eq!(price_data.agg_.price_, 45); |
| 190 | + assert_eq!(price_data.agg_.conf_, 0); |
| 191 | + } |
| 192 | + |
| 193 | + // Disable allow zero confidence bit |
| 194 | + add_publisher(accounts, Some(FORBID_ZERO_CI.into())); |
| 195 | + |
| 196 | + // Update again, with forbid zero confidence bit set, aggregation should have status |
| 197 | + // of unknown |
| 198 | + { |
| 199 | + update_clock_slot(&mut accounts.clock_account.as_account_info(), 6); |
| 200 | + update_price(accounts, 47, 0, 6); |
| 201 | + let info = accounts.price_account.as_account_info(); |
| 202 | + let price_data = load_checked::<PriceAccount>(&info, PC_VERSION).unwrap(); |
| 203 | + assert!(!price_data.flags.contains(PriceAccountFlags::ALLOW_ZERO_CI)); |
| 204 | + assert_eq!(price_data.agg_.status_, PC_STATUS_UNKNOWN); |
| 205 | + } |
| 206 | +} |
0 commit comments